AuthenticationEntryPoint
security-context.xml を次のように設定して、Spring MVC アプリケーションで Rest サポートを有効にしました。
<http auto-config="false" use-expressions="true"
disable-url-rewriting="true" entry-point-ref="restAuthenticationEntryPoint">
RestAuthenticationEntryPoint.java
@Component
public final class RestAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized");
}
}
ユーザーが認証せずにリソースにアクセスしようとすると、次のエラーが発生します。
HTTP Status 401 - Unauthorized
上記の動作は、Rest サービスに対してのみ正しいものです。ただし、ユーザーが認証されていない場合、通常の Web 要求のログイン ページにユーザーをリダイレクトするデフォルトの動作が必要です。これを達成する方法は?