SpringSecurityがこの呼び出しに対して匿名ユーザーのUserDetailsオブジェクトを返すようにする方法を理解しようとしています。
SecurityContextHolder.getContext().getAuthentication().getPrincipal()
特別な構成がないと、その呼び出しは、カスタムUserDetailsService実装で作成したUserDetailsオブジェクトの代わりに文字列を返すことを知っていますが、どこでも「if(principal instanceofString)」を常にチェックしたくはありません。Spring構成でこれを行う方法はありますか?ユーザーがログインするまで匿名のUserDetailsオブジェクトをユーザーのセッションコンテキストに保存する方法はありますか?表面上は、ゲストごとに一意の匿名UserDetailsが必要なので、個々の使用状況を追跡できます。
また、「PreAuthorize」アノテーションで保護したメソッドが、匿名ユーザーによるhasRoleチェックを尊重していないように見えることにも気づきました。それは私が間違っていることの症状だと確信しています。その例を次に示します。
@RequestMapping(value = "/almanac/new", method = RequestMethod.GET)
@PreAuthorize("hasRole('ROLE_ADMIN')")
public String newSetup(ModelMap model) {
これが私の春のセキュリティコンテキストです(囲んでいるbeansノードを除いて完全です)。「匿名」を有効にしようとしたことに注意してください
<debug />
<http pattern="/js/**" security="none" />
<http pattern="/css/**" security="none" />
<http pattern="/images/**" security="none" />
<http pattern="/loggedout.jsp" security="none"/>
<http name="httpSiteMap" use-expressions="true">
<custom-filter ref="almanacUsrPwdAuthProcFilter" before="FORM_LOGIN_FILTER"/>
<intercept-url pattern="/login*" access="isAnonymous()" />
<intercept-url pattern="/home/**" access="hasRole('ROLE_USER')" />
<intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/**" access="isAnonymous()" />
<form-login login-page="/login.jsp"
default-target-url="/home.htm"
always-use-default-target="false" />
<logout logout-success-url="/loggedout.jsp" delete-cookies="JSESSIONID"/>
<session-management invalid-session-url="/timeout.jsp">
<concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</session-management>
<anonymous enabled="true" />
</http>
<global-method-security pre-post-annotations="enabled" secured-annotations="enabled" />
<authentication-manager alias="mainAuthMgr">
<authentication-provider ref="almanacAuthenticationProvider"/>
</authentication-manager>
ここに表示されているコードに基づくその他の提案は大歓迎です。