Home > Back-end >  Shiro and @ Scheduled an error
Shiro and @ Scheduled an error

Time:05-15

 @ Scheduled (cron="0 1 * * * */?" )//once every 1 minute 
Public void the work () {
if (null! .=ShiroUtils getLoginName ()) {
RedisUtil. Set (ShiroUtils. GetLoginName (), ShiroUtils. GetSessionId (), 60).
}

}



The fault is
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 at org, apache shiro. The SecurityUtils. GetSecurityManager (123). The SecurityUtils Java:
The at org, apache shiro. Subject. The subject $Builder. & lt; Init> (626) Subject. Java:
The at org, apache shiro. The SecurityUtils. GetSubject (56) the SecurityUtils. Java:
At com. Loroi. Framework. Util. ShiroUtils. GetSubject (25) ShiroUtils. Java:
At com. Loroi. Framework. Util. ShiroUtils. GetSysUser (ShiroUtils. Java: 41)
At com. Loroi. Framework. Util. ShiroUtils. GetLoginName (ShiroUtils. Java: 74)
At com. Loroi. Web. Controller. System. SysLoginController. Work (107) SysLoginController. Java:
At sun. Reflect. NativeMethodAccessorImpl. Invoke0 (Native Method)
At sun. Reflect. NativeMethodAccessorImpl. Invoke (NativeMethodAccessorImpl. Java: 62)
At sun. Reflect. DelegatingMethodAccessorImpl. Invoke (43) DelegatingMethodAccessorImpl. Java:
The at Java. Lang. Reflect. Method. Invoke (498) Method. The Java:
How can I solve why can't get the user

CodePudding user response:

Use shiro, but I guess shiro get user information only if the user request execution code will have the session or other information, you this code is the system automatically run, and not the user request, so the estimated by shiro is not taken

CodePudding user response:

Well as if a little reason but still don't know how to solve the

CodePudding user response:

Look from your exception, the SecurityUtils SecurityManager didn't find the core components,

SecurityManager it is responsible for safety management, all of the subject is essentially a singleton application, generally defined shiro configuration will define the Bean of the interface implementation. Can be shiro provided or custom

@ Configuration
Public class ShiroAutoConfiguration {
@ Bean
AuthRealm AuthRealm () {
Return new AuthRealm ();
}

@ Bean
DefaultWebSecurityManager securityManager () {
DefaultWebSecurityManager manager=new DefaultWebSecurityManager ();
Manager. SetRealm (authRealm ());
Return the manager;
}
.
}

CodePudding user response:

You check your shiro configuration, are you missing

CodePudding user response:

In other places can be obtained only this time can't get userId

CodePudding user response:

I checked the source:

SecurityUtils# getSecurityManager method is through ThreadContext getSecurityManager (),
ThreadContext is through new InheritableThreadLocalMap< thread variables; Map SecurityManager () to save each thread, namely, the login operation when put into the thread variables, with threads access directly to your thread SecurityManager, so when you through Spring scheduling to deal with is the newly created thread, the thread is no SecurityManager, so an error



//some source:
Public static SecurityManager getSecurityManager () {
Return (SecurityManager) get (SECURITY_MANAGER_KEY);
}


Private static Object getValue (Object key) {
Map PerThreadResources=resources. The get ();
Return perThreadResources!=null? PerThreadResources. Get (key) : null;
}


Private static final ThreadLocal & gt; Resources=new InheritableThreadLocalMap (a);
  • Related