这是我参与8月更文挑战的第6天,活动详情查看:8月更文挑战
四、授权
4.1、授权概述
授权,即访问控制,控制谁能访问哪些资源。主体进行身份认证后需要分配权限方可访问系统的资源,对于某些资源没有权限是无法访问的。 系统中的授权功能就是为用户分配相关的权限,只有当用户拥有相应的权限后,才能访问对应的资源。 如果系统中无法管理用户的权限,那么将会出现客户信息泄露,数据被恶意篡改等问题,所以在绝大多数的应用中,我们都会有权限管理模块。一般基于角色的权限控制管理有以下三个子模块:
- 用户管理
- 角色管理
- 权限管理
4.2、 关键对象
授权可简单理解为who对what(which)进行How操作:
Who,即主体(Subject)
,主体需要访问系统中的资源。
What,即资源(Resource)
,如系统菜单、页面、按钮、类方法、系统商品信息等。资源包括资源类型
和资源实例
,比如商品信息为资源类型
,类型为t01的商品为资源实例
,编号为001的商品信息也属于资源实例。
How,权限/许可(Permission)
,规定了主体对资源的操作许可,权限离开资源没有意义,如用户查询权限、用户添加权限、某个类方法的调用权限、编号为001用户的修改权限等,通过权限可知主体对哪些资源都有哪些操作许可。
4.3、授权流程
4.4、授权方式
4.4.1、基于角色的访问控制
RBAC基于角色的访问控制(Role-Based Access Control)是以角色为中心进行访问控制
if(subject.hasRole("admin")){//主体具有admin角色
//操作什么资源
}
4.4.2、基于资源的访问控制
RBAC基于资源的访问控制(Resource-Based Access Control)是以资源为中心进行访问控制
if(subject.isPermission("user:find:*")){ //对用户模块的所有用户有查询权限
//对01用户进行修改
}
if(subject.isPermission("user:*:01")){ //user模块下的所有用户有所有权限
//对01用户进行修改
}
4.5、权限字符串
权限字符串的规则是:资源标识符:操作:资源实例标识符,意思是对哪个资源的哪个实例具有什么操作,“:”是资源/操作/实例的分割符,权限字符串也可以使用*通配符。
例子:
- 用户创建权限:user:create,或user:create:*
- 用户修改实例001的权限:user:update:001
- 用户实例001的所有权限:user:*:001
4.6、Shiro授权的实现方式
4.6.1、编程式
Subject subject = SecurityUtils.getSubject();
if(subject.hasRole(“admin”)) {
//有权限
} else {
//无权限
}
4.6.2、注解式
@RequiresRoles("admin")
public void hello() {
//有权限
}
4.6.3、标签式
JSP标签:在JSP 页面通过相应的标签完成:
<shiro:hasRole name="admin">
<!— 有权限—>
</shiro:hasRole>
注意: Thymeleaf/FreeMarker 中使用shiro需要额外集成!
4.7、基于ini的授权
4.7.1、编写ini文件
#用户的身份、凭据、角色
[users]
zhangsan=555,hr,seller
xiaolin=123,hr
#角色与权限信息
[roles]
hr=employee:list,employee:delete
seller=customer:list,customer:save
4.7.2、编写测试类
@Test
public void testAuthor(){
//创建Shiro的安全管理器,是shiro的核心
DefaultSecurityManager securityManager = new DefaultSecurityManager();
//加载shiro.ini配置,得到配置中的用户信息(账号+密码)
IniRealm iniRealm = new IniRealm("classpath:shiro-author.ini");
securityManager.setRealm(iniRealm);
//把安全管理器注入到当前的环境中
SecurityUtils.setSecurityManager(securityManager);
//无论有无登录都可以获取到subject主体对象,但是判断登录状态需要利用里面的属性来判断
Subject subject = SecurityUtils.getSubject();
System.out.println("认证状态:"+subject.isAuthenticated());
//创建令牌(携带登录用户的账号和密码)
UsernamePasswordToken token = new UsernamePasswordToken("dafei","666");
//执行登录操作(将用户的和 ini 配置中的账号密码做匹配)
subject.login(token);
System.out.println("认证状态:"+subject.isAuthenticated());
//登出
//subject.logout();
//System.out.println("认证状态:"+subject.isAuthenticated());
//判断用户是否有某个角色
System.out.println("role1:"+subject.hasRole("role1"));
System.out.println("role2:"+subject.hasRole("role2"));
//是否同时拥有多个角色
System.out.println("是否同时拥有role1和role2:"+subject.hasAllRoles(Arrays.asList("role1", "role2")));
//check开头的是没有返回值的,当没有权限时就会抛出异常
subject.checkRole("hr");
//判断用户是否有某个权限
System.out.println("user:delete:"+subject.isPermitted("user:delete"));
subject.checkPermission("user:delete");
}
4.7.3、自定义Realm
public class EmployeeRealm extends AuthorizingRealm {
//提供认证信息
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
throws AuthenticationException {
//暂且使用假数据来模拟数据库中真实的账号和密码
Employee employee = new Employee();
employee.setName("admin");
employee.setPassword("1");
//获取token中需要登录的账号名
String username = (String)token.getPrincipal();
//如果账号存在,则返回一个 AuthenticationInfo 对象
if(username.equals(employee.getName())){
return new SimpleAuthenticationInfo(
employee,//身份对象,与主体subject绑定在一起的对象,暂时无用但后续要用
employee.getPassword(),//该账号真正的密码,传给shiro做密码校验的
this.getName()//当前 Realm 的名称,暂时无用,不需纠结
);
}
return null;
}
//提供授权信息
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//登录成功用户对象
Employee employee = (Employee)principals.getPrimaryPrincipal();
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
// 根据登录用户的id查询到其拥有的所有角色的编码,这里进行模拟数据库
List<String> roleSns = Arrays.asList("hr","manager","seller");
// 将用户拥有的角色添加到授权信息对象中,供 Shiro 权限校验时使用
info.addRoles(roleSns);
// 根据登录用户的 id 查询到其拥有的所有权限表达式,这里进行模拟数据库
List<String> expressions = Arrays.asList("employee:list","employee:save");
// 将用户拥有的权限添加到授权信息对象中,供 Shiro 权限校验时使用
info.addStringPermissions(expressions);
return info;
}
}
4.8、SSM整合Shiro认证
在开发中,我们一般使用注解来完成授权操作, 我们需要使用 Shiro 自身提供的一套注解来完成。
4.8.1、贴注解
在 Controller 的方法上贴上 Shiro 提供的权限注解(@RequiresPermissions,@RequiresRoles)
// 说明需要有这个权限才可以访问这个方法
@RequiresPermissions(value = "employee:list")
@RequestMapping("list")
public String listAll(@ModelAttribute("qo") EmployeeQueryObject employeeQueryObject, Model model,
DepartmentQueryObject departmentQueryObject) {
PageInfo<Employee> pageInfo = employeeService.selectForPage(employeeQueryObject);
List<Department> departments = departmentService.selectAll();
List<Role> roles = roleService.selectAll();
model.addAttribute("departments", departments);
model.addAttribute("roles", roles);
model.addAttribute("pageInfo", pageInfo);
return "employee/list";
}
4.8.2、配置注解扫描
当扫描到 Controller 中有使用 @RequiresPermissions 注解时,会使用cglib动态代理为当前 Controller 生成代理对象,增强对应方法,进行权限校验
<!-- <aop:config/> 会扫描配置文件中的所有advisor,并为其创建代理 -->
<aop:config />
<!-- Pointcut advisor通知器, 会匹配所有加了shiro权限注解的方法 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager"/>
</bean>
4.8.3、修改自定义Realm
// 授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
// 创建info对象
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
// 获取当前登录主体对象
//Subject subject = SecurityUtils.getSubject();
// 获取主体的身份对象(这里获取的对象与认证方法doGetAuthenticationInfo返回的SimpleAuthenticationInfo对象的第一个参数是同一个对象)
Subject subject = SecurityUtils.getSubject();
Employee employee = (Employee) subject.getPrincipal();
// 判断是否是超级管理员
if (employee.getAdmin()) {
info.addRole("admin");
// 给他所有的权限
info.addStringPermission(" *:* ");
} else {
// 通过员工id拿到所有的角色
List<Role> roles = employeeService.selectRolesById(employee.getId());
for (Role role : roles) {
info.addRole(role.getSn());
}
//通过员工id查询出所有的权限
List<String> permissionByEmployeeId = employeeService
.getPermissionByEmployeeId(employee.getId());
info.addStringPermissions(permissionByEmployeeId);
}
return info;
}
4.8.4、配置自定义异常
@ExceptionHandler(AuthorizationException.class)
public String BussinessException(AuthorizationException exception, HttpServletResponse response,
HandlerMethod method) throws IOException {
exception.printStackTrace(); //方便开发的时候找bug
//如果原本控制器的方法是返回jsonresult数据,现在出异常也应该返回jsonresult
//获取当前出现异常的方法,判断是否有ResponseBody注解,有就代表需要返回jsonresult
if (method.hasMethodAnnotation(ResponseBody.class)) {
try {
response.setContentType("application/json;charset=UTF-8");
response.getWriter().print(JSON.toJSONString(new ResponseResult(false, "没有权限操作")));
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
//如果原本控制器的方法是返回视图页面,现在也应该返回视图页面
return "common/nopermission";
}
4.8.5、Shiro标签集成FreeMarker
在前端页面上,我们通常可以根据用户拥有的权限来显示具体的页面,如:用户拥有删除员工的权限,页面上就把删除按钮显示出来,否则就不显示删除按钮,通过这种方式来细化权限控制。我们需要使用Shiro标签来进行控制。要能够实现上面的控制,需要使用 Shiro 中提供的相关标签。
4.8.5.1、拓展FreeMarker标签
前端页面我们选择的是freemarker,而默认 freemarker 是不支持 shiro 标签的,所以需要对其功能做拓展,可以理解为注册 shiro 的标签,达到在freemarker 页面中使用的目的。
public class ShiroFreeMarkerConfig extends FreeMarkerConfigurer {
@Override
public void afterPropertiesSet() throws IOException, TemplateException {
//继承之前的属性配置,这不能省
super.afterPropertiesSet();
Configuration cfg = this.getConfiguration();
cfg.setSharedVariable("shiro", new ShiroTags());//注册shiro 标签,这里可以换成自己想要其他的标签,但是一般建议是用shiro
}
}
4.8.5.2、修改mvc.xml中的配置
在mvc.xml 中把以前的FreeMarkerConfigurer修改成我们自定义的MyFreeMarkerConfig类
<!-- 注册 FreeMarker 配置类 -->
<bean class="cn.linstudy.shiro.ShiroFreeMarkerConfig">
<!-- 配置 FreeMarker 的文件编码 -->
<property name="defaultEncoding" value="UTF-8"/>
<!-- 配置 FreeMarker 寻找模板的路径 -->
<property name="templateLoaderPath" value="/WEB-INF/views/"/>
<property name="freemarkerSettings">
<props>
<!-- 兼容模式 ,配了后不需要另外处理空值问题,时间格式除外 -->
<prop key="classic_compatible">true</prop>
<!-- 数字格式化 , 不会有,字符串的 -->
<prop key="number_format">0.##</prop>
</props>
</property>
</bean>
4.8.5.3、常用标签
4.8.5.3.1、authenticated标签
authenticated标签里面囊括的表示的是已经通过了认证了的用户才会显示的前端界面
<@shiro.authenticated> </@shiro.authenticated>
4.8.5.3.2、notAuthenticated标签
与authenticated标签相对立,表示为认证通过的用户。
<@shiro.notAuthenticated></@shiro.notAuthenticated>
4.8.5.3.3、principal 标签
principal 标签表示的是输出当前用户的信息。,通常可以用来输出登录用户的用户名。
<@shiro.principal property="name" />
4.8.5.3.4、hasRole 标签
hasRole表示验证当前用户是否拥有某些角色。
<@shiro.hasRole name="admin">Hello admin!</@shiro.hasRole>
4.8.5.3.5、hasAnyRoles 标签
hasAnyRoles标签表示验证当前用户是否拥有这些角色中的任何一个,角色之间逗号分隔。
<@shiro.hasAnyRoles name="admin,user">Hello admin</@shiro.hasAnyRoles>
4.8.5.3.6、hasPermission 标签
hasPermission 标签表示验证当前用户是否拥有该权限。
<@shiro.hasPermission name="department:delete">删除</@shiro.hasPermission>
4.8.6、重构权限加载方法
这里要说的是一种思想,我们在项目中可能会遇到需要加载项目中加了@RequiresPermissions注解的权限,就会有一个类似加载权限的按钮。
但是我们发现好像Shiro的@RequiresPermissions注解并没有提供name属性给我们,仅仅只有value属性,那么我们需要另辟蹊径来完成这个需求。 Shiro的@RequiresPermissions注解有两个属性:
- value属性:这个属性是一个数组,也就是说一个请求映射方法可以运行配置多个权限。多个权限之间用逗号隔开
value={"employee:list","employee:delete", }
-
logical 属性:该属性根据配置属性值对当前用户是否有权限访问请求映射方法进行限制,他有两个值:
Logical.AND: 必须同时拥有value配置所有权限才允许访问。 Logical.OR:只需要拥有value配置所有权限中一个即可允许访问。
我们可以约定@RequiresPermissions 注解中的value属性值(数组)中第一位为权限表达式, 第二位为权限名称。
// shiro注解无法使用name属性,所以约定,value中第一个位置的值是权限表达式,第二个位置的值是权限名称.
// 但是 logical 的值必须是Logical.OR
@RequiresPermissions(value = {"employee:list", "员工列表"}, logical = Logical.OR)
@RequestMapping("list")
public String listAll(@ModelAttribute("qo") EmployeeQueryObject employeeQueryObject, Model model,
DepartmentQueryObject departmentQueryObject) {
PageInfo<Employee> pageInfo = employeeService.selectForPage(employeeQueryObject);
List<Department> departments = departmentService.selectAll();
List<Role> roles = roleService.selectAll();
model.addAttribute("departments", departments);
model.addAttribute("roles", roles);
model.addAttribute("pageInfo", pageInfo);
return "employee/list";
}
同时修改reload方法
public void reload() {
//一次性的把所有的权限信息查出来.
List<Permission> permissionsOnDB = permissionMapper.selectAll();
// 拿到所有方法
Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
// 遍历所有方法
for (HandlerMethod method : handlerMethods.values()) {
// 判断方法上是否有RequiresPermissions注解
RequiresPermissions annotation = method.getMethodAnnotation(RequiresPermissions.class);
if (annotation != null) {
// 如果不为空。说明是贴了注解的
// 数组的第一个元素表示的是权限表达式
String expression = annotation.value()[0];
// 数组的第二个元素表示的是权限的名称
String name = annotation.value()[1];
// 在存入数据库之前先判断以下,如果数据库中没有才插入
if (!permissionsOnDB.contains(expression)) {
Permission permission = new Permission();
permission.setName(name);
permission.setExpression(expression);
permissionMapper.insert(permission);
}
}
}
}
五、Shiro密码加密
加密的目的是从系统数据的安全考虑,如,用户的密码,如果我们不对其加密,那么所有用户的密码在数据库中都是明文,只要有权限查看数据库的都能够得知用户的密码,这是非常不安全的。所以,只要密码被写入磁盘,任何时候都不允许是明文, 以及对用户来说非常机密的数据,我们都应该想到使用加密技术,这里我们采用的是 MD5+盐+散列次数来进行加密。
如何实现项目中密码加密的功能:
-
添加用户的时候,对用户的密码进行加密
-
登录时,按照相同的算法对表单提交的密码进行加密然后再和数据库中的加密过的数据进行匹配
5.1、MD5+盐加密
MD5 加密的数据如果一样,那么无论在什么时候加密的结果都是一样的,所以,相对来说还是不够安全,但是我们可以对数据加“盐”。同样的数据加不同的“盐”之后就是千变万化的,因为我们不同的人加的“盐”都不一样。这样得到的结果相同率也就变低了。
盐一般要求是固定长度的字符串,且每个用户的盐不同。
可以选择用户的唯一的数据来作为盐(账号名,身份证等等),注意使用这些数据作为盐要求是不能改变的,假如登录账号名改变了,则再次加密时结果就对应不上了。
5.2、Md5Hash()
Md5Hash()这个方法有三个参数,第一个参数表示需要加密的密码的明文,第二个参数表示加密时所需要的盐,第三个参数表示散列次数(加密几次),这样可以保证加密后的密文很难恢复和破解。
5.3、注册用户(密码加密)
在添加用户的时候,需要对用户的密码进行加密。
@RequestMapping("checkUsername")
@ResponseBody
public ResponseResult checkUsername(String username, String password, HttpSession session) {
Employee employee = employeeService.selectByUsername(username);
if (employee == null) {
// 进行MD5密码加密
Md5Hash md5Hash = new Md5Hash(password, username, 1024);
EmployeeInsertVO employeeVO = new EmployeeInsertVO(username, username, md5Hash.toString(),
session.getAttribute("EMAIL_IN_SESSION").toString(), false, true);
employeeService.regsiter(employeeVO);
return new ResponseResult(true, "注册成功");
} else {
return new ResponseResult(false, "用户名已存在");
}
}
5.4、登录
在登录时, 先对前端传过来的密码进行与注册相同的相同算法的加密,再传给shiro进行认证处理即可。
try {
// 对传进来的密码进行加密
Md5Hash md5Hash = new Md5Hash(password, username, 1024);
UsernamePasswordToken token = new UsernamePasswordToken(username, md5Hash.toString());
SecurityUtils.getSubject().login(token);
Employee employee = employeeMapper.selectByUsername(username);
return employee;
} catch (UnknownAccountException e) {
throw new CarBussinessException("用户名错误");
} catch (IncorrectCredentialsException e) {
throw new CarBussinessException("密码错误");
}
}
六、Shiro集成EhCache
6.1、Cache是什么
Cache是缓存,他是计算机内存中一段数据 ,他的作用是 用来减轻DB的访问压力,从而提高系统的查询效率。
6.2、使用缓存的原因
我们在进行Debug的时候,我们会发现,一旦请求到需要权限控制的方法的时候,每请求一次他都会去调用自定义Realm中的 doGetAuthorizationInfo 方法获取用户的权限信息,这个时候对数据库造成的访问压力是十分大的,而且用户登陆后,授权信息一般很少变动,所以我们可以在第一次授权后就把这些授权信息存到缓存中,下一次就直接从缓存中获取,避免频繁访问数据库。
6.3、集成EhCache
6.3.1、引入依赖
<!--引入shiro和ehcache-->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.5.3</version>
</dependency>
6.3.2、添加缓存配置文件
<ehcache>
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LRU">
</defaultCache>
</ehcache>
配置文件属性详解: maxElementsInMemory: 缓存对象最大个数。
**eternal **:对象是否永久有效,一但设置了,timeout 将不起作用。
timeToIdleSeconds: 对象空闲时间,指对象在多长时间没有被访问就会失效(单位:秒)。仅当 eternal=false 对象不是永久有效时使用,可选属性,默认值是 0,也就是可闲置时间无穷大。
timeToLiveSeconds:对象存活时间,指对象从创建到失效所需要的时间(单位:秒)。仅当 eternal=false 对象不是永久有效时使用,默认是 0,也就是对象存活时间无穷大。
memoryStoreEvictionPolicy:当达到 maxElementsInMemory 限制时,Ehcache 将会根据指定的策略去清理内存。
缓存策略一般有3种:
-
默认LRU(最近最少使用,距离现在最久没有使用的元素将被清出缓存)。
-
FIFO(先进先出, 如果一个数据最先进入缓存中,则应该最早淘汰掉)。
-
LFU(较少使用,意思是一直以来最少被使用的,缓存的元素有一个hit 属性(命中率),hit 值最小的将会被清出缓存)。
6.3.2、配置缓存管理器
<!--安全管理器-->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!--注册自定义数据源-->
<property name="realm" ref="employeeRealm"/>
<!--注册缓存管理器-->
<property name="cacheManager" ref="cacheManager"/>
</bean>
<!-- 缓存管理器 -->
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<!-- 设置配置文件 -->
<property name="cacheManagerConfigFile" value="classpath:shiro-ehcache.xml"/>
</bean>