shiro整合spring的bug(org.apache.shiro.UnavailableSecurityManagerException)

报错如下

org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration.

通俗的对错误原因描述为:
你所请求的URL是不在Shiro所管辖范围的,而你又在你请求的这个URL后试图通过Shiro来获取Session,所以对Shiro来说“你不让我负责的事,为什么要跟我要结果”。

org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton.  This is an invalid application configuration.
    at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123)
    at cn.com.sand.cmp.client.interceptor.TransmitUserInfoFeighClientIntercepter.apply(TransmitUserInfoFeighClientIntercepter.java:28)
    at feign.SynchronousMethodHandler.targetRequest(SynchronousMethodHandler.java:163)
    at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:89)
    at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:77)
    at feign.ReflectiveFeign$FeignInvocationHandler.invoke(ReflectiveFeign.java:102)
    at com.sun.proxy.$Proxy136.getCfCompOrgMap(Unknown Source)
    at cn.com.sand.cmp.client.listener.WebListener.loadCacheData(WebListener.java:84)
    at cn.com.sand.cmp.client.listener.WebListener.run(WebListener.java:67)
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:792)
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230)
    at cn.com.sand.cmp.client.Bootstrap.main(Bootstrap.java:44)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

解决方法

加上以下代码

ThreadContext.bind(securityManager)

    @Bean
    public DefaultWebSecurityManager defaultWebSecurityManager() {
    
    
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        hashedCredentialsMatcher.setHashIterations(3);
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        defaultWebSecurityManager.setRealm(myRealm);
        
        ThreadContext.bind(defaultWebSecurityManager);

        return defaultWebSecurityManager;

    }

猜你喜欢

转载自blog.csdn.net/weixin_58286934/article/details/129132017