Springboot整合Shiro:实现Redis缓存
项目整合Shiro后,在没有配置缓存的时候,会存在这样的问题。每发起一个请求,就会调用一次授权方法。用户基数大请求多的时候,会对数据库造成很大的压力。所以我们需要配置缓存,将用户信息放在缓存里,从而减小数据库压力。
自定义Realm中两个核心方法
认证:doGetAuthenticationInfo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken auth) throws AuthenticationException { String token = (String) auth.getPrincipal();
String username; try { username = this.jwtUtils.getUsername(token);
if (username == null || !this.jwtUtils.verify(token, username, this.jwtUtils.getSecret())) { if (this.jwtUtils.isExpire(token)) { throw new ExpiredCredentialsException("token过期,请重新登入!"); } throw new IncorrectCredentialsException("token值异常(2)!!!"); }
} catch (JWTDecodeException | IllegalArgumentException e) { e.printStackTrace(); throw new IncorrectCredentialsException("token值异常(1)!!!!"); } catch (AuthenticationException e) { e.printStackTrace(); throw new IncorrectCredentialsException(e.getMessage()); }
User user = this.userService.findUserByUsername(username); if (user == null) { throw new UnknownAccountException("账号不存在!"); }
return new SimpleAuthenticationInfo(user, token, this.getName()); }
|
授权:doGetAuthorizationInfo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { User user = (User) principals.getPrimaryPrincipal(); System.out.println("调用授权验证:" + user.getUsername()); User realUser = userService.findRolesByUserName(user.getUsername()); if (!CollectionUtil.isEmpty(realUser.getRoles())) { SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); System.out.println(realUser.getRoles()+"-------------------------"); realUser.getRoles().forEach(role -> { simpleAuthorizationInfo.addRole(role.getName()); System.out.println(role.getName()+"================================");
List<Permission> permissions = roleService.findPermissionsByRoleId(role.getId()); if (!CollectionUtil.isEmpty(permissions)) { permissions.forEach(permission -> { simpleAuthorizationInfo.addStringPermission(permission.getName()); System.out.println(permission.getName()+"==============AAAAAAAAAA=================="); }); } }); return simpleAuthorizationInfo; } return null; }
|
自定义缓存管理器
我们这里用redis做缓存,下面说下配置redis缓存的方法。
(1)application.yml中配置redis的相关参数
1 2 3 4 5 6 7 8 9 10 11 12
| spring: redis: host: localhost port: 6379 jedis: pool: max-idle: 8 min-idle: 0 max-active: 8 max-wait: -1 timeout: 0
|
(2)pom.xml文件中引入shiro-redis依赖
1 2 3 4 5 6
| <!-- shiro+redis缓存插件 --> <dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis</artifactId> <version>2.4.2.1-RELEASE</version> </dependency>
|
(3)ShiroConfig.java中添加相应的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
public RedisManager redisManager() { RedisManager redisManager = new RedisManager(); redisManager.setHost(host); redisManager.setPort(port); redisManager.setExpire(1800); return redisManager; }
public RedisCacheManager cacheManager() { RedisCacheManager redisCacheManager = new RedisCacheManager(); redisCacheManager.setRedisManager(redisManager()); return redisCacheManager; }
public RedisSessionDAO redisSessionDAO() { RedisSessionDAO redisSessionDAO = new RedisSessionDAO(); redisSessionDAO.setRedisManager(redisManager()); return redisSessionDAO; }
public DefaultWebSessionManager SessionManager() { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setSessionDAO(redisSessionDAO()); return sessionManager; }
|
(4)将session管理器和cache管理器注入到SecurityManager中
1 2 3 4 5 6 7 8 9 10 11
| @Bean public SecurityManager securityManager(){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(new CustomRealm()); securityManager.setCacheManager(cacheManager()); securityManager.setSessionManager(SessionManager()); return securityManager; }
|
(5)redis-server.exe启动redis,启动项目,完成。
未登录时,在redis中查看数据,得到空的结果。(empty list or set)
完成认证和授权后可以在redis中得到相应的信息。
参考
Springboot整合Shiro:实现Redis缓存