Skip to content

Commit

Permalink
[fix] SecurityContextHolder 관련 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
nimikgnoej committed Aug 30, 2023
1 parent 67e7451 commit f4095e9
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 11 deletions.
Binary file modified .gradle/8.1.1/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified .gradle/8.1.1/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified .gradle/8.1.1/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified .gradle/8.1.1/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified .gradle/8.1.1/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified .gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
4 changes: 3 additions & 1 deletion src/main/java/com/example/taskqueue/config/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpSession;
import java.util.List;

@Configuration
@RequiredArgsConstructor
public class WebMvcConfig implements WebMvcConfigurer {
private final UserRepository userRepository;
private final HttpSession httpSession;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new CurrentUserArgumentResolver(userRepository));
argumentResolvers.add(new CurrentUserArgumentResolver(userRepository,httpSession));
}
@Override
public void addCorsMappings(CorsRegistry registry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.taskqueue.oauth.currentUser;

import com.example.taskqueue.common.annotation.CurrentUser;
import com.example.taskqueue.oauth.dto.SessionUser;
import com.example.taskqueue.user.entity.User;
import com.example.taskqueue.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,12 +14,14 @@
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpSession;
import java.util.Optional;

@RequiredArgsConstructor
@Slf4j
public class CurrentUserArgumentResolver implements HandlerMethodArgumentResolver {
private final UserRepository userRepository;
private final HttpSession httpSession;
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterAnnotation(CurrentUser.class) != null;// &&
Expand All @@ -30,10 +33,9 @@ public Object resolveArgument(MethodParameter parameter,
ModelAndViewContainer mavContainer,
NativeWebRequest webRequest,
WebDataBinderFactory binderFactory) throws Exception {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
System.out.println("authentication = " + authentication.getPrincipal());
Optional<User> findUser = userRepository.findByEmail(authentication.getName());
//System.out.println("findUser = " + findUser.get().toString());
SessionUser user = (SessionUser) httpSession.getAttribute("user");
Optional<User> findUser = userRepository.findByEmail(user.getEmail());
System.out.println("findUser = " + findUser.get().toString());
if (findUser.isPresent() && findUser.get().getRefreshToken()!=null) {
//DB에 사용자가 있고 refresh token이 있다면(로그인 된 상태)
return findUser.get();
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/example/taskqueue/oauth/dto/SessionUser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.taskqueue.oauth.dto;

import com.example.taskqueue.oauth.CustomOAuth2User;
import lombok.Getter;

@Getter
public class SessionUser {
private String name;
private String email;
public SessionUser(CustomOAuth2User oAuth2User) {
this.name = oAuth2User.getName();
this.email = oAuth2User.getEmail();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.example.taskqueue.exception.jwt.JwtErrorCode;
import com.example.taskqueue.exception.notfound.config.ResourceNotFoundErrorCode;
import com.example.taskqueue.oauth.CustomOAuth2User;
import com.example.taskqueue.oauth.dto.SessionUser;
import com.example.taskqueue.oauth.jwt.JwtService;
import com.example.taskqueue.security.ResponseUtils;
import com.example.taskqueue.user.entity.Role;
Expand All @@ -22,6 +23,7 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Optional;

Expand All @@ -32,19 +34,16 @@ public class OAuth2LoginSuccessHandler implements AuthenticationSuccessHandler {

private final JwtService jwtService;
private final ResponseUtils responseUtils;
private final UserRepository userRepository;
private final HttpSession httpSession;
private final UserService userService;

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
try {
log.info("OAuth2 Login 성공!");
SecurityContextHolder.setStrategyName(SecurityContextHolder.MODE_INHERITABLETHREADLOCAL);
SecurityContext context = SecurityContextHolder.createEmptyContext();
context.setAuthentication(authentication);
SecurityContextHolder.setContext(context);

CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal();
httpSession.setAttribute("user",new SessionUser(oAuth2User));
SessionUser user = (SessionUser) httpSession.getAttribute("user");
System.out.println("oAuth2User = " + oAuth2User.toString());
loginSuccess(response,oAuth2User);
} catch (Exception e) {
Expand Down

0 comments on commit f4095e9

Please sign in to comment.