3

認証拒否時にカスタム エラー ページを表示し (事前認証済みのシナリオを使用しています)、Spring Security 3.0.X からのアクセス拒否も行いたいです。

以下を使用してこれを実行できることを理解してください。

<beans:bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
    <beans:property name="loginFormUrl" value="/error.jsp"/>
</beans:bean>

<beans:bean id="accessDeniedHandler" class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
    <beans:property name="errorPage" value="/error.jsp"/>
</beans:bean>

ただし、これを行うと、エラー ページへの転送ではなく、リダイレクトが発生します。とにかくエラーページへの転送を実行する(リクエストでいくつかの属性を設定できるようにするため)

ありがとう

4

1 に答える 1

3

私のでは、認証が失敗した場合、次のようにします (属性security-context.xmlに注意してください)。authentication-failure-url

    <security:form-login login-page="/auth/login"
        authentication-failure-url="/auth/login?error=true"
        default-target-url="/mvc/home"
        always-use-default-target="true" />

そして、アクセスが拒否された場合、私はこれを使用します:

    <security:access-denied-handler error-page="/auth/access-denied"/>

内の両方のタグ<security:http use-expressions="true">。私にとっては魅力のように機能します。Spring が使いやすい素晴らしいタグを提供しているのに、なぜあなたがやっているように設定しようとしているのかわかりません。

それがあなたの質問に答えているかどうかはわかりませんが、役に立てば幸いです。

編集:

上記の構成を使用すると、デフォルトの認証失敗ハンドラー ( SimpleUrlAuthenticationFailureHandler ) をバックグラウンドで使用することになります。属性値を変更することで、デフォルトの動作 (デフォルトでは、認証が失敗したときにリダイレクトを実行する) を変更できforwardToDestinationます。これは SimpleUrlAuthenticationFailureHandler が行うことです:

/**
 * Performs the redirect or forward to the {@code defaultFailureUrl} if set, otherwise returns a 401 error code.
 * <p>
 * If redirecting or forwarding, {@code saveException} will be called to cache the exception for use in
 * the target view.
 */
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {

    if (defaultFailureUrl == null) {
        logger.debug("No failure URL set, sending 401 Unauthorized error");

        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authentication Failed: " + exception.getMessage());
    } else {
        saveException(request, exception);

        if (forwardToDestination) {
            logger.debug("Forwarding to " + defaultFailureUrl);

            request.getRequestDispatcher(defaultFailureUrl).forward(request, response);
        } else {
            logger.debug("Redirecting to " + defaultFailureUrl);
            redirectStrategy.sendRedirect(request, response, defaultFailureUrl);
        }
    }
}

だから私はあなたがあなたを宣言しSimpleUrlAuthenticationFailureHandler、それが機能するメソッドsecurity-context.xmlを使用して言及されたプロパティ値を設定すると思います。setUseForward(boolean forwardToDestination)次のようなものかもしれません:

<bean id="simpleUrlAuthenticationFailureHandler" class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
    <property name="useForward" value="true">
</bean>

その後:

    <security:form-login login-page="/auth/login"
        authentication-failure-handler-ref="simpleUrlAuthenticationFailureHandler"
        default-target-url="/mvc/home"
        always-use-default-target="true" />

幸運を。

于 2012-10-03T09:10:31.680 に答える