반응형
250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 객체의비교
- 비주얼스튜디오
- 자바스크립트함수
- 인프런강의
- 인프런강좌
- 고차함수
- 틱택토구현
- HTTP
- NPM
- Blazor
- EntityFramework
- 객체리터럴
- 인프런자바스크립트
- 인터넷프로토콜
- slice
- 자바스크립트파라미터
- 자바스크립트객체리터럴
- 콜백함수
- 제로초
- 인프런인강
- 인프런무료강좌
- 이벤트리스너
- c#
- 인프런
- .NET
- 자바스크립트
- 자바스크립트틱택토
- 코딩
- 자바스크립트recude
- sort
Archives
- Today
- Total
샐님은 개발중
스프링 인터셉터 본문
728x90
반응형
1. 인터셉터
- 서블릿 필터와 같이 웹과 관련된 공통 관심 사항을 해결하는 스프링 기술
서블릿 필터와는 적용되는 순서, 범위, 사용방법이 다르다.
2. 스프링 인터셉터 흐름
HTTP 요청 -> WAS -> 필터 -> 서블릿 -> 스프링 인터셉터 -> 컨트롤러
3. 스프링 인터셉터 체인
HTTP 요청 -> WAS -> 필터 -> 서블릿 -> 인터셉터1 -> 인터셉터2 -> 컨트롤러
4. 스프링 인터셉터 인터페이스
public interface HandlerInterceptor {
// 컨트롤러 호출 전에 호출(정확히 핸들러 어댑터 호출전)
default boolean preHandle(HttpServletRequest request, HttpServletResponse
response,
Object handler) throws Exception {}
// 컨트롤러 호출 후에 호출(정확히 핸들러 어댑터 호출후)
default void postHandle(HttpServletRequest request, HttpServletResponse
response,
Object handler, @Nullable ModelAndView modelAndView)
throws Exception {}
// 뷰가 렌더링 된 이후에 호출
default void afterCompletion(HttpServletRequest request, HttpServletResponse
response,
Object handler, @Nullable Exception ex) throws
Exception {}
}
postHandle : 컨트롤러에서 예외 발생시 호출되지 않음.
afterCompletion : 예외 발생상관없이 호출됨.
* 필터를 꼭 사용해야하는 경우 외에 인터셉터를 사용하는 편이 좋음
5. 로그인에 적용
1) LoginCheckInterceptor.java 생성
package hello.login.web.interceptor;
import hello.login.web.SessionConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@Slf4j
public class LoginCheckInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) throws Exception {
String requestURI = request.getRequestURI();
log.info("인증 체크 인터셉터 실행 {}", requestURI);
HttpSession session = request.getSession(false);
if (session == null || session.getAttribute(SessionConst.LOGIN_MEMBER)
== null) {
log.info("미인증 사용자 요청");
//로그인으로 redirect
response.sendRedirect("/login?redirectURL=" + requestURI);
return false;
}
return true;
}
}
2) WebConfig.java 에 인터셉터 등록
package hello.login.web;
import hello.login.web.filter.LogFilter;
import hello.login.web.filter.LoginCheckFilter;
import hello.login.web.interceptor.LogInterceptor;
import hello.login.web.interceptor.LoginCheckInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import javax.servlet.Filter;
@Slf4j
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LogInterceptor())
.order(1)
.addPathPatterns("/**")
.excludePathPatterns("/css/**", "/*.ico", "/error");
registry.addInterceptor(new LoginCheckInterceptor())
.order(2)
.addPathPatterns("/**")
.excludePathPatterns(
"/", "/members/add", "/login", "/logout",
"/css/**", "/*.ico", "/error"
);
}
}
인터셉터를 적용하거나 하지 않을 부분은 addPathPatterns 와 excludePathPatterns 에 작성
728x90
반응형
'스프링 MVC 2편 - 인프런 김영한 > 섹션 6,7 - 로그인 처리' 카테고리의 다른 글
서블릿 필터 - 인증 체크 (0) | 2023.07.15 |
---|---|
서블릿 필터 (0) | 2023.07.15 |
3. 로그인 - 서블릿 HTTP 세션 1 (0) | 2023.07.15 |
2. 로그인 - 세션 직접 구현 (0) | 2023.07.15 |
1. 로그인 - 세션 방식 (0) | 2023.07.15 |