shiro安全认证之FilterChainDefinitionMap

最近在用shiro实现安全认证功能时发现一个问题

上图我们定义了一个LinkedHashMap,并往Map里面添加了两对键值对,然后执行

bean.setFilterChainDefinitionMap(filterMap)

 之后在将这两个键值对里面的值进行修改,但是没有执行上面的set操作,debug时发现,虽然没有执行set操作,但是FilterChainDefinitionMap里面的filterMap的键值对的值仍然发生了改变。

 甚至在后面再添加一个新的键值对,也能够进入FilterChainDefinitionMap里面,猜测这是Map的机制导致的。

同时注意一个点,在使用shiro安全认证时,定义的Map最好为LinkedHashMap,不要HashMap,因为如果是HashMap,提交到FilterChainDefinitionMap里面后顺序是乱的,有可能会使得我们需要拦截的路径出现了差错。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
非常感谢您的提问,我可以为您提供以下基于shiro安全认证springboot程序的代码示例: 1. 添加依赖 ```xml <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> <version>1.6.0</version> </dependency> ``` 2. 配置shiro ```java @Configuration public class ShiroConfig { // 配置shiro过滤器 @Bean public FilterRegistrationBean<DelegatingFilterProxy> delegatingFilterProxy(){ FilterRegistrationBean<DelegatingFilterProxy> filterRegistrationBean = new FilterRegistrationBean<>(); filterRegistrationBean.setFilter(new DelegatingFilterProxy("shiroFilterFactoryBean")); filterRegistrationBean.addUrlPatterns("/*"); return filterRegistrationBean; } // 配置shiro @Bean(name = "shiroFilterFactoryBean") public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){ ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); shiroFilterFactoryBean.setSecurityManager(securityManager); shiroFilterFactoryBean.setLoginUrl("/login"); shiroFilterFactoryBean.setUnauthorizedUrl("/error/403"); Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); filterChainDefinitionMap.put("/logout", "logout"); filterChainDefinitionMap.put("/static/**", "anon"); filterChainDefinitionMap.put("/login", "anon"); filterChainDefinitionMap.put("/error/**", "anon"); filterChainDefinitionMap.put("/**", "authc"); shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); return shiroFilterFactoryBean; } // 配置SecurityManager @Bean public SecurityManager securityManager(AuthRealm authRealm){ DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(authRealm); return securityManager; } // 配置Realm @Bean public AuthRealm authRealm(HashedCredentialsMatcher hashedCredentialsMatcher){ AuthRealm authRealm = new AuthRealm(); authRealm.setCacheManager(new MemoryConstrainedCacheManager()); authRealm.setCredentialsMatcher(hashedCredentialsMatcher); return authRealm; } // 配置凭证匹配器 @Bean public HashedCredentialsMatcher hashedCredentialsMatcher(){ HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher(); hashedCredentialsMatcher.setHashAlgorithmName("md5"); hashedCredentialsMatcher.setHashIterations(2); return hashedCredentialsMatcher; } } ``` 3. 实现自定义Realm ```java public class AuthRealm extends AuthenticatingRealm { @Autowired private UserService userService; // 验证用户身份 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken; String username = token.getUsername(); User user = userService.getUserByUsername(username); if(user == null){ throw new UnknownAccountException(); } return new SimpleAuthenticationInfo(user, user.getPassword(), ByteSource.Util.bytes(user.getSalt()), getName()); } // 授权 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); User user = (User) principalCollection.getPrimaryPrincipal(); authorizationInfo.addRole(user.getRole()); authorizationInfo.addStringPermission(user.getPermission()); return authorizationInfo; } } ``` 4. 实现登录接口 ```java @RestController public class LoginController { @PostMapping("/login") public Result login(String username, String password){ Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken(username, password); try{ subject.login(token); }catch (UnknownAccountException e){ return Result.fail("用户名不存在"); }catch (IncorrectCredentialsException e){ return Result.fail("密码错误"); } return Result.success(); } @GetMapping("/logout") public Result logout(){ Subject subject = SecurityUtils.getSubject(); subject.logout(); return Result.success(); } } ``` 以上便是基于shiro安全认证springboot程序的代码示例,希望能够对您有所帮助。如果您有其他问题,欢迎继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值