5

私はしばらくの間、この問題に苦労してきました。それに関するいくつかの投稿が見つかりましたが、私の問題を解決したものはありませんでした。おそらく、SecurityContext が特定の Thread にバインドされているという事実と関係がありますが、それでも解決方法がわかりません。

ログインしたユーザーを取得するには、次のコードを検討してください。

SecurityContextHolder.getContext().getAuthentication().getPrincipal()

コントローラーでこのコードを実行すると、(正しく) ログインしたユーザーが返されます。このコードを taglib または jsp から実行すると、NPE (authentication = null) がスローされます。スプリングタグも機能しません(おそらく同じ理由で)。

web.xml から抽出:

    <filter>
    <filter-name>AcegiFilter</filter-name>
    <filter-class>org.acegisecurity.util.FilterToBeanProxy</filter-class>
    <init-param>
        <param-name>targetClass</param-name>
        <param-value>org.acegisecurity.util.FilterChainProxy</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>AcegiFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

春のセキュリティ構成ファイルから抽出します。

    <bean id="filterChainProxy" class="org.springframework.security.util.FilterChainProxy">
    <property name="filterInvocationDefinitionSource">
        <value>
            CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON 
            PATTERN_TYPE_APACHE_ANT
            /**=httpSessionIntegrationFilter,authenticationProcessingFilter,exceptionTranslationFilter,filterSecurityInterceptor
        </value>
    </property>
</bean>
    <bean id="filterSecurityInterceptor"
    class="org.springframework.security.intercept.web.FilterSecurityInterceptor">
    <property name="authenticationManager" ref="authenticationManager" />
    <property name="accessDecisionManager" ref="accessDecisionManager" />
    <property name="alwaysReauthenticate" value="true" />
    <property name="objectDefinitionSource">
        <value>
            CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON 
            PATTERN_TYPE_APACHE_ANT
            /myaccount.htm=ROLE_CUSTOMER
        </value>
    </property>
</bean>
4

2 に答える 2

0

これは当たり前の質問のように思えるかもしれませんが、ユーザーがページ /myaccount.htm にアクセスする前に、ユーザーにログイン (つまり、認証) を強制しましたか? オブジェクト定義ソースで匿名アクセスを必要とするログイン ページへのマッピングが見られなかったので、お尋ねします。ユーザーが認証なしで /myaccount.htm にアクセスできる場合、ユーザーがページにアクセスするまでにセキュリティ コンテキストでプリンシパルが作成されていないため、NullPointerException が発生します。また、フォームベースの認証を使用していますか? HTTP BASIC 認証? Acegi でサポートされている他のタイプはありますか?

于 2009-10-15T14:33:45.957 に答える