이글루스 | 로그인  



로그인 처리 관련

웹 검색해 보니... Filter를 사용하는 방법이 있네요.

출저[http://www.devtt.com/Ryan/18]

 

로그인 판별, 로그인 여부는 session 기본 객체가 이름일 "login"인 속성을 갖고 있는 지에 따라 결정

  1. public class AuthFilter implements Filter
    {
        private FilterConfig filterConfig;
        public void init(FilterConfig filterConfig) throws ServletException
        {
            this.filterConfig = filterConfig;
        }
        public void destroy()
        {
            filterConfig = null;
        }
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException
        {
            HttpSession session = ((HttpServletRequest) request).getSession(false);
            if (session == null || session.getAttribute("login") == null)
            {
                System.out.println("not login");
                ((HttpServletResponse) response)
                        .sendRedirect("http://localhost:8090/www2/main.jsp");
            }
            else
            {
                System.out.println("already login");
                chain.doFilter(request, response);
            }
        }
    }

getSession() 메서드는 HttpServletRequest 클래스에 정의 되어 있으므로 request를 HttpServletRequest로 다운 캐스팅한 후 getSession() 호출

getSession(false)는 세션이 불필요하게 생성되는 경우를 막기 위해




<filter>

<filter-name>AuthFilter</filter-name>
<filter-class>com.package.AuthFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

인터셉트 구현을 아래와 같이 한 것도 있었습니다.

이 경우 저 인터셉트를 xml 한곳에서만 선언후 전 사이트에 적용하는 방법을

모르겠습니다.

매번 빈에서 인터셉트를 참조 하게 하는것은 xml이 너무 지저분해 지는거

같습니다

public class InterceptorExample extends HandlerInterceptorAdapter{

 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception
{
  HttpSession session  = request.getSession();
//  User user = (User)session.getAttribute("user"); 
  
  return true;
}
}


1. 올려주신건 서블릿의 필터를 이용하는 방법이고,
2. Controller 레이어에서 작업을 필요로 한다면 HandlerInterceptor 을 쓰시면되고요
3. AOP를 이용하시려면
for ( Object o : joinPoint.getArgs() ){
if ( o instanceof HttpServletRequest ) { o.겟셋션, 필요한 작업 }
} 이런식으로 구현하시면 되요.

by 나림 | 2009/08/24 19:04 | spring | 트랙백 | 덧글(0)

트랙백 주소 : http://gt1000.egloos.com/tb/2472143
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]

:         :

:

비공개 덧글

◀ 이전 페이지          다음 페이지 ▶