当前位置:天才代写 > tutorial > JAVA 教程 > Acegi(十二):anonymousProcessingFilter有什么好玩的?

Acegi(十二):anonymousProcessingFilter有什么好玩的?

2017-11-13 08:00 星期一 所属: JAVA 教程 浏览:529

在这篇博客中, 我们接着看另一个Filter, anonymousProcessingFilter.

1, 为什么要设置这个? 它能给我们带来什么长处?

为了办理这个问题, 看了下Acegi的文档 , 但说实在的, 由于文档顶用是"convenient"和"nice"这样的词来描写这个filter的长处, 我此刻还感受不到. 这里把我此刻给想到的来由总结一下.

为了理论上的完美. 用了这个fitler,就可以对系统中所有 的链接加权限打点了, 像login, logout和home这样的"非通例会见",也可以加一个默认的匿名会见Authentication. 像这样的"非通例会见"是可以在acegi的设置文件中写死的, 虽然写死有写死的弊端. 由这个弊端,我们看下一个来由. 

为了某个链接的动态设定. 大概会有这样的环境: 一个链接的会见权限刚开始时是有专门的会见权限的, 但由于业务逻辑的改观, 这个链接的权限改为匿名的了,可能说本来的匿名会见要改为具有必然权限会见了. 这时, 为了在不重启处事器环境下设定权限, 就预先设置一个anonymousProcessingFilter, 让它来处理惩罚那些默认的环境.

此外我此刻猜不出来了, 但愿各人能补上.

2, 怎么设置?

虽说不是很领略, 但照旧要设置的. 那怎么设置呢? 我们先看这个filter所涉及到的三个类: AnonymousProcessingFilter, AnonymousAuthenticationToken,AnonymousAuthenticationProvider. 第一个类没什么说的, 它就是这个filter的实现类, 没有它办不成事. 第二个类实际上是一个Authentication, acegi通过它来加一个默认的匿名Authentication. 第三个类实现了AuthenticationProvider接口, 有了一个匿名的Authentication, 相应地得给一个Provider, 以便在filterInvocationInterceptor查抄权限时,被"卡"住. 呵呵, 看到这, 我以为挺可笑的了: 自己是一个虚头八脑的对象, 为了"掩盖"它, 让在真实世界里行的通, 还得再给弄两个一样虚头八脑的东东陪着.

有了一种大抵的相识后, 我们看设置:

2.1 设置anonymousProcessingFilter bean.

 

Xml代码

<bean id="anonymousProcessingFilter" class="org.acegisecurity.providers.anonymous.AnonymousProcessingFilter">
<property name="key" value="anonymous"/>
<property name="userAttribute" value="anonymous,ROLE_ANONYMOUS"/>
</bean>

这两个Property, 都是在生成AnonymousAuthenticationToken时用到.    userAttribute中的anonymous对应着Authentication(AnonymousAuthenticationToken也是一种Authentication,虽说有些虚)的principal, ROLE_ANONYMOUS对应着Authentication中的GrantedAuthority[],  key的anonymous生成AnonymousAuthenticationToken中的keyHash(通过String类的hashCode要领得到).

<property name="key" value="anonymous"/>与下面设置的anonymousAuthenticationProvider中的相应行对应.

2.2  设置anonymousAuthenticationProvider

Xml代码

<bean id="anonymousAuthenticationProvider" class="org.acegisecurity.providers.anonymous.AnonymousAuthenticationProvider">
<property name="key" value="anonymous"/>
</bean>

设置好后, 把这个anonymousAuthenticationProvider设置到ProviderManager类下providers, 这样filterInvocationInterceptor遇到前面设置的匿名Authentication时, 才气在AccessDecisionVoter"投票"时, 由AnonymousAuthentication的本身人anonymousAuthenticationProvider"掩护"着"逃过 "check.

一个意料: 这里的key跟上面anonymousProcessingFilter的key得一致, 否则在"投票"时, 没这个"灯号""本身人"也相互不认识了.想验证这个意料, 看了下文档,发明这样的话: "The key is shared between the filter and authentication provider, so that tokens created by the former are accepted by the latter".本身的意料不错!

 

有了上面的设置阐明, 运行机理稍看下源码就可以大白了, 这里也就不消再另写了.

—————————————–

看文档时发明这么段话, 以为很有须要记下来,虽说此刻还没有切身体验: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.

 

    关键字:

[contact-form-7 id="17097" title="form2"]天才代写-代写联系方式