Spring Security(3)——異常処理
異常処理
ここではSpring Securityの簡単な異常なピックアップを紹介します.ソースの分析はしません.
よくある異常 UsernameNotFoundExceptionユーザは存在しませんが、この異常をプログラムに投げた時、よくキャッチしたのはBadCredentials Exceptionの異常です.このような状況はカスタム異常でAuthentication Exception類 を継承できます. DispabledExceptionユーザはすでに を無効にしました. BadCredentialsExceptionの悪い証明書 LockedExceptionアカウントロック Accent ExpiredExceptionアカウントの期限が切れました. CredentialsExpiredException証明書が失効しました.これらの異常はAuthentication Exceptionのサブクラスです. 二,spring securityの異常処理過程
(1)AbstractAuthentication ProcessigFilter.dofilter()
(2)AbstractAuthentication ProcessigFilter.unsuccess ful Authentication()
(3)SimpleUrlAuthentication Failure Handler.on Authentication Failure()この方法では、まず設定されているdefault Failure Url があるかどうかを判断します.設定がなければ、直接に401エラーを返します.すなわち、HttpStatus.UNAUTHORIZED もし設定されているなら、まずsaveException方法を実行し、forwardToDestination、すなわちサーバーがジャンプするかどうか、デフォルトでリダイレクトを使用します. (4)SimpleUrlAuthentication Failure Handler.saveException()まずforwardToDestination を判断します.サーバ・トランジットを使用するとRequest(転送) に書き込みます.クライアント・トランポリンを使用すると、Session(リダイレクト) に書き込みます.対応の異常情報をSPRING_uと書く.SECURITY_ラスト.EXCEPTIONは、BadCredentialsException の値です.
三、異常キャプチャコード
異常なグリップは私達が自分でControllerを書いて構成のを取りに行きます.もしエラーがあったらリダイレクトしたページを取りに行きます.配置類 Controller類
ここではSpring Securityの簡単な異常なピックアップを紹介します.ソースの分析はしません.
よくある異常
(1)AbstractAuthentication ProcessigFilter.dofilter()
(2)AbstractAuthentication ProcessigFilter.unsuccess ful Authentication()
(3)SimpleUrlAuthentication Failure Handler.on Authentication Failure()
三、異常キャプチャコード
異常なグリップは私達が自分でControllerを書いて構成のを取りに行きます.もしエラーがあったらリダイレクトしたページを取りに行きます.
/**
*
* @param http
* @throws Exception
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login")
.defaultSuccessUrl("/sayHello")
.failureUrl("/login/error")
.permitAll()
.and()
.logout().permitAll()
//
.and().rememberMe()
.tokenRepository(persistentTokenRepository())
// , s
.tokenValiditySeconds(60)
// service
.userDetailsService(userDetailsService);
// CSRF
http.csrf().disable();
}
(1)failureUrl():これはリダイレクトを表しています.クライアントジャンプです.sessionに書いてください.(2).failureForwardUrl():この方法は転送を表しています.サービストランジットです.requestに書いてください. @RequestMapping("/login/error")
public void loginError(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=utf-8");
AuthenticationException exception =
(AuthenticationException)request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
try {
response.getWriter().write(exception.toString());
}catch (IOException e) {
e.printStackTrace();
}
}