authentication interceptor

This commit is contained in:
jinho jeong
2022-04-28 21:39:55 +09:00
parent 712e855d9d
commit 9b4e37f9af
9 changed files with 52 additions and 11 deletions

View File

@@ -1,12 +1,16 @@
package com.example.oneul.domain.user.dto;
import javax.validation.constraints.NotBlank;
import com.example.oneul.domain.user.domain.UserEntity;
import lombok.Getter;
@Getter
public class LoginDTO {
@NotBlank
private String username;
@NotBlank
private String password;
public void setUsername(String username){

View File

@@ -1,13 +1,18 @@
package com.example.oneul.domain.user.dto;
import javax.validation.constraints.NotBlank;
import com.example.oneul.domain.user.domain.UserEntity;
import lombok.Getter;
@Getter
public class SignUpDTO {
@NotBlank
private String username;
@NotBlank
private String password1;
@NotBlank
private String password2;
public void setUsername(String username ){

View File

@@ -0,0 +1,21 @@
package com.example.oneul.global.config.security;
import com.example.oneul.global.util.LoginCheckInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Value("${login-page}")
private String loginPage;
@Override
public void addInterceptors(InterceptorRegistry interceptorRegistry){
// TODO: 왜 exception이 다시 prehandler로 돌아가냐
interceptorRegistry.addInterceptor(new LoginCheckInterceptor(loginPage))
.excludePathPatterns("/user/login/**", "/user/signup/**");
}
}

View File

@@ -19,8 +19,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.cors().disable()
.csrf().disable()
.formLogin().disable()
.headers().frameOptions().disable();
.csrf().disable()
.formLogin().disable()
.headers().frameOptions().disable();
}
}

View File

@@ -1,6 +1,7 @@
package com.example.oneul.global.error;
import com.example.oneul.domain.user.exception.UserAlreadyExistException;
import com.example.oneul.domain.user.exception.WrongUsernameAndPasswordException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -13,13 +14,19 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
protected String handleException(Exception e){
log.debug(e.getMessage());
log.info(e.getMessage());
return e.toString();
}
@ExceptionHandler(UserAlreadyExistException.class)
protected String handleUserAlreadyExistException(UserAlreadyExistException e){
log.debug(e.getMessage());
log.info(e.getMessage());
return e.toString();
}
@ExceptionHandler(WrongUsernameAndPasswordException.class)
protected String handleWrongUsernameAndPasswordException(WrongUsernameAndPasswordException e){
log.info(e.getMessage());
return e.toString();
}
}

View File

@@ -8,14 +8,16 @@ import com.example.oneul.domain.user.domain.UserEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
public class LoginCheckInterceptor implements HandlerInterceptor{
@Value("${login-page}")
private String loginPage;
private final Logger log = LoggerFactory.getLogger(LoginCheckInterceptor.class);
public LoginCheckInterceptor(String loginPage){
this.loginPage = loginPage;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("login check prehandler");

View File

@@ -30,4 +30,4 @@ logging:
hibernate:
SQL: DEBUG
log-in-page: www.naver.com
login-page: https://www.naver.com

View File

@@ -31,4 +31,4 @@ logging:
hibernate:
SQL: DEBUG
log-in-page: www.naver.com
login-page: https://www.google.com

View File

@@ -27,4 +27,6 @@ logging:
web: DEBUG
org:
hibernate:
SQL: DEBUG
SQL: DEBUG
login-page: https://www.naver.com