250x250
Notice
Recent Posts
Recent Comments
Link
관심쟁이 영호
[ADYB] Session이 없으면 특정페이지 접근못하게 하기 본문
반응형
오늘은 Session이 없으면 특정 페이지에 접근을 하지 못하게 하는 작업을 할 것이다.
목차
- 전략
- 인터셉터 생성
- WebConfig 등록
전략
- 특정 페이지 URL 요청 시, Interceptor를 통해서 세션을 확인한다.
- 세션이 존재하지 않을 경우 "home"으로 redirect해준다.
세션 체크를 할 특정 페이지는 회원 수정, 게시판 글 작성이다.
인터셉터 생성
- 먼저 인터셉터를 생성하자.
SessionCheckInterceptor.java
@Slf4j
public class SessionCheckInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse
response, Object handler) throws Exception {
//Session Check - Session X
if(request.getSession().getAttribute(SessionConst.LOGIN_USER) == null){
// 세션이 없을 시, login으로 Redirect
response.sendRedirect("/login");
return false;
}
// Session O
return true;
};
}
코드 설명:
- request.getSession().getAttribute(SessionConst.LOGIN_USER) == null)
로그인 세션이 있는지 확인한다. 세션이 null일 경우(세션이 존재하지 않는 경우) if문에 진입한다.
- response.sendRedirect("/login")
세션이 없는 경우에 "/login" 으로 보낸다.
WebConfig 등록
@Configuration
public class WebConfig implements WebMvcConfigurer {
registry.addInterceptor(new SessionCheckInterceptor())
.order(2)
.addPathPatterns("/modify", "/board_write")
.excludePathPatterns("/css/**", "/*.ico", "/error","/login");
}
};
코드 설명:
- "registry.addInterceptor(new SessionCheckInterceptor()) : 인터셉터를 등록한다.
- ".order(2)" : 2번째 인터셉터이다.
- "addPathPatterns("/modify", "/board_write") : "/modify", "/board_write" 에만 인터셉터를 실행한다.
- .excludePathPatterns : 지정한 것은 해당 인터셉터를 실행하지 않는다.
300x250
'Project > ADYB - 쇼핑몰' 카테고리의 다른 글
[ADYB] 게시판 댓글기능 구현 (0) | 2021.08.05 |
---|---|
[ADYB] 게시판 글쓰기 구현 ㅣ JPA + Spring Boot + Thymeleaf (1) | 2021.08.03 |
[ADYB] Spring Boot ㅣ 로그아웃 구현하기 ㅣ 세션 만료시키기 (0) | 2021.08.03 |
[#6] JPA를 이용한 로그인 (0) | 2021.08.03 |
Comments