Récemment, j'utilise la sécurité de printemps pour contrôler l'autorisation d'accès aux méthodes. L'accès à une page peut déclencher la méthode d'authentification (spécifiée dans @PreAuthorize) pour décider si l'utilisateur a la premission.Exception: Un objet d'authentification n'a pas été trouvé dans SecurityContex
Mais j'utilise le travail planifié par Spring (la méthode avec @Scheduled), ce qui signifie que la méthode s'exécute automatiquement sans contexte de session. Si la méthode appelle une fonction avec @PreAuthorize, elle ne peut pas passer l'authentification. Il ne donne pas de "vrai" ou de "faux" pour dire si l'accès est accepté ou refusé. Il donne l'exception suivante. C'est vraiment agaçant!
============================================== ====================================
org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:325)
at org.springframework.security.access.intercept.AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:196)
at org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor.invoke(MethodSecurityInterceptor.java:64)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy40.refreshGlobalCacheStrategyMetrics(Unknown Source)
at org.sly.main.server.service.system.scheduling.MaintenanceServiceImpl.runRecreateStrategyMetricsCache(MaintenanceServiceImpl.java:85)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:80)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.lang.Thread.run(Thread.java:662)
======= ============================================= =========================
J'ai cherché beaucoup de pages pour essayer de le comprendre, mais je ne peux pas trouver la finale solution pour cela.
Une page springsource.org montre le problème:
http://static.springsource.org/spring-security/site/faq.html#auth-exception-credentials-not-found
1,5. Je reçois une exception avec le message "Un objet d'authentification n'a pas été trouvé dans SecurityContext". Qu'est-ce qui ne va pas?
Ceci est un autre message de niveau de débogage qui se produit la première fois qu'un utilisateur anonyme tente d'accéder à une ressource protégée, mais quand vous ne pas avoir un AnonymousAuthenticationFilter dans votre chaîne de filtre de configuration . DEBUG [ExceptionTranslationFilter] - Exception de l'authentification survenue; redirection vers l'authentification point d'entrée
org.springframework.security.AuthenticationCredentialsNotFoundException: Un objet d'authentification n'a pas été trouvé dans le SecurityContext à org.springframework.security.intercept.AbstractSecurityInterceptor.credentialsNotFound (AbstractSecurityInterceptor.java:342) à org. springframework.security.intercept.AbstractSecurityInterceptor.beforeInvocation (AbstractSecurityInterceptor.java:254)
Et je trouve une autre page pour décrire comment configurer AnonymousAuthenticationFilter http://static.springsource.org/spring-security/site/docs/3.0.x/reference/anonymous.html
Il dit que la configuration de ce devrait être:
<bean id="anonymousAuthFilter"
class="org.springframework.security.web.authentication.AnonymousAuthenticationFilter">
<property name="key" value="foobar"/>
<property name="userAttribute" value="anonymousUser,ROLE_ANONYMOUS"/>
</bean>
<bean id="anonymousAuthenticationProvider"
class="org.springframework.security.authentication.AnonymousAuthenticationProvider">
<property name="key" value="foobar"/>
</bean>
Je configurer dans mon application security.xml mais il ne fait aucune différence.
<beans:bean id="springSecurityFilterChain"
class="org.springframework.security.web.FilterChainProxy">
<security:filter-chain-map path-type="ant">
<security:filter-chain pattern="/**"
filters="anonymousAuthFilter" />
</security:filter-chain-map>
</beans:bean>
<!-- ///////////////////////////////////////// -->
<!-- ////for AnonymousAuthenticationFilter//// -->
<!-- ///////////////////////////////////////// -->
<beans:bean id="anonymousAuthFilter"
class="org.springframework.security.web.authentication.AnonymousAuthenticationFilter">
<beans:property name="key" value="foobar" />
<beans:property name="userAttribute" value="anonymousUser,ROLE_ANONYMOUS" />
</beans:bean>
<beans:bean id="anonymousAuthenticationProvider"
class="org.springframework.security.authentication.AnonymousAuthenticationProvider">
<beans:property name="key" value="foobar" />
</beans:bean>
Quelqu'un connaît-il la sécurité au printemps?