登录模块:在登录时必须调用

授权模块:不是一登录就调动,而是当角色权限控制时才会调用

登录控制

环境搭建在上一篇。

数据库表

表名:role 字段:id rolename

表名:user 字段:id username sex roleid

在代码中简历数据库表对应的实体类

code

userController.java

//添加如下方法
@RequestMaping("/loginAction")
public String loginAction(String name,String password){
Subject subject = SecurityUtiles.getSubject();
 MD5Hash md5Hash= new Md5Hash(password,"123").toString();
AuthenticationTocken tocken = new UsernamePasswordTocken(username,password);
		try{
		//如果执行subject.login(tocken);没有抛出异常,则证明登陆成功。
		//走到这里会进入下一个类
			subject.login(tocken);
				return "login Success"
		}catch(Exception e){
		//如果执行subject.login(tocken);抛出异常,则证明登陆失败。
			return "login Failed"
		}
		return "loginPage";
}

UserRealm.java

//控制登录
@Overrride
protected AuthenticationInfi doGEtAuthenticationInfo(AuthenticationTocken at)throws AuthenticationException{
		AuthenticationInfo info;
		String usersname = (String)at.getPrincipal;//从tocken里面得到username
		User user = userService.selectuserByUsername(username);
		if(user!=null){
		info = new SimpleAuthenticationInfo(username,user.getPassword(),"xxxxx");
		//执行到这里会自动和调用这个类的上面那个类里面的username和password
		进行比较
		return info;
		}
		return null;
}

权限控制

数据库

表名: permission (权限表)字段: id pername roleid

表名 :role(角色表)字段 :id rolename

表名:user 字段:id username sex roleid

在代码中简历数据库表对应的实体类

code

UserRealm.java

@Overrride
protected AuthenticationInfi doGEtAuthenticationInfo(AuthenticationTocken at)throws AuthenticationException{
		SimpleAuthenticationInfo info = new SimpleAuthenticationInfo();
		String usersname = (String)at.getPriMaryPrincipal;//从tocken里面得到username
		//将当前用户的角色和权限复制进来
		User user = userService.selectuserByUsername(username);
		Integer roleid = user.getRoleid();
		Role role = roleMapper.selectById(roleid);

		info.addRole(role.getRolename());

		Wrapper<Permission> wrapper = new EntityWrapper<>();
		wrapper.eq("roleid",role.getId(0);
		List<Permission> selectList = perMapper.seletList(wrapper);
		List<String> perlist = new ArrayList<>();
		selectList.forEach(per -> {
			perlist.add(per.getRolename());
		}
		)
		infoi.addStringPermissions(perlist);

		return info;
}

UserController.java

@RequiresRoles("admin")//当用户是这个的时候才能执行下面的方法
@RequestMapping("/getuser")
public Object getUser(Integer id){
	return userMapper.selectById(id);
}
@RequiresPermissions("sys:delete")//只有有这个权限的时候才能执行下面的方法
@RequestMapping("/deleteuser")
public Object deleteuser(Integer id){
	return userMapper.deleteById(id);
}

SpringBoot学习笔记(六):SpringBoot实现Shiro登录控制和权限控制的更多相关文章

  1. springboot学习笔记-5 springboot整合shiro

    shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...

  2. SpringBoot学习笔记(6) SpringBoot数据缓存Cache [Guava和Redis实现]

    https://blog.csdn.net/a67474506/article/details/52608855 Spring定义了org.springframework.cache.CacheMan ...

  3. springboot学习笔记-6 springboot整合RabbitMQ

    一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...

  4. springboot+mybatis+shiro——登录认证和权限控制

    转载:https://z77z.oschina.io/ 一.引入依赖 shiro-all包含shiro所有的包.shiro-core是核心包.shiro-web是与web整合.shiro-spring ...

  5. 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)

    http://blog.csdn.net/a67474506/article/details/61640548 Dubbo是什么东西我这里就不详细介绍了,自己可以去谷歌 SpringBoot整合Dub ...

  6. SpringBoot学习笔记(16)----SpringBoot整合Swagger2

    Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务 http://swagger.io Springfox的前身是swagger-springmvc,是 ...

  7. SpringBoot学习笔记(15)----SpringBoot使用Druid

    直接访问Druid官网wiki,有详细教程,地址如下: SpringBoot支持Druid地址:https://github.com/alibaba/druid/tree/master/druid-s ...

  8. SpringBoot学习笔记(11)-----SpringBoot中使用rabbitmq,activemq消息队列和rest服务的调用

    1. activemq 首先引入依赖 pom.xml文件 <dependency> <groupId>org.springframework.boot</groupId& ...

  9. SpringBoot学习笔记(10)-----SpringBoot中使用Redis/Mongodb和缓存Ehcache缓存和redis缓存

    1. 使用Redis 在使用redis之前,首先要保证安装或有redis的服务器,接下就是引入redis依赖. pom.xml文件如下 <dependency> <groupId&g ...

  10. SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理

    在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...

随机推荐

  1. 大道浮屠诀---mysql5.7.28 for linux安装

    环境: redhat6.5 MySQL Community Server 5.7.28 https://dev.mysql.com/downloads/mysql/5.7.html 安装RMP包的具体 ...

  2. 【POJ】1797 Heavy Transportation

    题目链接:http://poj.org/problem?id=1797 题意:n个城镇,m条路上能承载的最大重量.现在问你从1到n的最大承重量. 题解:spfa的变体. 假设当前1->当前点的承 ...

  3. php 图片旋转和png透明

    因需要先处理生成的二维码图片旋转,再和另外一张png图片合并,图片都是png <?php // this file writes the image into the http response ...

  4. 一个切图仔的 JS 笔记

    1,常用数据操作 Math.round(mum,2);num.toFixed(2);两位数 Math.floor(); 返回不大于的最大整数 Math.ceil(); 则是不小于他的最小整数 Math ...

  5. 廖雪峰Java16函数式编程-2Stream-6reduce

    1. 聚合方法 Stream.reduce()是一个Stream的聚合方法:把一个Stream的所有元素聚合成一个结果 例如: Stream.of(1, 2, 3, 4, 5).count(); // ...

  6. 长链接生成短链接Java源码(调用百度接口)

    public static DefaultHttpClient httpclient; static { httpclient = new DefaultHttpClient(); //httpcli ...

  7. excel导入、下载功能

    1.excel导入.下载功能 2.首先,我们是居于maven项目进行开发引入poi,如果不是那就手动下载相应的jar包引入项目就可以了 <!-- poi --> <dependenc ...

  8. 计算几何——直线交点poj1269

    求直线交点还是要推一个公式的.. 见博客https://blog.csdn.net/u013050857/article/details/40923789 还要学一下向量的定点比分法 另外poj精度好 ...

  9. hibernate_04_hibernate多对多的关系映射

    1.实体类的多对多的关系映射 一个用户可以有多个角色 User.java public class User { private Long user_id; private String user_c ...

  10. arcmap分类标注问题

    在给图层标注的时候,经常出现冲突后有些标注出不来,需要将某些个别的点要素进行标注位置调整,如下图: 处理步骤如下, (1)打开Maplex标注引擎.从ToolBars中打开Labeling工具,勾选U ...