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

我查了很多资料,都查不到。请大神帮忙,谢谢!!!
补充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";
    }

}
阅读 970
1 个回答

你遇到的 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 的警告问题。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