空间站

北极心空

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  15 Posts :: 393 Stories :: 160 Comments :: 0 Trackbacks

acegi的登陆过程

先来无事看看acegi的登陆过滤器 写下来当作备忘吧
主要的类是AuthenticationProcessingFilter 继承了AbstractProcessingFilter 这要的逻辑都在后面这个类中
让我们看看核心代码吧
Java代码 复制代码
  1. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,   
  2.         ServletException {   
  3.     if (!(request instanceof HttpServletRequest)) {   
  4.         throw new ServletException("Can only process HttpServletRequest");   
  5.     }   
  6.   
  7.     if (!(response instanceof HttpServletResponse)) {   
  8.         throw new ServletException("Can only process HttpServletResponse");   
  9.     }   
  10.   
  11.     HttpServletRequest httpRequest = (HttpServletRequest) request;   
  12.     HttpServletResponse httpResponse = (HttpServletResponse) response;   
  13.   
  14.     if (requiresAuthentication(httpRequest, httpResponse)) {   
  15.         if (logger.isDebugEnabled()) {   
  16.             logger.debug("Request is to process authentication");   
  17.         }   
  18.   
  19.         Authentication authResult;   
  20. /下面才是重点 上面都是些基本检查   
  21.         try {   
  22.             onPreAuthentication(httpRequest, httpResponse);   
  23.             authResult = attemptAuthentication(httpRequest);//这个方法就是去登陆了 就是调用dao检查用户名密码 登陆不成功将抛出异常   
  24.         }   
  25.         catch (AuthenticationException failed) {   
  26.             // Authentication failed   
  27.             unsuccessfulAuthentication(httpRequest, httpResponse, failed);   
  28.   
  29.             return;   
  30.         }   
  31.   
  32.         // Authentication success   
  33.         if (continueChainBeforeSuccessfulAuthentication) {   
  34.             chain.doFilter(request, response);   
  35.         }   
  36.   
  37.         successfulAuthentication(httpRequest, httpResponse, authResult);   
  38.   
  39.         return;   
  40.     }   
  41.   
  42.     chain.doFilter(request, response);   
  43. }  


看一些登陆成功后 做些什么
Java代码 复制代码
  1.     protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,   
  2.             Authentication authResult) throws IOException {   
  3.         if (logger.isDebugEnabled()) {   
  4.             logger.debug("Authentication success: " + authResult.toString());   
  5.         }   
  6.   
  7. //把用户信息保存到SecurityContextHolder中1980   
  8. SecurityContextHolder.getContext().setAuthentication(authResult);   
  9.   
  10.         if (logger.isDebugEnabled()) {   
  11.             logger.debug("Updated SecurityContextHolder to contain the following Authentication: '" + authResult + "'");   
  12.         }   
  13. //转到目标页面 即登陆成功页面   
  14.         String targetUrl = determineTargetUrl(request);   
  15.   
  16.         if (logger.isDebugEnabled()) {   
  17.             logger.debug("Redirecting to target URL from HTTP Session (or default): " + targetUrl);   
  18.         }   
  19.   
  20.         onSuccessfulAuthentication(request, response, authResult);   
  21.   
  22.         rememberMeServices.loginSuccess(request, response, authResult);   
  23.   
  24.         // Fire event   
  25.         if (this.eventPublisher != null) {   
  26.             eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));   
  27.         }   
  28.   
  29.         sendRedirect(request, response, targetUrl);   
  30.     }  
posted on 2008-06-19 10:36 芦苇 阅读(855) 评论(0)  编辑  收藏 所属分类: SpringJAVA

只有注册用户登录后才能发表评论。


网站导航: