在这篇博客中, 我们接着看另一个Filter, anonymousProcessingFilter.
1, 为什么要配置这个? 它能给我们带来什么好处?
为了解决这个问题, 看了下Acegi的文档 , 但说实在的, 由于文档中用是"convenient"和"nice"这样的词来描述这个filter的好处, 我现在还感觉不到. 这里把我现在给想到的理由总结一下.
2, 怎么配置?
虽说不是很理解, 但还是要配置的. 那怎么配置呢? 我们先看这个filter所涉及到的三个类: AnonymousProcessingFilter, AnonymousAuthenticationToken,AnonymousAuthenticationProvider. 第一个类没什么说的, 它就是这个filter的实现类, 没有它办不成事. 第二个类实际上是一个Authentication, acegi通过它来加一个默认的匿名Authentication. 第三个类实现了AuthenticationProvider接口, 有了一个匿名的Authentication, 相应地得给一个Provider, 以便在filterInvocationInterceptor检查权限时,被"卡"住. 呵呵, 看到这, 我觉得挺好笑的了: 本身是一个虚头八脑的东西, 为了"掩盖"它, 让在真实世界里行的通, 还得再给弄两个一样虚头八脑的东东陪着.
有了一种大致的了解后, 我们看配置:
2.1 配置anonymousProcessingFilter bean.
<bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter"> <property name="key" value="anonymous"/> <property name="userAttribute" value="anonymous,ROLE_ANONYMOUS"/> </bean>
2.2 配置anonymousAuthenticationProvider
<bean id="anonymousAuthenticationProvider" class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider"> <property name="key" value="anonymous"/> </bean>
有了上面的配置分析, 运行机理稍看下源码就可以明白了, 这里也就不用再另写了.
-----------------------------------------
看文档时发现这么段话, 觉得很有必要记下来,虽说现在还没有切身体验:Rounding out the anonymous authentication discussion is the AuthenticationTrustResolver interface, with its corresponding AuthenticationTrustResolverImpl implementation. This interface provides an isAnonymous(Authentication) method, which allows interested classes to take into account this special type of authentication status. The ExceptionTranslationFilter uses this interface in processing AccessDeniedExceptions. If an AccessDeniedException is thrown, and the authentication is of an anonymous type, instead of throwing a 403 (forbidden) response, the filter will instead commence the AuthenticationEntryPoint so the principal can authenticate properly. This is a necessary distinction, otherwise principals would always be deemed "authenticated" and never be given an opportunity to login via form, basic, digest or some other normal authentication mechanism.