refactor login interceptor

This commit is contained in:
jinho jeong
2022-04-25 14:35:07 +09:00
parent cbf4c13e2e
commit 89ddd5dde6
4 changed files with 18 additions and 11 deletions

View File

@@ -1,4 +1,4 @@
package com.example.oneul.domain.user.command;
package com.example.oneul.domain.user.service;
import javax.servlet.http.HttpSession;
@@ -7,8 +7,8 @@ import com.example.oneul.domain.user.domain.UserEntity;
import org.springframework.stereotype.Service;
@Service
public interface UserCommandService {
public interface UserService {
UserEntity signUp(UserEntity userEntity, HttpSession httpSession);
UserEntity login(UserEntity userEntity , HttpSession httpSession);
UserEntity logout(UserEntity userEntity, HttpSession httpSession);
void logout(UserEntity userEntity, HttpSession httpSession);
}

View File

@@ -1,4 +1,4 @@
package com.example.oneul.domain.user.command;
package com.example.oneul.domain.user.service;
import javax.servlet.http.HttpSession;
@@ -15,12 +15,12 @@ import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserCommandServiceImpl implements UserCommandService {
public class UserServiceImpl implements UserService {
private final UserRepository userRepository;
private final Logger log = LoggerFactory.getLogger(UserCommandServiceImpl.class);
private final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
private final PasswordEncoder passwordEncoder;
public UserCommandServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder){
public UserServiceImpl(UserRepository userRepository, PasswordEncoder passwordEncoder){
this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder;
}
@@ -51,8 +51,10 @@ public class UserCommandServiceImpl implements UserCommandService {
}
@Override
public UserEntity logout(UserEntity userEntity, HttpSession httpSession){
return new UserEntity();
public void logout(UserEntity userEntity, HttpSession httpSession){
UserEntity user = (UserEntity) httpSession.getAttribute("user");
if(user == null) return ;
httpSession.removeAttribute("user");
}
}

View File

@@ -6,21 +6,25 @@ import javax.servlet.http.HttpSession;
import com.example.oneul.domain.user.domain.UserEntity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.servlet.HandlerInterceptor;
public class LoginCheckInterceptor implements HandlerInterceptor{
@Value("${login-page}")
private String loginPage;
@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");
response.sendRedirect(loginPage);
return false;
}
UserEntity userEntity = (UserEntity) httpSession.getAttribute("user");
if(userEntity == null){
response.sendRedirect("www.google.com");
response.sendRedirect(loginPage);
return false;
}
return true;

View File

@@ -27,3 +27,4 @@ logging:
hibernate:
SQL: DEBUG
log-in-page: www.naver.com