반응형
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
- 자바스크립트recude
- .NET
- 자바스크립트
- c#
- HTTP
- 객체의비교
- EntityFramework
- sort
- 인프런인강
- 코딩
- 콜백함수
- 자바스크립트함수
- 인프런강좌
- 인프런자바스크립트
- 객체리터럴
- 인터넷프로토콜
- slice
- 인프런
- 틱택토구현
- 비주얼스튜디오
- 인프런강의
- 자바스크립트객체리터럴
- 제로초
- 자바스크립트파라미터
- NPM
- 자바스크립트틱택토
- 이벤트리스너
- Blazor
- 고차함수
- 인프런무료강좌
Archives
- Today
- Total
샐님은 개발중
서블릿 필터 - 인증 체크 본문
728x90
반응형
1. 로그인시 필터 추가
LoginCheckFilter,java - 인증 체크 필터
package hello.login.web.filter;
import hello.login.web.SessionConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.PatternMatchUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Slf4j
public class LoginCheckFilter implements Filter {
private static final String[] whitelist = {"/","/members/add","/login","/logout","/css/*"};
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
String requestURI = httpRequest.getRequestURI();
HttpServletResponse httpResponse = (HttpServletResponse) response;
try {
log.info("인증 체크 필터 시작 {}", requestURI);
log.info("isLoginCheckPath(requestURI) ={}",isLoginCheckPath(requestURI));
if(isLoginCheckPath(requestURI)){
// 세션을 가지고 온다.
HttpSession session = httpRequest.getSession(false);
log.info("session ={}",session);
if(session == null || session.getAttribute(SessionConst.LOGIN_MEMBER) ==null){
// 로그인 정보가 없으면 로그인화면으로 리다이렉트 시킨다.
httpResponse.sendRedirect("/login?redirecrtURL="+requestURI); // 로그인한 후 다시 이전의 화면을 호출하기 위해
return; // 미인증 사용자는 다음으로 진행하지 않는다. 서블릿 호출하지않음
}
}
log.info("모든 유저 접근가능 화면");
chain.doFilter(request,response);
}catch(Exception e){
throw e; // 예외 로깅 가능하지만 톰캣까지 예외 보내야함 // 더 알아보기
}finally {
log.info("인증 체크 필터 종료 {}",requestURI);
}
}
/**
* 화이트 리스트의 경우 인증 체크X
*/
private boolean isLoginCheckPath(String requestURI) {
return !PatternMatchUtils.simpleMatch(whitelist, requestURI);
}
}
WebConfig- loginCheckFilter() 추가
package hello.login.web;
import hello.login.web.filter.LogFilter;
import hello.login.web.filter.LoginCheckFilter;
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 javax.servlet.Filter;
@Slf4j
@Configuration
public class WebConfig {
@Bean
public FilterRegistrationBean loginCheckFilter() {
FilterRegistrationBean<Filter> filterRegistrationBean = new
FilterRegistrationBean<>();
filterRegistrationBean.setFilter(new LoginCheckFilter());
filterRegistrationBean.setOrder(2);
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}
}
LoginController.java - 로그인 성공시 이전의 url 을 호출
@PostMapping("/login")
public String loginV4(@Validated @ModelAttribute LoginForm form, BindingResult
bindingResult, HttpServletRequest request, @RequestParam(value = "redirecrtURL",defaultValue = "/")String redirectUrl) {
if (bindingResult.hasErrors()) {
return "login/loginForm";
}
Member loginMember = loginService.login(form.getLoginId(),
form.getPassword());
log.info("login? {}", loginMember);
if (loginMember == null) {
bindingResult.reject("loginFail", "아이디 또는 비밀번호가 맞지 않습니다.");
return "login/loginForm";
}
//로그인 성공 처리
//세션이 있으면 있는 세션 반환, 없으면 신규 세션 생성
HttpSession session = request.getSession();
//세션에 로그인 회원 정보 보관
session.setAttribute(SessionConst.LOGIN_MEMBER, loginMember);
log.info("redirectUrl ={}",redirectUrl);
return "redirect:"+redirectUrl;
}
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 |