基于Spring框架的Shiro配置
2023-09-27 14:23:19 时间
一、在web.xml中添加shiro过滤器
二、在Spring的applicationContext.xml中添加shiro配置
1、添加shiroFilter定义
2、添加securityManager定义
3、添加realm定义
三、实现MyRealm:继承AuthorizingRealm,并重写认证授权方法
- <!-- Shiro filter-->
- <filter>
- <filter-name>shiroFilter</filter-name>
- <filter-class>
- org.springframework.web.filter.DelegatingFilterProxy
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>shiroFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
二、在Spring的applicationContext.xml中添加shiro配置
1、添加shiroFilter定义
- <!-- Shiro Filter -->
- <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
- <property name="securityManager" ref="securityManager" />
- <property name="loginUrl" value="/login" />
- <property name="successUrl" value="/user/list" />
- <property name="unauthorizedUrl" value="/login" />
- <property name="filterChainDefinitions">
- <value>
- /login = anon
- /user/** = authc
- /role/edit/* = perms[role:edit]
- /role/save = perms[role:edit]
- /role/list = perms[role:view]
- /** = authc
- </value>
- </property>
- </bean>
2、添加securityManager定义
- <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
- <property name="realm" ref="myRealm" />
- </bean>
3、添加realm定义
- <bean id=" myRealm" class="com...MyRealm" />
三、实现MyRealm:继承AuthorizingRealm,并重写认证授权方法
- public class MyRealm extends AuthorizingRealm{
- private AccountManager accountManager;
- public void setAccountManager(AccountManager accountManager) {
- this.accountManager = accountManager;
- }
- /**
- * 授权信息
- */
- protected AuthorizationInfo doGetAuthorizationInfo(
- PrincipalCollection principals) {
- String username=(String)principals.fromRealm(getName()).iterator().next();
- if( username != null ){
- User user = accountManager.get( username );
- if( user != null && user.getRoles() != null ){
- SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
- for( SecurityRole each: user.getRoles() ){
- info.addRole(each.getName());
- info.addStringPermissions(each.getPermissionsAsString());
- }
- return info;
- }
- }
- return null;
- }
- /**
- * 认证信息
- */
- protected AuthenticationInfo doGetAuthenticationInfo(
- AuthenticationToken authcToken ) throws AuthenticationException {
- UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
- String userName = token.getUsername();
- if( userName != null && !"".equals(userName) ){
- User user = accountManager.login(token.getUsername(),
- String.valueOf(token.getPassword()));
- if( user != null )
- return new SimpleAuthenticationInfo(
- user.getLoginName(),user.getPassword(), getName());
- }
- return null;
- }
- }
相关文章
- Spring Cloud 配置中心多环境配置bootstrap.yml
- Spring Boot 2.X系列教程:七天从无到有掌握Spring Boot-持续更新
- Spring源码解析-核心类之XmlBeanFactory 、DefaultListableBeanFactory
- Spring Boot 自动配置 源码分析
- 微服务轮子项目(51) -Spring Cloud性能调优
- spring boot
- 在Spring框架中实现属性配置动态刷新,不需要重启应用。
- Spring Boot 监听 Redis Key 失效事件实现定时任务
- Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制
- Spring任务执行和调度task:scheduler与task:executor的配置
- Spring Boot 优雅的配置拦截器方式
- spring batch(二):核心部分(1):配置Spring batch
- Spring Boot 2.4 对多环境配置的支持更改
- Spring Cloud Config采用Git存储时两种常用的配置策略
- 《精通Spring MVC 4》——1.8 嵌入式Servlet容器(Tomcat)的配置
- Spring Boot 2 实战:常用读取配置的方式
- mysql+spring+mybatis实现数据库读写分离[代码配置] .
- Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter标横线 已废弃
- java (Spring) 控制器中返回图片(验证码需求)
- Spring的Bean加载流程
- Spring Cloud Alibaba 微服务组件 Nacos 配置中心(六)
- Spring Boot内置Tomcat配置阿里云免费SSL证书
- 【spring框架】通过XML配置事务(Transaction)
- 【spring框架】通过注解配置bean
- Spring 中JCA CCI分析--转载
- 曹工说Spring Boot源码(23)-- ASM又立功了,Spring原来是这么递归获取注解的元注解的
- spring-security-oauth2 中优雅的扩展自定义(短信验证码)登录方式-系列1
- Java中核心注解的作用及其使用,了解Spring容器装载的过程和机制,自定义注解来实现自动配置项目依赖环境,包括mybatis、Dubbo、log4j、RabbitMQ、redis相关等自动配置
- spring boot 日志配置 默认的日志配置