spring boot整合shiro后,部分注解(Cache缓存、Transaction事务等)失效的问题
2018-01-03 20:30
1566 查看
前言
整合有缓存、事务的spring boot项目一切正常。在该项目上整合shiro安全框架,发现部分类的缓存Cache不能正常使用。
然后发现该类的注解基本失效,包括事务Transaction注解。事务不能正常运行。
分析
注解失效的类,都是在shiro框架中(UserRealm)使用过@Autowire注入的类。基本确定是shiro框架与spring框架的BeanFactory有所冲突,导致注入shiro框架的类不能被spring正确初始化。
参考
stackoverflow网站上的一些文章认为,Shiro框架初始化比Spring框架的某些部件早,导致使用@Autowire注入Shiro框架的某些类不能被Spring正确初始化。文章链接:
https://stackoverflow.com/questions/21512791/spring-service-with-cacheable-methods-gets-initialized-without-cache-when-autowi
解决方法
在Shiro框架中注入Bean时,不使用@Autowire,使用ApplicationContextRegister.getBean()方法,手动注入bean。保证该方法只有在程序完全启动运行时,才被注入。使用@Autowire+@Lazy注解,设置注入到Shiro框架的Bean延时加载(即在第一次使用的时候加载)。
示例
1.Shiro中会出问题的代码public class MyAuthRealm extends AuthorizingRealm { //UserService中的注解可能会出现无效的情况 @Autowired private UserService userService; @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken utoken = (UsernamePasswordToken) token; String username = utoken.getUsername(); User user = userService.getUserByName(username); return new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getName()); } }
2.手动注入bean
public class UserRealm extends AuthorizingRealm { //该代码仅作手动注入bean的说明,前后略有省略代码 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) { Long userId = ShiroUtils.getUserId(); //这里手动注入MenuService MenuService menuService = ApplicationContextRegister.getBean(MenuService.class); Set<String> perms = menuService.listPerms(userId); SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); info.setStringPermissions(perms); return info; } }
3.延时加载(懒加载)
@Component(value = "myAuthRealm") public class MyAuthRealm extends AuthorizingRealm { //延时加载(懒加载) @Autowired @Lazy private UserService userService; @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken utoken = (UsernamePasswordToken) token; String username = utoken.getUsername(); User user = userService.getUserByName(username); return new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getName()); } }
相关文章推荐
- springboot+shiro+mybatis整合发现部分功能事务没有被spring管理
- springboot+shiro+mybatis整合发现部分功能事务没有被spring管理
- SpringBoot 整合redis实现缓存 记录@CachePut值为1
- Spring Cache扩展:注解失效时间+主动刷新缓存(二)
- SpringBoot 整合 Shiro 过程中遇到奇怪的类型转换问题
- spring boot整合shiro引用配置文件配置是出现的问题
- springboot整合缓存redis-cache
- 关于Spring的@Transactional注解失效以及事务无法回滚问题
- Springboot整合shiro 路径问题
- Spring Cache扩展:注解失效时间+主动刷新缓存
- 基于注解的spring缓存,轻松无侵入解决cache问题
- spring注解实现业务层事务管理,当业务层自调用时,事务失效问题解决
- 解决SpringBoot整合shiro之后无法加载静态资源文件问题
- spring事务注解失效问题
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用
- spring boot 整合shiro 配置类报错问题
- spring注解实现业务层事务管理,当业务层自调用时,事务失效问题解决
- spring整合hibernate配置注解事务的相关问题
- Spring 缓存注解(@Cacheable、@CachePut、@CacheEvict)使用