.java
package com.example.demo2.config;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 sssr* @version 1.0* @Description:* @date 2019/2/17*/@Configurationpublic class ShiroConfig {@Bean("shiroFilter")public ShiroFilterFactoryBean shiroFilter(@Qualifier("securityManager") SecurityManager manager) {ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();bean.setSecurityManager(manager);//登录接口bean.setLoginUrl("/user/login");//登录成功跳转页面bean.setSuccessUrl("/user/index");//没有权限跳转的页面bean.setUnauthorizedUrl("/user/unauthorized");LinkedHashMap filterChainDefinitionMap = new LinkedHashMap<>();filterChainDefinitionMap.put("/user/index", "authc");filterChainDefinitionMap.put("/user/login", "anon");filterChainDefinitionMap.put("/user/loginUser", "anon");filterChainDefinitionMap.put("/user/admin", "roles[admin]");filterChainDefinitionMap.put("/user/edit", "perms[edit]");filterChainDefinitionMap.put("/druid/**", "anon");filterChainDefinitionMap.put("/**", "user");bean.setFilterChainDefinitionMap(filterChainDefinitionMap);return bean;}@Bean("securityManager")public SecurityManager securityManager(@Qualifier("authRealm") AuthRealm authRealm) {DefaultWebSecurityManager manager = new DefaultWebSecurityManager();manager.setRealm(authRealm);return manager;}@Bean("authRealm")public AuthRealm authRealm(@Qualifier("credentialMatcher") CredentialMatcher matcher) {AuthRealm authRealm = new AuthRealm();authRealm.setCacheManager(new MemoryConstrainedCacheManager());authRealm.setCredentialsMatcher(matcher);return authRealm;}@Bean("credentialMatcher")public CredentialMatcher credentialMatcher() {return new CredentialMatcher();}@Beanpublic AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") SecurityManager securityManager) {AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();advisor.setSecurityManager(securityManager);return advisor;}@Beanpublic DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();creator.setProxyTargetClass(true);return creator;}}
九、运行效果
1.打开地址会自动跳转到登录页面
文章插图
2.登录成功
文章插图
3.访问user/edit接口,没有权限时跳转到没有权限页面
文章插图
4.访问user/index,可以正常访问,因为没有做权限控制
文章插图
5.退出登录
【springBoot2.0 配置shiro实现权限管理】
文章插图
- vs2019项目部署到阿里云服务器与iis配置
- centos7.4系统的虚拟机网络配置教程
- lv3 嵌入式开发-1linux介绍及环境配置
- 掘金领航者全自动付费进群系统代理端域名配置教程
- servlet配置
- SVN版本控制器的安装和配置以及一些技巧svn开机自启动
- 阿里云服务器配置个人用户怎么选?CPU内存带宽系统盘?
- 华为交换机——策略路由配置实战
- 【Linux下进程的CPU配置和线程绑定】
- 三 【Audio】配置文档audio_policy_conf.xml