3

最近在使用spring security来控制方法的访问权限。访问一个页面可以触发认证方法(在@PreAuthorize 中指定)来决定用户是否有权限。

但我使用的是 Spring 计划作业(带有 @Scheduled 的方法),这意味着该方法在没有会话上下文的情况下自动运行。如果该方法使用@PreAuthorize 调用某个函数,则无法通过身份验证。它没有给出“真”或“假”来判断访问是被接受还是被拒绝。它给出了以下异常。这很烦人!

==================================================== =================================

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)

==================================================== =================================

我已经搜索了很多页面试图弄清楚,但我找不到最终的解决方案。

springsource.org 中的一个页面显示了问题:

http://static.springsource.org/spring-security/site/faq.html#auth-exception-credentials-not-found

1.5。我收到一条异常消息“在 SecurityContext 中找不到身份验证对象”。怎么了?

这是另一个调试级别的消息,当匿名用户第一次尝试访问受保护的资源时出现,但是当您的过滤器链配置中没有 AnonymousAuthenticationFilter 时。

DEBUG [ExceptionTranslationFilter] - 发生身份验证异常;重定向到身份验证入口点
org.springframework.security.AuthenticationCredentialsNotFoundException:在 org.springframework.security.intercept.AbstractSecurityInterceptor.credentialsNotFound(AbstractSecurityInterceptor.java:342) 在 org.springframework.security.intercept 的 SecurityContext 中找不到身份验证对象。 AbstractSecurityInterceptor.beforeInvocation(AbstractSecurityInterceptor.java:254)

我找到另一个页面来描述如何配置 AnonymousAuthenticationFilter http://static.springsource.org/spring-security/site/docs/3.0.x/reference/anonymous.html

它说配置应该是:

<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>

所以我在我的 application-security.xml 中配置它,但它没有任何区别。

<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>

有人精通春季安全吗?

4

1 回答 1

0

从异常和您的配置中,我看到没有身份验证过程。这对于任何访问安全资源都是必需的。所以你应该被重定向到entryPoint页面。

根据您的过滤器配置,我没有看到其他过滤器,这很奇怪。您使用的是命名空间还是过滤器链?如果后者为真,请添加其他过滤器。您可能需要查看内容以了解您的任务需要哪些过滤器。

于 2012-08-09T05:28:58.683 回答