[MVC2 게시판] 6. 접근 제한 걸기(Filter 이용)

2016. 12. 6. 16:27Programming/JSP

<오늘 배운 것>


1) web.xml에 매핑하기


경로: WebContent > WEB-INF > web.xml


1
2
3
4
5
6
7
8
9
10
11
 
<filter>
            <filter-name>loginCheck</filter-name>
            <filter-class>com.model2.filter.LoginCheck</filter-class>
</filter>
<filter-mapping>
            <filter-name>loginCheck</filter-name>
            <url-pattern>*.dml</url-pattern>
            <!-- 요청 끝에 .dml이 오면 LoginCheck Filter에 들린다. -->
</filter-mapping>
 
cs


2) Filter를 구현


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
32
 
public class LoginCheck implements Filter{
 
            @Override
            public void destroy() {                 
            }
 
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                                    throws IOException, ServletException {
                        System.out.println("<<AdminCheck test>>");
                        HttpServletRequest httpReq = (HttpServletRequest)request;
                        HttpSession session = httpReq.getSession();
                        Object login = session.getAttribute("login");
                        boolean loginCheck = false;
                        if(login!=null){
                                    if((int)login>0){
                                                loginCheck = true;
                                    }
                        }
                        if(loginCheck){ //로그인O일 때
                                    chain.doFilter(request, response); //이걸 써야 목적지로 이동
                        }else{          //로그인X일 때
                                    RequestDispatcher dispatcher = request.getRequestDispatcher("/boardList.do");
                                    dispatcher.forward(request, response);
                        }           
            }
            @Override
            public void init(FilterConfig arg0) throws ServletException {              
            }
}
 
cs


'Programming > JSP' 카테고리의 다른 글

[MVC2 게시판] 7. Error 페이지 띄우기  (0) 2016.12.13
[MVC2 게시판] 5. 파일 업로드  (0) 2016.12.01
[MVC2 게시판] 4. 페이징(Paging)  (0) 2016.11.28
[MVC2 게시판] 3. 회원관리  (0) 2016.11.25
[MVC2 게시판] 2. 로그인  (0) 2016.11.25