session interceptor

This commit is contained in:
jinho jeong
2022-04-25 14:09:07 +09:00
parent 66ba084916
commit 37eb4fabbe
5 changed files with 46 additions and 2 deletions

View File

@@ -25,9 +25,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.session:spring-session-data-redis'
implementation 'org.springframework.session:spring-session-core'
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.session:spring-session-data-redis'
// implementation 'org.springframework.session:spring-session-core'
runtimeOnly 'com.h2database:h2'

View File

@@ -9,4 +9,6 @@ import org.springframework.stereotype.Service;
@Service
public interface UserCommandService {
UserEntity signUp(UserEntity userEntity, HttpSession httpSession);
UserEntity login(UserEntity userEntity , HttpSession httpSession);
UserEntity logout(UserEntity userEntity, HttpSession httpSession);
}

View File

@@ -36,5 +36,16 @@ public class UserCommandServiceImpl implements UserCommandService {
log.info("session value: " + httpSession.getAttribute("user"));
return user;
}
@Override
public UserEntity login(UserEntity userEntity, HttpSession httpSession){
UserEntity user = (UserEntity) httpSession.getAttribute("user");
return user;
}
@Override
public UserEntity logout(UserEntity userEntity, HttpSession httpSession){
return new UserEntity();
}
}

View File

@@ -64,6 +64,8 @@ public class UserEntity implements Serializable {
this.createdAt = createdAt;
}
public UserEntity() {}
public UserEntity(Long id, String username, String password, LocalDateTime createdAt){
this.id = id;
this.username = username;

View File

@@ -0,0 +1,28 @@
package com.example.oneul.global.util;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.example.oneul.domain.user.domain.UserEntity;
import org.springframework.web.servlet.HandlerInterceptor;
public class LoginCheckInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession httpSession = request.getSession(false);
if(httpSession == null){
response.sendRedirect("www.naver.com");
return false;
}
UserEntity userEntity = (UserEntity) httpSession.getAttribute("user");
if(userEntity == null){
response.sendRedirect("www.google.com");
return false;
}
return true;
}
}