티스토리 뷰

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.javalec.ex.handler;
 
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
 
public class CustomAuthenticationFailHandler implements AuthenticationFailureHandler {
    
    
    @Override
    public void onAuthenticationFailure(HttpServletRequest req, HttpServletResponse res, AuthenticationException arg2)
            throws IOException, ServletException {
        req.setAttribute("loginid", req.getParameter("bId"));
        req.getRequestDispatcher("/login_view?error=true").forward(req, res);
    }
 
}
//For example, if you receive your request on http://example.com/myapp/subdir,
//
//    RequestDispatcher dispatcher = 
//        request.getRequestDispatcher("index.jsp");
//    dispatcher.forward( request, response ); 
//Will forward the request to the page http://example.com/myapp/subdir/index.jsp.
//
//In any case, you can't forward request to a resource outside of the context.
cs


스프링 시큐리티에서 로그인 실패시 처리해주어야 할 일 들을 커스터마이징 하고 싶다면 AuthenticationFailureHandler를 

구현한 커스텀 클래스를 만들어 주면 됩니다. 


만들고 나서 아래와 같이 xml파일에 AuthenticationFailureHandler를 빈으로 등록해 준뒤,

authentication-failure-handler-ref="customAuthenticationFailHandler" />와 같이 설정해주면 스프링 시큐리티가 알아서 

로그인 실패시 이 핸들러를 거치게끔 해줍니다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
         <bean id="customAuthenticationFailHandler" class="com.javalec.ex.handler.CustomAuthenticationFailHandler"/>
         
     <security:authentication-manager>
         <security:authentication-provider user-service-ref ="customUserDetailsService">
             <security:password-encoder ref="passwordEncoder"/>
         </security:authentication-provider>
     </security:authentication-manager>
     
   
    
     <security:http use-expressions="true"> 
          <security:intercept-url pattern="/memberinfo/**" access="isAuthenticated()" />
          <security:intercept-url pattern="/write_view/**" access="isAuthenticated()" />
          <security:intercept-url pattern="/memberModify/**" access="isAuthenticated()" />
          <security:intercept-url pattern="/note/**" access="isAuthenticated()" />
          <security:intercept-url pattern="/login_view/**" access="isAnonymous()" />
          <security:intercept-url pattern="/**" access="permitAll" />
 
                   <security:form-login login-page="/login_view"
                           login-processing-url="/login"
                           username-parameter="bId"
                           password-parameter="bPass"
                           authentication-failure-handler-ref="customAuthenticationFailHandler" />
 
                  <security:logout logout-url="/logout" 
                                  logout-success-url="/index"/>       
     </security:http>
           
cs
  @Override
    public void onAuthenticationFailure(HttpServletRequest req, HttpServletResponse res, AuthenticationException arg2)
            throws IOException, ServletException {
        req.setAttribute("loginid", req.getParameter("bId"));
        req.getRequestDispatcher("/login_view?error=true").forward(req, res);
    }
우선 req.setAttribute("loginid", req.getParameter("bId"));를 보겠습니다.

현재 요청 파라미터에 있는 bId라는 변수에 들어있는 값을 loginid라는 값으로 새롭게 담아줍니다.

bId에는 로그인 창에서 사용자가 입력한 아이디가 들어가있습니다.

req.getRequestDispatcher("/login_view?error=true").forward(req, res);

그 뒤에 현재 요청 경로인 http://localhost:8181/ex//login_view?error=true 을 붙여서 로그인창으로 다시 안내해줍니다.

위의 함수는 현재 요청경로를 구해서(req.getRequestDispatcher) 뒤에 나오는 경로를 붙인뒤 

그 곳으로 forward를 해줍니다.

그렇게 되면 loginid라는 변수에 아까 사용자가 로그인시 입력했던 문자열이 들어간 상태로 login_view라는 

로그인창으로 다시 안내를 해주기 때문에 로그인이 실패했을때

아까 입력했던 로그인 아이디가 그대로 남아있게 되는것입니다.






댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함