acegi的登陸過(guò)程
先來(lái)無(wú)事看看acegi的登陸過(guò)濾器 寫下來(lái)當(dāng)作備忘吧
主要的類是AuthenticationProcessingFilter 繼承了AbstractProcessingFilter 這要的邏輯都在后面這個(gè)類中
讓我們看看核心代碼吧
看一些登陸成功后 做些什么
主要的類是AuthenticationProcessingFilter 繼承了AbstractProcessingFilter 這要的邏輯都在后面這個(gè)類中
讓我們看看核心代碼吧
- public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
- ServletException {
- if (!(request instanceof HttpServletRequest)) {
- throw new ServletException("Can only process HttpServletRequest");
- }
- if (!(response instanceof HttpServletResponse)) {
- throw new ServletException("Can only process HttpServletResponse");
- }
- HttpServletRequest httpRequest = (HttpServletRequest) request;
- HttpServletResponse httpResponse = (HttpServletResponse) response;
- if (requiresAuthentication(httpRequest, httpResponse)) {
- if (logger.isDebugEnabled()) {
- logger.debug("Request is to process authentication");
- }
- Authentication authResult;
- /下面才是重點(diǎn) 上面都是些基本檢查
- try {
- onPreAuthentication(httpRequest, httpResponse);
- authResult = attemptAuthentication(httpRequest);//這個(gè)方法就是去登陸了 就是調(diào)用dao檢查用戶名密碼 登陸不成功將拋出異常
- }
- catch (AuthenticationException failed) {
- // Authentication failed
- unsuccessfulAuthentication(httpRequest, httpResponse, failed);
- return;
- }
- // Authentication success
- if (continueChainBeforeSuccessfulAuthentication) {
- chain.doFilter(request, response);
- }
- successfulAuthentication(httpRequest, httpResponse, authResult);
- return;
- }
- chain.doFilter(request, response);
- }
看一些登陸成功后 做些什么
- protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response,
- Authentication authResult) throws IOException {
- if (logger.isDebugEnabled()) {
- logger.debug("Authentication success: " + authResult.toString());
- }
- //把用戶信息保存到SecurityContextHolder中1980
- SecurityContextHolder.getContext().setAuthentication(authResult);
- if (logger.isDebugEnabled()) {
- logger.debug("Updated SecurityContextHolder to contain the following Authentication: '" + authResult + "'");
- }
- //轉(zhuǎn)到目標(biāo)頁(yè)面 即登陸成功頁(yè)面
- String targetUrl = determineTargetUrl(request);
- if (logger.isDebugEnabled()) {
- logger.debug("Redirecting to target URL from HTTP Session (or default): " + targetUrl);
- }
- onSuccessfulAuthentication(request, response, authResult);
- rememberMeServices.loginSuccess(request, response, authResult);
- // Fire event
- if (this.eventPublisher != null) {
- eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
- }
- sendRedirect(request, response, targetUrl);
- }