Spring

인터셉터, servlet-context.xml에 bean 추가

Birthmark 2019. 2. 25. 21:39


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.board.cban2.interceptor;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
 
public class LoginInterceptor extends HandlerInterceptorAdapter {
 
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        HttpSession session = request.getSession();
        String loginId = (String) session.getAttribute("loginId");
        if (loginId == null) {
            response.sendRedirect(request.getContextPath() + "/cban2/login");
            return false;
        }
        return super.preHandle(request, response, handler);
    }
}
 
cs




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
33
34
35
36
37
38
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 이 위쪽에 있는것들은 스키마. xml문서에서 태그사용시 기본적으로 제공되지 않지만 외부에서 받아오는 태그를 사용하고 싶을때  -->
    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
    
    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />
 
    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />
 
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>
    
    <context:component-scan base-package="com.board.cban2" />
    <beans:bean id = "loginInterceptor" class = "com.board.cban2.interceptor.LoginInterceptor"></beans:bean><!-- 3 -->
    <mvc:interceptors>
        <mvc:interceptor>
        <!-- 특정 경로를 지정해서 인터셉터를 연결시킨다. -->
        <mvc:mapping path="/cban2/write"/><!--1-->
        <!--  <mvc:mapping path="/cban2/*"/>-->
        <!-- <mvc:mapping path="/cban2/**"/>와일드카드-->
        
        <beans:ref bean="loginInterceptor"/><!-- 2 -->
        </mvc:interceptor>
    </mvc:interceptors>
</beans:beans>
 
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
 
<!-- interceptor 클래스를 빈으로 등록 -->
    <beans:bean id="loginInterceptor" class="global.scsoc.board.interceptor.LoginInterceptor"/>
    <!-- interceptor를 경유해야하는 요청을 지정함 -->
    <interceptors>
        <interceptor>
            <mapping path="/boardRegist"/>
            <mapping path="/boardUpdate"/>
            <mapping path="/boardDelete"/>
            
            <beans:ref bean="loginInterceptor"/>
        </interceptor>
    </interceptors>
    
를 </beans:beans> 위에 추가
cs