欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Spring Security3源碼分析-ExceptionTranslationFilter分析
ExceptionTranslationFilter過(guò)濾器對應的類(lèi)路徑為
org.springframework.security.web.access.ExceptionTranslationFilter
從類(lèi)名就看出這個(gè)過(guò)濾器用于異常翻譯的。但是從這個(gè)過(guò)濾器在filterchain中的位置來(lái)看,它僅僅處于倒數第三的位置(這個(gè)filter后面分為是FilterSecurityInterceptor、SwitchUserFilter),所以ExceptionTranslationFilter只能捕獲到后面兩個(gè)過(guò)濾器所拋出的異常。
這里需要強調一下,spring security中的異常類(lèi)基本上都繼承RuntimeException。

接著(zhù)看ExceptionTranslationFilter執行過(guò)程
Java代碼
 
  1. //doFilter攔截到請求時(shí),不做處理。僅僅處理后面filter所拋出的異常  
  2. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)  
  3.         throws IOException, ServletException {  
  4.     HttpServletRequest request = (HttpServletRequest) req;  
  5.     HttpServletResponse response = (HttpServletResponse) res;  
  6.   
  7.     try {  
  8.         chain.doFilter(request, response);  
  9.     }  
  10.     catch (IOException ex) {  
  11.         throw ex;  
  12.     }  
  13.     catch (Exception ex) {  
  14.         //這里主要是從異常堆棧中提取SpringSecurityException  
  15.         Throwable[] causeChain = throwableAnalyzer.determineCauseChain(ex);  
  16.         RuntimeException ase = (AuthenticationException)  
  17.                 throwableAnalyzer.getFirstThrowableOfType(AuthenticationException.class, causeChain);  
  18.   
  19.         if (ase == null) {  
  20.             ase = (AccessDeniedException)throwableAnalyzer.getFirstThrowableOfType(AccessDeniedException.class, causeChain);  
  21.         }  
  22.         //如果提取到安全異常,則進(jìn)行處理  
  23.         if (ase != null) {  
  24.             handleException(request, response, chain, ase);  
  25.         } else {  
  26.             //沒(méi)有安全異常,繼續拋出  
  27.             // Rethrow ServletExceptions and RuntimeExceptions as-is  
  28.             if (ex instanceof ServletException) {  
  29.                 throw (ServletException) ex;  
  30.             }  
  31.             else if (ex instanceof RuntimeException) {  
  32.                 throw (RuntimeException) ex;  
  33.             }  
  34.             throw new RuntimeException(ex);  
  35.         }  
  36.     }  
  37. }  
  38. //處理安全異常  
  39. private void handleException(HttpServletRequest request, HttpServletResponse response, FilterChain chain,  
  40.         RuntimeException exception) throws IOException, ServletException {  
  41.     //如果是認證異常,由sendStartAuthentication處理  
  42.     if (exception instanceof AuthenticationException) {  
  43.         sendStartAuthentication(request, response, chain, (AuthenticationException) exception);  
  44.     }  
  45.     //如果是訪(fǎng)問(wèn)拒絕異常,由訪(fǎng)問(wèn)拒絕處理類(lèi)的handle處理  
  46.     else if (exception instanceof AccessDeniedException) {  
  47.         if (authenticationTrustResolver.isAnonymous(SecurityContextHolder.getContext().getAuthentication())) {  
  48.             sendStartAuthentication(request, response, chain, new InsufficientAuthenticationException(  
  49.                     "Full authentication is required to access this resource"));  
  50.         }  
  51.         else {  
  52.             accessDeniedHandler.handle(request, response, (AccessDeniedException) exception);  
  53.         }  
  54.     }  
  55. }  

先分析如何處理認證異常
Java代碼
 
  1. //處理認證異常  
  2. protected void sendStartAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,  
  3.         AuthenticationException reason) throws ServletException, IOException {  
  4.     // SEC-112: Clear the SecurityContextHolder's Authentication, as the  
  5.     // existing Authentication is no longer considered valid  
  6.     //首先把SecurityContext中的認證實(shí)體置空  
  7.     SecurityContextHolder.getContext().setAuthentication(null);  
  8.     //通過(guò)cache保存當前的請求信息(分析RequestCacheAwareFilter時(shí)再深入)  
  9.     requestCache.saveRequest(request, response);  
  10.     logger.debug("Calling Authentication entry point.");  
  11.     //由認證入口點(diǎn)開(kāi)始處理  
  12.     authenticationEntryPoint.commence(request, response, reason);  
  13. }  

