SPRING #6 - INTERCEPTOR

0. 라이브러리 환경설정

0-1. /interceptor/interceptorConfig.java

  • 환경설정파일
  • 주소 필터 사용
package com.example.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

// 인터셉터 환경설정
// 주소로만 필터처리
@Configuration
public class interceptorConfig implements WebMvcConfigurer {

    @Autowired
    HandlerInterceptor interceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(interceptor)
                .addPathPatterns("/**")
                .excludePathPatterns( // 수행되지 않아야 하는 주소를 설정
                        "/member/login/**",
                        "/member/logout/**",
                        "/item/image/**",
                        "/item/subimage/**",
                        "/resources/**");
    }
}

1. 사용

1-1. /interceptor/HttpInterceptor.java

  • 주소정보, 쿼리정보를 불러오는 기능
  • 주소를 session에 BACKURL로 저장하고 불러오면서 사용
package com.example.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

@Component
public class HttpInterceptor implements HandlerInterceptor {

    @Override
    public void postHandle(
            HttpServletRequest request,
            HttpServletResponse response,
            Object handler,
            ModelAndView modelAndView) throws Exception {
        // 주소정보
        String pathName = request.getServletPath();
        // ?뒤의 QUERY정보
        String queryName = request.getQueryString();

        // 세션 객체 가져오기
        HttpSession httpSession = request.getSession();
        // 세션에 추가하기
        httpSession.setAttribute("BACKURL", pathName);

        System.out.println("----------interceptor----------");
        System.out.println("pathName => " + pathName);
        System.out.println("queryName => " + queryName);
        System.out.println("BACKURL => " + httpSession.getAttribute("BACKURL"));
    }
}

1-2. /controller/MemberController.java

  • 로그인 POST에서 적용
// 로그인
// login
@PostMapping(value = "loginaction")
public String loginactionPOST(
        @RequestParam(name = "uemail") String em,
        @RequestParam(name = "upw") String pw) {
    System.out.println(em);
    System.out.println(pw);

    MemberDTO member = mMapper.memberLogin(em, pw);
    System.out.println("member => " + member);
    if (member != null) {
        httpSession.setAttribute("M_EMAIL", member.getUemail());
        httpSession.setAttribute("M_NAME", member.getUname());
        httpSession.setAttribute("M_ROLE", member.getUrole());
        // 로그인 성공시
        String url = (String) httpSession.getAttribute("BACKURL");
        return "redirect:" + url;
    }
    return "redirect:/member/login";
}

좋은 웹페이지 즐겨찾기