|
@@ -1,13 +1,19 @@
|
|
|
package com.usky.config.shiro;
|
|
|
|
|
|
+import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
|
|
|
import org.apache.shiro.authc.credential.CredentialsMatcher;
|
|
|
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
|
|
|
+import org.apache.shiro.cache.CacheManager;
|
|
|
import org.apache.shiro.mgt.SecurityManager;
|
|
|
import org.apache.shiro.realm.Realm;
|
|
|
+import org.apache.shiro.session.mgt.SessionManager;
|
|
|
+import org.apache.shiro.session.mgt.eis.SessionDAO;
|
|
|
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
|
|
|
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
|
|
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
|
|
+import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
|
|
|
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
@@ -17,45 +23,45 @@ import java.util.LinkedHashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
- * @author laowo
|
|
|
- * @version v1.0
|
|
|
- * @date 2020/11/3 9:20
|
|
|
- * @description TODO
|
|
|
- **/
|
|
|
-
|
|
|
-
|
|
|
-//@Configuration
|
|
|
-//public class ShiroConfig {
|
|
|
-// //创建ShiroFilterFactoryBean
|
|
|
-// @Bean
|
|
|
-// public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
|
|
|
-// ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|
|
-// //设置安全管理器
|
|
|
-// shiroFilterFactoryBean.setSecurityManager(securityManager());
|
|
|
-// /*
|
|
|
-// anon 无需认证访问
|
|
|
-// authc 认证访问
|
|
|
-// perms 授权访问
|
|
|
-// role 角色授权访问
|
|
|
-// */
|
|
|
-// Map<String, String> hashMap = new LinkedHashMap<>();
|
|
|
-// shiroFilterFactoryBean.setLoginUrl("/");
|
|
|
-// hashMap.put("/login/toLogin", "anon");
|
|
|
-// hashMap.put("/", "anon");
|
|
|
-// //swagger2
|
|
|
-// hashMap.put("/swagger-ui.html", "anon");
|
|
|
-// hashMap.put("/swagger-resources", "anon");
|
|
|
-// hashMap.put("/swagger-resources/configuration/security", "anon");
|
|
|
-// hashMap.put("/swagger-resources/configuration/ui", "anon");
|
|
|
-// hashMap.put("/v2/api-docs", "anon");
|
|
|
-// hashMap.put("/webjars/springfox-swagger-ui/**", "anon");
|
|
|
-// // hashMap.put("/user/queryUserAll", "anon");
|
|
|
-// // hashMap.put("/*", "authc");
|
|
|
-// hashMap.put("/login/loginOut", "logout");
|
|
|
-// shiroFilterFactoryBean.setFilterChainDefinitionMap(hashMap);
|
|
|
-// return shiroFilterFactoryBean;
|
|
|
-// }
|
|
|
-// //创建DefaultWebSecurityManager
|
|
|
+ *
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+public class ShiroConfig {
|
|
|
+ //shiroFilter
|
|
|
+ @Bean
|
|
|
+ public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager securityManager) {
|
|
|
+ ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
|
|
+ shiroFilterFactoryBean.setSecurityManager(securityManager);
|
|
|
+ shiroFilterFactoryBean.setLoginUrl("/page/toLogin");
|
|
|
+
|
|
|
+ //控制 访问xx资源 需要xx权限
|
|
|
+ Map filterChainMap = new LinkedHashMap<String,String>();
|
|
|
+ filterChainMap.put("/sys/login","anon"); //访问登录页面 直接放行
|
|
|
+ filterChainMap.put("/","anon"); //访问登录页面 直接放行
|
|
|
+ filterChainMap.put("/user/all","perms[user:select]"); //查询所有用户 需要认证(登录)
|
|
|
+
|
|
|
+ //当用户查看仓库列表时,需要有仓库权限
|
|
|
+ filterChainMap.put("/storage/all","perms[storage:select]");
|
|
|
+ //当用户删除用户时,需要有超级管理员角色
|
|
|
+// filterChainMap.put("/user/del/*","roles[role_superman]");
|
|
|
+
|
|
|
+ filterChainMap.put("/backend/logout","logout");
|
|
|
+
|
|
|
+ shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainMap);
|
|
|
+ return shiroFilterFactoryBean;
|
|
|
+ }
|
|
|
+
|
|
|
+ //安全管理器
|
|
|
+ @Bean
|
|
|
+ @Lazy
|
|
|
+ public SecurityManager securityManager() {
|
|
|
+ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
|
|
+ securityManager.setSessionManager(sessionManager());
|
|
|
+ securityManager.setRealm(myRealm());
|
|
|
+ return securityManager;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
// @Bean("SecurityManager")
|
|
|
// public SecurityManager securityManager() {
|
|
|
// DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
|
|
@@ -65,36 +71,83 @@ import java.util.Map;
|
|
|
// defaultWebSecurityManager.setRealm(getRealm());
|
|
|
// return defaultWebSecurityManager;
|
|
|
// }
|
|
|
-// @Bean(name = "Realm")
|
|
|
-// @Lazy
|
|
|
-// public Realm getRealm() {
|
|
|
-// MyRealm myRealm = new MyRealm();
|
|
|
-// //设置密码匹配器
|
|
|
-// myRealm.setCredentialsMatcher(credentialsMatcher());
|
|
|
-// return myRealm;
|
|
|
-// }
|
|
|
-// //创建密码匹配器
|
|
|
-// @Bean
|
|
|
-// public CredentialsMatcher credentialsMatcher() {
|
|
|
-// HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
|
|
|
-// hashedCredentialsMatcher.setHashAlgorithmName("md5");
|
|
|
-// hashedCredentialsMatcher.setHashIterations(1);
|
|
|
-// return hashedCredentialsMatcher;
|
|
|
-// }
|
|
|
-// /**
|
|
|
-// * 注解支持:
|
|
|
-// */
|
|
|
-// @Bean
|
|
|
-// @ConditionalOnMissingBean
|
|
|
-// public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
|
|
|
-// DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
|
|
|
-// defaultAAP.setProxyTargetClass(true);
|
|
|
-// return defaultAAP;
|
|
|
-// }
|
|
|
-// @Bean
|
|
|
-// public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
|
|
|
-// AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
|
|
|
-// authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
|
|
|
-// return authorizationAttributeSourceAdvisor;
|
|
|
-// }
|
|
|
-//}
|
|
|
+
|
|
|
+ //realm
|
|
|
+ @Bean
|
|
|
+ public Realm myRealm(){
|
|
|
+ MyRealm myRealm = new MyRealm();
|
|
|
+ //告诉realm密码匹配方式
|
|
|
+ myRealm.setCredentialsMatcher(credentialsMatcher());
|
|
|
+ myRealm.setAuthorizationCacheName("perms");
|
|
|
+ myRealm.setAuthorizationCachingEnabled(true);
|
|
|
+ myRealm.setAuthenticationCachingEnabled(false);
|
|
|
+ //设置缓存管理器
|
|
|
+ myRealm.setCacheManager(MycacheManager());
|
|
|
+ return myRealm;
|
|
|
+ }
|
|
|
+
|
|
|
+ //缓存管理
|
|
|
+ @Bean
|
|
|
+ public CacheManager MycacheManager(){
|
|
|
+ MyRedisCacheManager cacheManager = new MyRedisCacheManager();
|
|
|
+ return cacheManager;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public CredentialsMatcher credentialsMatcher(){
|
|
|
+ HashedCredentialsMatcher hashedMatcher = new HashedCredentialsMatcher();
|
|
|
+ hashedMatcher.setHashAlgorithmName("md5");
|
|
|
+// hashedMatcher.setHashIterations(1);
|
|
|
+ return hashedMatcher;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注解支持
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ @ConditionalOnMissingBean
|
|
|
+ public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
|
|
|
+ DefaultAdvisorAutoProxyCreator defaultAAP = new DefaultAdvisorAutoProxyCreator();
|
|
|
+ defaultAAP.setProxyTargetClass(true);
|
|
|
+ return defaultAAP;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
|
|
|
+ AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
|
|
|
+ authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
|
|
|
+ return authorizationAttributeSourceAdvisor;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public ShiroDialect shiroDialect(){
|
|
|
+ return new ShiroDialect();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 会话管理器
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public SessionManager sessionManager() {
|
|
|
+ DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
|
|
|
+ sessionManager.setSessionDAO(redisSessionDAO());
|
|
|
+
|
|
|
+ //设置会话过期时间
|
|
|
+ sessionManager.setGlobalSessionTimeout(3*60*1000); //默认半小时
|
|
|
+ sessionManager.setDeleteInvalidSessions(true); //默认自定调用SessionDAO的delete方法删除会话
|
|
|
+ //设置会话定时检查
|
|
|
+ // sessionManager.setSessionValidationInterval(180000); //默认一小时
|
|
|
+ // sessionManager.setSessionValidationSchedulerEnabled(true);
|
|
|
+ return sessionManager;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public SessionDAO redisSessionDAO(){
|
|
|
+ ShiroRedisSessionDao redisDAO = new ShiroRedisSessionDao();
|
|
|
+ return redisDAO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|