import org.apache.shiro.cache.MemoryConstrainedCacheManager; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.LinkedHashMap; /** * * @author Administrator * @create 2017/12/2 * @since 1.0.0 */ @Configuration public class ShiroConfiguration { @Bean("shiroFilter") public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manager){ System.out.println("shiroFilter"); ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); bean.setSecurityManager(manager); //配置shiro默认登录界面地址,前后端分离中登录界面跳转应由前端路由控制,后台仅返回json数据 bean.setLoginUrl("/login/unauth"); // 登录成功后要跳转的链接 //bean.setSuccessUrl("/user/index"); //未授权界面 //bean.setUnauthorizedUrl("/user/unauthorized"); //请求拦截 key-访问请路径 value-使用的拦截方式 LinkedHashMap<String, String> filterChainDefinittionMap = new LinkedHashMap<>(); //配置退出 filterChainDefinittionMap.put("/login/logout", "logout"); // 静态资源 filterChainDefinittionMap.put("/WEB-INF/views/**", "anon"); //filterChainDefinittionMap.put("/index","authc");//需要通过验证 filterChainDefinittionMap.put("/login/loginUser","anon");//不需要验证 // filterChainDefinittionMap.put("/user/admin","roles[管理员]");//只允许admin用户 //filterChainDefinittionMap.put("/add","perms[add]");//只允许权限又add的用户 //filterChainDefinittionMap.put("/**","user");//需要验证--已经登录的用户 //filterChainDefinittionMap.put("/**","authc"); bean.setFilterChainDefinitionMap(filterChainDefinittionMap); return bean; } //注入缓存管理器 @Bean("securityManager") public SecurityManager securityManager(@Qualifier("authRealm") AuthRealm authRealm){ System.out.println("securityManager"); DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); manager.setRealm(authRealm); return manager; } @Bean("authRealm") public AuthRealm authRealm(@Qualifier("credentialMatcher") CredentialMatcher matcher){ System.out.println("authRealm"); AuthRealm authRealm = new AuthRealm(); authRealm.setCacheManager(new MemoryConstrainedCacheManager());//启用缓存 authRealm.setCredentialsMatcher(matcher); return authRealm; } @Bean("credentialMatcher") public CredentialMatcher credentialMatcher(){ System.out.println("Credentialmatcher"); return new CredentialMatcher(); } //开启shiro aop注解支持 @Bean public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager){ System.out.println("authorizationAttributeSourceAdvisor"); AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor(); advisor.setSecurityManager(securityManager); return advisor; } @Bean public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){ System.out.println("defaultAdvisorAutoProxyCreator"); DefaultAdvisorAutoProxyCreator creater = new DefaultAdvisorAutoProxyCreator(); creater.setProxyTargetClass(true); return creater; } }
springboot1.5 shiro配置
最新推荐文章于 2022-08-24 02:03:03 发布