這里補充一下
authenticationEntryPoint是由配置http標簽時(shí),通過(guò)什么認證入口來(lái)決定注入相應的入口點(diǎn)bean的。請看下面的對應關(guān)系列表
form-login認證:LoginUrlAuthenticationEntryPoint
http-basic認證:BasicAuthenticationEntryPoint
openid-login認證:LoginUrlAuthenticationEntryPoint
x509認證:Http403ForbiddenEntryPoint


就不一一分析每個(gè)EntryPoint了,著(zhù)重看一下LoginUrlAuthenticationEntryPoint
Java代碼
 
  1. //主要目的是完成跳轉任務(wù)  
  2.  //創(chuàng )建該bean時(shí),只注入了loginFormUrl屬性,其他類(lèi)變量均為默認值  
  3. public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)  
  4.         throws IOException, ServletException {  
  5.     HttpServletRequest httpRequest = (HttpServletRequest) request;  
  6.     HttpServletResponse httpResponse = (HttpServletResponse) response;  
  7.   
  8.     String redirectUrl = null;  
  9.     //默認為false  
  10.     if (useForward) {  
  11.         if (forceHttps && "http".equals(request.getScheme())) {  
  12.             redirectUrl = buildHttpsRedirectUrlForRequest(httpRequest);  
  13.         }  
  14.   
  15.         if (redirectUrl == null) {  
  16.             String loginForm = determineUrlToUseForThisRequest(httpRequest, httpResponse, authException);  
  17.             RequestDispatcher dispatcher = httpRequest.getRequestDispatcher(loginForm);  
  18.             dispatcher.forward(request, response);  
  19.             return;  
  20.         }  
  21.     } else {  
  22.         //返回的url為loginFormUrl配置的值,如果未配置,跳轉到默認登錄頁(yè)面/spring_security_login  
  23.         redirectUrl = buildRedirectUrlToLoginPage(httpRequest, httpResponse, authException);  
  24.   
  25.     }  
  26.     redirectStrategy.sendRedirect(httpRequest, httpResponse, redirectUrl);  
  27. }  


接著(zhù)分析訪(fǎng)問(wèn)拒絕類(lèi)異常的處理過(guò)程,看AccessDeniedHandlerImpl的handle方法
Java代碼
 
  1. public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)  
  2.         throws IOException, ServletException {  
  3.     if (!response.isCommitted()) {  
  4.         //如果配置了access-denied-page屬性,跳轉到指定的url  
  5.         if (errorPage != null) {  
  6.             // Put exception into request scope (perhaps of use to a view)  
  7.             request.setAttribute(SPRING_SECURITY_ACCESS_DENIED_EXCEPTION_KEY, accessDeniedException);  
  8.   
  9.             // Set the 403 status code.  
  10.             response.setStatus(HttpServletResponse.SC_FORBIDDEN);  
  11.   
  12.             // forward to error page.  
  13.             RequestDispatcher dispatcher = request.getRequestDispatcher(errorPage);  
  14.             dispatcher.forward(request, response);  
  15.         //如果沒(méi)有配置,則直接響應403禁止訪(fǎng)問(wèn)的錯誤信息到瀏覽器端  
  16.         } else {  
  17.             response.sendError(HttpServletResponse.SC_FORBIDDEN, accessDeniedException.getMessage());  
  18.         }  
  19.     }  
  20. }  


通過(guò)以上分析,可以大體上認識到ExceptionTranslationFilter主要攔截兩類(lèi)安全異常:認證異常、訪(fǎng)問(wèn)拒絕異常。而且僅僅是捕獲FilterSecurityInterceptor、SwitchUserFilter以及自定義攔截器的異常。所以在自定義攔截器時(shí),需要注意在鏈中的順序。

在上面分析過(guò)程中,有requestCache.saveRequest(request, response);的語(yǔ)句,具體requestCache的用途下篇分析。
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
Spring Security教程(9)
AJAX傳值(精)
韓順平2011細說(shuō)Servlet筆記2
Java Servlet實(shí)現文件下載 可解決文件名中文亂碼
Proxool連接池在reload web容器時(shí)出現HouseKeeper的空指針異常 -...
【Java實(shí)戰干貨警告】談?wù)刉eb端實(shí)現即時(shí)消息推送五種方式
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久