Skip to content

Commit

Permalink
[FIX] : REISSUE 문제 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
김교휘 authored and 김교휘 committed Dec 1, 2024
1 parent 4f9eb48 commit 3bef22e
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.successHandler(customSuccessHandler))
.authorizeHttpRequests(auth -> auth
.requestMatchers(HttpMethod.OPTIONS, "/**").permitAll() // 모든 OPTIONS 요청에 대해 인증을 요구하지 않음
.requestMatchers("/health-check", "/", "/auth/reissue/**", "/security-check").permitAll()
.requestMatchers("/health-check", "/", "/auth/reissue/**", "/security-check", "/reissue").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v2/mentors/{mentorId}/**").permitAll() // mentorId로 조회
.requestMatchers(HttpMethod.GET, "/api/v2/mentors/part").permitAll() // 파트별 조회.requestMatchers("/api/v2/users/**", "/auth/**").hasRole("USER")
.requestMatchers("/auth/reissue/mobile/**").permitAll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
}
String path = request.getRequestURI();
if (path.startsWith("/health-check") || path.startsWith("/security-check")
|| path.startsWith("/auth/reissue") || path.startsWith("/login")
|| path.startsWith("/auth/reissue") || path.startsWith("/login") || path.startsWith("/reissue")
|| path.matches("^/api/v2/mentors/\\d+$") || path.matches("^/api/v2/mentors/part$")) {
System.out.println("jwt필터 통과로직");
filterChain.doFilter(request, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Iterator;

//로그인이 성공했을 때 받은 데이터들을 바탕으로 JWT발급을 위한 핸들러
/*
@Component
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private final JWTUtil jwtUtil;
Expand Down Expand Up @@ -94,6 +95,76 @@ private void addSameSiteCookie(HttpServletResponse response, String name, String
response.addHeader("Set-Cookie", responseCookie.toString());
}
}
*/
@Component
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private final JWTUtil jwtUtil;
private final RefreshRepository refreshRepository;

public CustomSuccessHandler(JWTUtil jwtUtil, RefreshRepository refreshRepository) {
this.jwtUtil = jwtUtil;
this.refreshRepository = refreshRepository;
}

private void addRefreshEntity(String username, String refresh, Long expiredMs) {
Date date = new Date(System.currentTimeMillis() + expiredMs);

Refresh refreshEntity = new Refresh();
refreshEntity.setUsername(username);
refreshEntity.setRefresh(refresh);
refreshEntity.setExpiration(date.toString());

refreshRepository.save(refreshEntity);
}

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException, ServletException {

CustomOAuth2User customUserDetails = (CustomOAuth2User)authentication.getPrincipal();

String username = customUserDetails.getUsername();

Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
Iterator<? extends GrantedAuthority> iterator = authorities.iterator();
GrantedAuthority auth = iterator.next();
String role = auth.getAuthority();

//String accessToken = jwtUtil.createJwt("access", username, role, 600000L); // 10분
String accessToken = jwtUtil.createJwt("access", username, role, 180000L); // 10분
System.out.println("accessToken = " + accessToken);
String refreshToken = jwtUtil.createJwt("refresh", username, role, 86400000L); // 24시간

addRefreshEntity(username, refreshToken, 86400000L);

// Refresh 토큰 쿠키에 추가
addSameSiteCookie(response, "refresh", refreshToken);

// loginStatus 쿠키 추가
if (role.equals("ROLE_USER")) {
addSameSiteCookie(response, "loginStatus", "signup");
} else if (role.equals("ROLE_MENTEE") || role.equals("ROLE_MENTOR")) {
addSameSiteCookie(response, "loginStatus", "main");
}

response.setStatus(HttpStatus.OK.value());
//response.sendRedirect("http://localhost:8080/swagger-ui/index.html"); //서버 로컬 테스트용
//response.sendRedirect("https://localhost:3000/callback");
response.sendRedirect("https://coffeego-ssu.web.app/callback");
}

private void addSameSiteCookie(HttpServletResponse response, String name, String value) {
ResponseCookie responseCookie = ResponseCookie.from(name, value)
.httpOnly(true)
.secure(true)
.path("/")
.maxAge(24 * 60 * 60)
.sameSite("None")
.build();

response.addHeader("Set-Cookie", responseCookie.toString());
}
}

/*
package com.soongsil.CoffeeChat.config.oauth2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class RefreshTokenController { //Refresh토큰으로 Access토큰 발급
@ApiResponse(responseCode = "200", description = "헤더 : access, refresh, loginStatus")
public ResponseEntity<ApiResponseGenerator<String>> reissue(HttpServletRequest request,
HttpServletResponse response) {
//System.out.println("ㅇㅇ");
return ResponseEntity.ok().body(
ApiResponseGenerator.onSuccessOK(
refreshTokenService.reissueByRefreshToken(request, response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private void addRefreshEntity(String username, String refresh, Long expiredMs) {
}

public String reissueByRefreshToken(HttpServletRequest request, HttpServletResponse response) {
System.out.println("들어옴");
// Get refresh token
String refresh = null;
String loginStatus = null;
Expand Down

1 comment on commit 3bef22e

@DevDAN09
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.