关于 Apache Shiro 概念基本都粘自官网 http://shiro.apache.org/
详细中文博客 http://wiki.jikexueyuan.com/p...
与SpringBoot整合 https://segmentfault.com/a/11...
Apache Shiro是一个功能强大且灵活的开源安全框架,能够清晰地处理身份验证,受权,企业会话管理和加密。html
如下是Apache Shiro能够作的一些事情:前端
能够参考官方文档:http://shiro.apache.org/archi...web
缓存架包先用 ehcachespring
<dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-all</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependency>
<?xml version="1.0" encoding="UTF-8"?> <ehcache> <!-- mac 电脑, 跟 win 设置路径有点不同 示例: path="d:/ehcache/" --> <diskStore path="${user.home}/Downloads/ehcache" /> <!-- 默认缓存配置 没有特别指定就用这个配置 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" memoryStoreEvictionPolicy="LRU" diskExpiryThreadIntervalSeconds="120" /> </ehcache>
<!-- 1. 配置 shiro 拦截器 --> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!-- 2. 配置shiro的核心组件 SecurityManager --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <!-- 配置缓存 --> <property name="cacheManager" ref="cacheManager" /> <!-- 自定义的 realm --> <property name="realm"> <bean class="com.ogemray.shiro.MyShiroRealm"> <!-- 自定义 realm 里面的的密码匹对器 --> <property name="credentialsMatcher"> <bean class="com.ogemray.shiro.MyCredentialsMatcher"/> </property> </bean> </property> </bean> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property> </bean> <!-- 3. 配置shiroFilter,配置shiro的一些基本规则信息,id必须和web.xml中配置的拦截器名字同样(DelegatingFilterProxy 经过名字去spring容器中找注入的拦截器) --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login.html"/> <!-- loginUrl 表示登陆页面地址 --> <property name="successUrl" value="/admin/main.html"/> <!-- successUrl 表示登陆成功后跳转的页面 --> <property name="unauthorizedUrl" value="/unauthorized.html"/> <!-- 配置没有受权跳转页面 --> <property name="filterChainDefinitions"> <value> <!--/logout.action=logout --> <!-- logout 表示登出, 清空session, 这里不须要了, 由于已经在登出对象的方法里手动清空了 [ SecurityUtils.getSubject().logout() ] --> /admin/userlist*=roles[user] /admin/adduser*=roles[user,admin] <!-- 表示拥有 user 角色 而且 拥有 admin 角色 --> /admin/editRPRelation*=roles[admin],perms[user:insert,user:update,user:select,user:delete] /admin/editURRelation*=perms[user:select] /admin/**=authc /**=anon </value> </property> </bean>
拦截器 shiroFilter 的基本数据数据库
关于过滤器能够参考shiro提供的枚举类 org.apache.shiro.web.filter.mgt.DefaultFilterapache
url 模式使用 Ant 风格模式
Ant 路径通配符支持?、、,注意通配符匹配不包括目录分隔符 “/”:
?:匹配一个字符,如”/admin?” 将匹配 / admin1,但不匹配 / admin 或 / admin2;
:匹配零个或多个字符串,如 / admin * 将匹配 / admin、/admin123,但不匹配 / admin/1;
:匹配路径中的零个或多个路径,如 / admin/ 将匹配 / admin/a 或 / admin/a/b。segmentfault
public class MyShiroRealm extends AuthorizingRealm { @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { System.out.println("********************* 进行登陆认证 *********************"); String username = (String) authenticationToken.getPrincipal(); //获取提交的用户名 User user = userRepository.findByUsername(username); if (user == null) throw new UnknownAccountException("用户不存在, 请先注册而后再来登陆"); if (user.getState() == 1) throw new LockedAccountException("该用户已经被管理员禁用, 请换个帐号登陆"); //接下来进行密码的比对逻辑 //参数 principal 做为下面受权部分参数集合里面的一部分 //参数 credentials 做为后面与token里面密码比对基础 //返回值 info 做为下面自定义密码匹类里面比对方法的参数 SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user, user.getPassword(), getName()); return info; } @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { System.out.println("********************* 进行受权认证 *********************"); User user = (User) principalCollection.asList().get(0); //获得该用户的全部角色和权限 Set<String> roles = new HashSet<>(); Set<String> permissions = new HashSet<>(); user.getRoles().forEach(role -> { roles.add(role.getRoleName()); role.getPermissions().forEach(permission -> { permissions.add(permission.getPermissionName()); }); }); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.setRoles(roles); info.setStringPermissions(permissions); return info; } @Autowired private UserRepository userRepository; }
前端密码加密规则:ciphertext_pwd = AES.encrypt(MD5(password))
后端解密密码规则:md5_password = AES.desEncrypt(ciphertext_pwd)
后端匹对密码规则:(md5_password + 用户名作盐值) 进行 1024 次 MD5 转换,而后与数据库取出密码作比对后端
public class MyCredentialsMatcher implements CredentialsMatcher { @Override public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) { String tokenCredentials = new String((char[])token.getCredentials()); //前端传过来的密码 String accountCredentials = (String) info.getCredentials(); //数据库查询到的密码 //首先对前端传过来的密码进行AES解密 -> 清空 session 里面的key Session session = SecurityUtils.getSubject().getSession(); String key = (String) session.getAttribute("AESKey"); try { tokenCredentials = AesEncryptUtil.desEncrypt(tokenCredentials, key, key); } catch (Exception e) { throw new IncorrectCredentialsException("可能受到重放攻击, AES 解密失败"); } session.removeAttribute("AESKey"); //加密方式 待加密数据 加密盐值 加密次数 SimpleHash simpleHash = new SimpleHash("MD5", tokenCredentials, token.getPrincipal(), 1024); tokenCredentials = simpleHash.toString(); return accountCredentials.equals(tokenCredentials); } }
@Service("userService") public class UserServiceImpl implements UserService { @Override public void registerUser(User user) { if (userRepository.existsByUsername(user.getUsername())) { throw new RuntimeException("用户名已经被注册, 请换个用户名"); } user.setState((byte) 0); //密码进行加密 SimpleHash simpleHash = new SimpleHash("MD5", user.getPassword(), user.getUsername(), 1024); user.setPassword(simpleHash.toString()); userRepository.save(user); } @Override public void login(User user) { Subject currentUser = SecurityUtils.getSubject(); if (currentUser.isAuthenticated() == false) { //没有登陆过须要进行登陆验证 UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword(), false); currentUser.login(token); } } @Autowired private UserRepository userRepository; }
从上面咱们能够看到url地址规则匹配能够配置多角色和多权限,当对应多个角色和权限时中间用 “,” 隔开。spring-mvc
以对应多角色为例缓存
/admin/userlist*=roles[user] /admin/adduser*=roles[user,admin]
当访问第二个 /admin/adduser*
时须要同时拥有 user
和 admin
角色,可是有时咱们须要他们之间是或者的关系,这个时候咱们就须要自定义对应的过滤器。
以自定角色过滤器为例
<!-- 配置shiroFilter,配置shiro的一些基本规则信息 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> <property name="loginUrl" value="/login.html"/> <property name="successUrl" value="/admin/main.html"/> <property name="unauthorizedUrl" value="/unauthorized.html"/> <property name="filterChainDefinitions"> <value> ... /admin/userlist*=roles[user] /admin/adduser*=roles[user,admin] ... </value> </property> <property name="filters"> <map> <entry key="perms"> <bean class="com.ogemray.shiro.MyRolesAuthorizationFilter"></bean> </entry> </map> </property> </bean>
public class MyRolesAuthorizationFilter extends AuthorizationFilter { public boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws IOException { Subject subject = this.getSubject(request, response); String[] rolesArray = (String[])((String[])mappedValue); if (rolesArray != null && rolesArray.length != 0) { for (String role : rolesArray) { if (subject.hasRole(role)) return true; } } return false; } }
启动web项目看下自定义过滤器有没有加进去
在有些方法多,可是权限分的细的地方用注解要比用配置的方案来的方便
使用注解首先要在spring-mvc.xml配置文件中加入如下配置
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"></bean> <!-- 开启shiro的注解支持 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <!-- 必须改成true,即便用cglib方式为Action建立代理对象。默认值为false,使用JDK建立代理对象,会形成问题 --> <property name="proxyTargetClass" value="true"/> </bean> <!-- 使用shiro框架提供的切面类,用于建立代理对象 --> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"></property> </bean>
在spring.xml配置文件对于过滤器的配置就简单多了
没有那些繁杂的配置规则和跳转页面
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager"/> </bean>
5个权限注解
上面全部要求的权限和认证没有时就会抛出对应的异常,只需在SpringMVC中写好对应的异常截获方法便可
示例
//表示须要认证 @RequiresAuthentication //表示在须要认证的基础上要同时拥有 user 和 admin 角色 @RequiresRoles(value = {"user", "admin"}) //同上 @RequiresRoles(value = {"user", "admin"}, logical = Logical.AND) //表示在须要认证的基础上拥有 user 或 admin 角色 @RequiresRoles(value = {"user", "admin"}, logical = Logical.OR)