私のでは、認証が失敗した場合、次のようにします (属性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" />
幸運を。