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

The error is as follows

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.

The popular description of the cause of the error is:
the URL you requested is not under the jurisdiction of Shiro, and you are trying to get the Session through Shiro after the URL you requested, so for Shiro, "you don't make me responsible Why do you want the result from me?"

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)

Solution

add the following code

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;

    }

Guess you like

Origin blog.csdn.net/weixin_58286934/article/details/129132017