当前位置: 首页 > 知识库问答 >
问题:

springboot - Shiro整合Springboot报错,提示is not eligible for getting processed by all BeanPostProcessors ?

拓拔弘扬
2024-09-10

我查了很多资料,都查不到。请大神帮忙,谢谢!!!
补充1:对了,教程的版本是jdk8,我是17,springboot版本教程是2,我是3.可能是这个原因。
报错:

2024-09-09T12:24:45.597+08:00  WARN 73792 --- [shiro-spring-boot-demo] [           main] trationDelegate$BeanPostProcessorChecker : Bean 'userRealm' of type [com.example.shirospringbootdemo.UserRealm] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected into a currently created BeanPostProcessor []? Check the corresponding BeanPostProcessor declaration and its dependencies.
2024-09-09T12:24:45.601+08:00  WARN 73792 --- [shiro-spring-boot-demo] [           main] trationDelegate$BeanPostProcessorChecker : Bean 'shiroConfig' of type [com.example.shirospringbootdemo.ShiroConfig$$SpringCGLIB$$0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected into a currently created BeanPostProcessor []? Check the corresponding BeanPostProcessor declaration and its dependencies.
2024-09-09T12:24:45.647+08:00  WARN 73792 --- [shiro-spring-boot-demo] [           main] trationDelegate$BeanPostProcessorChecker : Bean 'defaultWebSecurityManager' of type [org.apache.shiro.web.mgt.DefaultWebSecurityManager] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying). Is this bean getting eagerly injected into a currently created BeanPostProcessor []? Check the corresponding BeanPostProcessor declaration and its dependencies.

UserRealm:

@Component
public class UserRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }
}

ShiroConfig:

@Configuration
public class ShiroConfig {

    private UserRealm userRealm;

    @Autowired
    public ShiroConfig(UserRealm userRealm){
        this.userRealm=userRealm;
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager manager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(manager);
        Map<String, String> filterMap = new LinkedHashMap();
        filterMap.put("/add", "authc");
        filterMap.put("/query", "authc");
        filterMap.put("/**", "anon");
        bean.setFilterChainDefinitionMap(filterMap);
        return bean;
    }


}

MyController:

@Controller
public class MyController {

    @RequestMapping({"/","/index","/index.html"})
    public ModelAndView toIndex(ModelAndView modelAndView){
        modelAndView.addObject("msg","Hello");
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("/add")
    public String toAdd(){
        return "add";
    }

    @RequestMapping("/query")
    public String toQuery(){
        return "query";
    }

}

共有1个答案

益清野
2024-09-10

你遇到的 BeanPostProcessor 的问题可能与 UserRealm 的注入顺序有关。Spring 在处理 BeanPostProcessor 时,要求所有 bean 的生命周期在正确的顺序中被处理。如果某个 bean 在 BeanPostProcessor 完成准备之前被注入或初始化,则该 bean 就可能无法被完全代理或处理。

解决方案建议

1.使用懒加载(Lazy Initialization): 可以尝试让 UserRealm 延迟加载,以避免它在 Spring 还没有完全初始化其他 bean 时被急切注入。

在 UserRealm 类上加上 @Lazy 注解,使其懒加载:

@Component
@Lazy
public class UserRealm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        return null;
    }
}

2.依赖注入改为通过构造函数: 可以将 ShiroConfig 类中的 UserRealm 改为通过构造函数注入,并同时使用 @Bean 的方式声明。
@Configuration

public class ShiroConfig {

    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager manager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(manager);
        Map<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/add", "authc");
        filterMap.put("/query", "authc");
        filterMap.put("/**", "anon");
        bean.setFilterChainDefinitionMap(filterMap);
        return bean;
    }
}

3.使用 @DependsOn 注解: 如果你确信有些 bean 必须在 BeanPostProcessor 之后进行初始化,可以使用 @DependsOn 注解来确保初始化顺序。例如,你可以在 ShiroConfig 中对 defaultWebSecurityManager 进行依赖声明。

@Bean
@DependsOn("userRealm")
public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm) {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    securityManager.setRealm(userRealm);
    return securityManager;
}

这样可以避免 userRealm 过早初始化而导致 BeanPostProcessor 无法正常处理的问题。尝试以上解决方案,看是否能够解决 BeanPostProcessor 的警告问题。

 类似资料:
  • 我的项目是SpringBoot(版本2.0.9.RELEASE)整合spring-boot-starter-quartz。代码完全来自于这篇博客代码来源,我在启动项目后,报错: 这个问题网上也有相关的问题,但是他们的答案我一直尝试不成功。 大部分回答都是AdaptableJobFactory的里面用的反射有问题,说要重写AdaptableJobFactory(https://blog.csdn.n

  • 本文向大家介绍SpringBoot整合Shiro的代码详解,包括了SpringBoot整合Shiro的代码详解的使用技巧和注意事项,需要的朋友参考一下 shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能.   而springboot作为一个开源框架,必然提供了和shiro整合的功能!接下来就用springbo

  • 本文向大家介绍Springboot整合Shiro的代码实例,包括了Springboot整合Shiro的代码实例的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了Springboot整合Shiro的代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1、导入依赖 2、创建ShiroRealm.java文件 (这里按照需求,只做登录认证

  • 本文向大家介绍Springboot整合Gson报错问题解决过程,包括了Springboot整合Gson报错问题解决过程的使用技巧和注意事项,需要的朋友参考一下 在Springboot 中依赖Gson,项目启动时报错: 解决办法: 将pom中依赖的Gson版本更换为2.6以上即可 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍SpringBoot中整合Shiro实现权限管理的示例代码,包括了SpringBoot中整合Shiro实现权限管理的示例代码的使用技巧和注意事项,需要的朋友参考一下 之前在 SSM 项目中使用过 shiro,发现 shiro 的权限管理做的真不错,但是在 SSM 项目中的配置太繁杂了,于是这次在 SpringBoot 中使用了 shiro,下面一起看看吧 一、简介 Apache Sh

  • 本文向大家介绍Springboot整合Shiro之加盐MD5加密的方法,包括了Springboot整合Shiro之加盐MD5加密的方法的使用技巧和注意事项,需要的朋友参考一下 1.自定义realm,在Shiro的配置类中加入以下bean 2.重写方法 需要注意的是SimpleAuthenticationInfo 类,我们需要把数据交给他,格式为(用户,用户密码,盐,当前Realm的类名) 3.你还

  • 本文向大家介绍SpringBoot整合Shiro实现登录认证的方法,包括了SpringBoot整合Shiro实现登录认证的方法的使用技巧和注意事项,需要的朋友参考一下 安全无处不在,趁着放假读了一下 Shiro 文档,并记录一下 Shiro 整合 Spring Boot 在数据库中根据角色控制访问权限 简介 Apache Shiro是一个功能强大、灵活的,开源的安全框架。它可以干净利落地处理身份验

  • 本文向大家介绍Springboot 整合shiro实现权限控制的方法,包括了Springboot 整合shiro实现权限控制的方法的使用技巧和注意事项,需要的朋友参考一下 Author:jeffrey Date:2019-04-08 一、开发环境: 1、mysql - 5.7 2、navicat(mysql客户端管理工具) 3、idea 2017.2 4、jdk8 5、tomcat 8.5 6、s