首先引入相关依赖:
shiro-redis-spring-boot-starter在maven仓库里搜不到,自己复制引入就好。我记得是整合的部分其他依赖(比如spring-boot-starter)然后外加上大佬制作的shiro-redis依赖,可以引入后自己看一下具体依赖,然后移除之前引入的重复依赖。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency> <dependency> <groupId>org.crazycake</groupId> <artifactId>shiro-redis-spring-boot-starter</artifactId> <version>3.2.1</version> </dependency>
|
相关配置:
包括开启缓存,redis相关配置,缓存前缀等
1 2 3 4 5 6 7 8 9 10
| shiro-redis: enabled: true redis-manager: host: 127.0.0.1:6379 password: cache-manager: key-prefix: "shiro:cache:" expire: 600 session-dao: key-prefix: "shiro:session:"
|
自定义realm:
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 43 44 45 46 47
| @Component public class MyRealm extends AuthorizingRealm {
@Autowired private UserService userService;
@Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { User principal = (User) principalCollection.getPrimaryPrincipal(); List<String> userRoles= userService.getUserRoles(principal.getUsername()); List<String> userPermissions = userService.getUserPermissions(userRoles); SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(); info.addRoles(userRoles); info.addStringPermissions(userPermissions); return info; }
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String userName = authenticationToken.getPrincipal().toString(); User userByName = userService.getUserByName(userName); if(userByName.getStatus()==1) { throw new LockedAccountException(); } if(userByName!=null){ AuthenticationInfo info = new SimpleAuthenticationInfo( userByName, userByName.getPassword(), ByteSource.Util.bytes("yahoo"), userName ); return info; } return null; } }
|
配置类:
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| @Configuration public class ShiroConfig { @Autowired private MyRealm myRealm;
@Autowired private RedisSessionDAO redisSessionDAO;
@Autowired private RedisCacheManager redisCacheManager;
@Bean public DefaultWebSecurityManager defaultWebSecurityManager() { DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager(); HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(); matcher.setHashAlgorithmName("md5"); matcher.setHashIterations(3); myRealm.setCredentialsMatcher(matcher); defaultWebSecurityManager.setRealm(myRealm); defaultWebSecurityManager.setRememberMeManager(rememberMeManager()); defaultWebSecurityManager.setCacheManager(redisCacheManager); defaultWebSecurityManager.setSessionManager(sessionManager()); return defaultWebSecurityManager; }
@Bean(name = "sessionManager") public DefaultWebSessionManager sessionManager() { DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); sessionManager.setGlobalSessionTimeout(600000L); sessionManager.setSessionDAO(redisSessionDAO); return sessionManager; } public CookieRememberMeManager rememberMeManager() { CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager(); cookieRememberMeManager.setCookie(rememberMeCookie()); cookieRememberMeManager.setCipherKey("1234567890987654".getBytes()); return cookieRememberMeManager; } public SimpleCookie rememberMeCookie() { SimpleCookie cookie = new SimpleCookie("rememberMe"); cookie.setPath("/"); cookie.setHttpOnly(true); cookie.setMaxAge(30 * 24 * 60 * 60); return cookie; }
@Bean("shiroFilterFactoryBean") public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager securityManager) { ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); Map<String, String> urlMap = new LinkedHashMap<>(); urlMap.put("/user/login", "anon");
shiroFilterFactoryBean.setLoginUrl("/user/login");
shiroFilterFactoryBean.setFilterChainDefinitionMap(urlMap); return shiroFilterFactoryBean; } @Bean public ShiroFilterChainDefinition shiroFilterChainDefinition() { DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition(); Map<String, String> filterMap = new LinkedHashMap<>(); chainDefinition.addPathDefinitions(filterMap); return chainDefinition; } @Bean public static DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); defaultAdvisorAutoProxyCreator.setUsePrefix(true);
return defaultAdvisorAutoProxyCreator; } }
|
Controller相关示例代码:
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
| @PostMapping("/login") public R<String> userLogin(User userInWeb, @RequestParam(defaultValue = "false") boolean rememberMe){ Subject subject= SecurityUtils.getSubject(); AuthenticationToken token=new UsernamePasswordToken(userInWeb.getUsername(),userInWeb.getPassword(),rememberMe); try { subject.login(token); return R.success("登录成功"); } catch (UnknownAccountException e) { e.printStackTrace(); return R.error("用户不存在"); }catch (IncorrectCredentialsException e){ e.printStackTrace(); return R.error("密码错误"); }catch (LockedAccountException e){ e.printStackTrace(); return R.error("账户被封禁"); } catch (Exception e){ e.printStackTrace(); return R.error("程序异常"); } }
|