|
@@ -1,265 +0,0 @@
|
|
|
-package com.usky.config.shiro;
|
|
|
-
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.apache.shiro.authc.credential.CredentialsMatcher;
|
|
|
-import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
|
|
|
-import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
|
|
|
-import org.apache.shiro.mgt.DefaultSubjectDAO;
|
|
|
-import org.apache.shiro.mgt.SecurityManager;
|
|
|
-import org.apache.shiro.spring.LifecycleBeanPostProcessor;
|
|
|
-import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
|
|
|
-import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
|
|
-import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
|
|
-import org.crazycake.shiro.IRedisManager;
|
|
|
-import org.crazycake.shiro.RedisCacheManager;
|
|
|
-import org.crazycake.shiro.RedisManager;
|
|
|
-import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.context.annotation.DependsOn;
|
|
|
-import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
|
|
-import org.springframework.util.StringUtils;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-import java.util.LinkedHashMap;
|
|
|
-import java.util.Map;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author laowo
|
|
|
- */
|
|
|
-@Configuration
|
|
|
-@Slf4j
|
|
|
-public class ShiroConfig {
|
|
|
-// @Resource
|
|
|
-// private LettuceConnectionFactory lettuceConnectionFactory;
|
|
|
-// @Bean
|
|
|
-// public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager) {
|
|
|
-// ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|
|
-// shiroFilterFactoryBean.setSecurityManager(securityManager);
|
|
|
-// //登录
|
|
|
-// shiroFilterFactoryBean.setLoginUrl("/sys/login");
|
|
|
-// //控制 访问xx资源 需要xx权限
|
|
|
-// Map<String, String> filterChainMap = new LinkedHashMap<>();
|
|
|
-// //swagger接口权限 开放
|
|
|
-// filterChainMap.put("/doc.html", "anon");
|
|
|
-// filterChainMap.put("/webjars/**/**", "anon");
|
|
|
-// filterChainMap.put("/swagger-ui.html", "anon");
|
|
|
-// filterChainMap.put("/webjars/**", "anon");
|
|
|
-// filterChainMap.put("/v2/**", "anon");
|
|
|
-// filterChainMap.put("/swagger-resources/**", "anon");
|
|
|
-// //退出
|
|
|
-// filterChainMap.put("/logout", "logout");
|
|
|
-// filterChainMap.put("/static/**", "anon");
|
|
|
-// filterChainMap.put("/templates/**", "anon");
|
|
|
-// //swagger接口权限 开放
|
|
|
-// filterChainMap.put("/**", "authc");
|
|
|
-// shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);
|
|
|
-// return shiroFilterFactoryBean;
|
|
|
-// }
|
|
|
-// @Bean(name = "securityManager")
|
|
|
-// public SecurityManager securityManager(@Qualifier("myRealm") MyRealm myRealm) {
|
|
|
-// DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
|
|
-// // 设置realm.
|
|
|
-// securityManager.setRealm(myRealm);
|
|
|
-// securityManager.setCacheManager(redisCacheManager());
|
|
|
-// // 自定义session管理 使用redis
|
|
|
-// securityManager.setSessionManager(sessionManager());
|
|
|
-// return securityManager;
|
|
|
-// }
|
|
|
-// @Bean(name = "myRealm")
|
|
|
-// public MyRealm shiroRealm(@Qualifier("credentialsMatcher") CredentialsMatcher credentialsMatcher) {
|
|
|
-// MyRealm shiroRealm = new MyRealm();
|
|
|
-// shiroRealm.setCredentialsMatcher(credentialsMatcher);
|
|
|
-// return shiroRealm;
|
|
|
-// }
|
|
|
-// /**
|
|
|
-// * 凭证匹配器
|
|
|
-// * @return
|
|
|
-// */
|
|
|
-// @Bean
|
|
|
-// public CredentialsMatcher credentialsMatcher() {
|
|
|
-// HashedCredentialsMatcher hashedMatcher = new HashedCredentialsMatcher();
|
|
|
-// hashedMatcher.setHashAlgorithmName("md5");
|
|
|
-//// hashedMatcher.setHashIterations(1);
|
|
|
-// return hashedMatcher;
|
|
|
-// }
|
|
|
-// @Bean
|
|
|
-// public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) {
|
|
|
-// AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
|
|
|
-// authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
|
|
|
-// return authorizationAttributeSourceAdvisor;
|
|
|
-// }
|
|
|
-//
|
|
|
-// /**
|
|
|
-// * 配置shiro redisManager
|
|
|
-// * @return
|
|
|
-// */
|
|
|
-// public RedisManager redisManager() {
|
|
|
-// RedisManager redisManager = new RedisManager();
|
|
|
-// redisManager.setHost("47.111.81.118:6379");
|
|
|
-// redisManager.setDatabase(1);
|
|
|
-// redisManager.setTimeout(0);
|
|
|
-// redisManager.setPassword(redisProperties.getPassword());
|
|
|
-// return redisManager;
|
|
|
-// }
|
|
|
-// /**
|
|
|
-// * cacheManager 缓存 redis实现
|
|
|
-// * 使用的是shiro-redis开源插件
|
|
|
-// * @return
|
|
|
-// */
|
|
|
-// @Bean
|
|
|
-// public RedisCacheManager redisCacheManager() {
|
|
|
-// RedisCacheManager redisCacheManager = new RedisCacheManager();
|
|
|
-// redisCacheManager.setRedisManager(redisManager());
|
|
|
-// redisCacheManager.setPrincipalIdFieldName("userId");
|
|
|
-// return redisCacheManager;
|
|
|
-// }
|
|
|
-// /**
|
|
|
-// * RedisSessionDAO shiro sessionDao层的实现 通过redis
|
|
|
-// * 使用的是shiro-redis开源插件
|
|
|
-// */
|
|
|
-//// @Bean
|
|
|
-// public RedisSessionDAO redisSessionDAO() {
|
|
|
-// RedisSessionDAO redisSessionDAO = new RedisSessionDAO();
|
|
|
-// redisSessionDAO.setRedisManager(redisManager());
|
|
|
-// return redisSessionDAO;
|
|
|
-// }
|
|
|
-// /**
|
|
|
-// * shiro session的管理
|
|
|
-// */
|
|
|
-// @Bean
|
|
|
-// public DefaultWebSessionManager sessionManager() {
|
|
|
-// DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
|
|
|
-// sessionManager.setGlobalSessionTimeout(redisProperties.getExpire());
|
|
|
-// sessionManager.setSessionDAO(redisSessionDAO());
|
|
|
-// return sessionManager;
|
|
|
-// }
|
|
|
-
|
|
|
- @Resource
|
|
|
- LettuceConnectionFactory lettuceConnectionFactory;
|
|
|
-
|
|
|
- /**
|
|
|
- * Filter Chain定义说明
|
|
|
- * <p>
|
|
|
- * 1、一个URL可以配置多个Filter,使用逗号分隔
|
|
|
- * 2、当设置多个过滤器时,全部验证通过,才视为通过
|
|
|
- * 3、部分过滤器可指定参数,如perms,roles
|
|
|
- */
|
|
|
- @Bean("shiroFilter")
|
|
|
- public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
|
|
|
- ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|
|
- shiroFilterFactoryBean.setSecurityManager(securityManager);
|
|
|
- // 拦截器
|
|
|
- // 配置不会被拦截的链接 顺序判断
|
|
|
- //控制 访问xx资源 需要xx权限
|
|
|
- Map<String, String> filterChainMap = new LinkedHashMap<>();
|
|
|
- //swagger接口权限 开放
|
|
|
- filterChainMap.put("/doc.html", "anon");
|
|
|
- filterChainMap.put("/v2/**", "anon");
|
|
|
- filterChainMap.put("/webjars/**/**", "anon");
|
|
|
- filterChainMap.put("/swagger-ui.html", "anon");
|
|
|
- filterChainMap.put("/webjars/**", "anon");
|
|
|
- filterChainMap.put("/swagger-resources/**", "anon");
|
|
|
- filterChainMap.put("/druid/**", "anon");
|
|
|
- filterChainMap.put("/swagger**/**", "anon");
|
|
|
- //登录
|
|
|
- shiroFilterFactoryBean.setLoginUrl("/sys/login");
|
|
|
- //退出
|
|
|
- filterChainMap.put("/logout", "logout");
|
|
|
- filterChainMap.put("/static/**", "anon");
|
|
|
- filterChainMap.put("/templates/**", "anon");
|
|
|
- //swagger接口权限 开放
|
|
|
- filterChainMap.put("/**", "authc");
|
|
|
- shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);
|
|
|
- return shiroFilterFactoryBean;
|
|
|
- }
|
|
|
-
|
|
|
- @Bean("securityManager")
|
|
|
- public DefaultWebSecurityManager securityManager(MyRealm myRealm) {
|
|
|
- DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
|
|
- myRealm.setCredentialsMatcher(credentialsMatcher());
|
|
|
- securityManager.setRealm(myRealm);
|
|
|
- DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
|
|
|
- DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
|
|
|
- defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
|
|
|
- subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
|
|
|
- securityManager.setSubjectDAO(subjectDAO);
|
|
|
- //自定义缓存实现,使用redis
|
|
|
- securityManager.setCacheManager(redisCacheManager());
|
|
|
- return securityManager;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 凭证匹配器
|
|
|
- * @return
|
|
|
- */
|
|
|
- @Bean
|
|
|
- public CredentialsMatcher credentialsMatcher() {
|
|
|
- HashedCredentialsMatcher hashedMatcher = new HashedCredentialsMatcher();
|
|
|
- hashedMatcher.setHashAlgorithmName("md5");
|
|
|
-// hashedMatcher.setHashIterations(1);
|
|
|
- return hashedMatcher;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- /**
|
|
|
- * 下面的代码是添加注解支持
|
|
|
- */
|
|
|
- @Bean
|
|
|
- @DependsOn("lifecycleBeanPostProcessor")
|
|
|
- public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
|
|
|
- DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
|
|
|
- defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
|
|
|
- defaultAdvisorAutoProxyCreator.setUsePrefix(true);
|
|
|
- defaultAdvisorAutoProxyCreator.setAdvisorBeanNamePrefix("_no_advisor");
|
|
|
- return defaultAdvisorAutoProxyCreator;
|
|
|
- }
|
|
|
- @Bean
|
|
|
- public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
|
|
|
- return new LifecycleBeanPostProcessor();
|
|
|
- }
|
|
|
- @Bean
|
|
|
- public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
|
|
|
- AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
|
|
|
- advisor.setSecurityManager(securityManager);
|
|
|
- return advisor;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * cacheManager 缓存 redis实现
|
|
|
- * 使用的是shiro-redis开源插件
|
|
|
- *
|
|
|
- */
|
|
|
- public RedisCacheManager redisCacheManager() {
|
|
|
- log.info("===============(1)创建缓存管理器RedisCacheManager");
|
|
|
- RedisCacheManager redisCacheManager = new RedisCacheManager();
|
|
|
- redisCacheManager.setRedisManager(redisManager());
|
|
|
- //redis中针对不同用户缓存(此处的id需要对应user实体中的id字段,用于唯一标识)
|
|
|
- // redisCacheManager.setPrincipalIdFieldName("id");
|
|
|
- redisCacheManager.setPrincipalIdFieldName("userId");
|
|
|
- //用户权限信息缓存时间
|
|
|
- redisCacheManager.setExpire(200000);
|
|
|
- return redisCacheManager;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 配置shiro redisManager
|
|
|
- * 使用的是shiro-redis开源插件
|
|
|
- *
|
|
|
- */
|
|
|
- @Bean
|
|
|
- public IRedisManager redisManager() {
|
|
|
- log.info("===============(2)创建RedisManager,连接Redis..");
|
|
|
- IRedisManager manager;
|
|
|
- RedisManager redisManager = new RedisManager();
|
|
|
- redisManager.setHost(lettuceConnectionFactory.getHostName());
|
|
|
- redisManager.setPort(lettuceConnectionFactory.getPort());
|
|
|
- redisManager.setDatabase(1);
|
|
|
- redisManager.setTimeout(0);
|
|
|
- if (!StringUtils.isEmpty(lettuceConnectionFactory.getPassword())) {
|
|
|
- redisManager.setPassword(lettuceConnectionFactory.getPassword());
|
|
|
- }
|
|
|
- manager = redisManager;
|
|
|
- return manager;
|
|
|
- }
|
|
|
-}
|