Skip to content

Commit

Permalink
[FEATURE] IDPW 로그인시 쿠키로 임시 토큰 발급 (#354)
Browse files Browse the repository at this point in the history
* feat(token): 임시 수정

* feat(token): 로직 변경

* feat(token): 의존성 주입
  • Loading branch information
m-a-king authored Feb 5, 2025
1 parent beaa7ad commit 9fd9245
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.somemore.global.auth.authentication.UserIdentity;
import com.somemore.global.auth.cookie.CookieUseCase;
import com.somemore.global.auth.jwt.domain.EncodedToken;
import com.somemore.global.auth.jwt.domain.TokenType;
import com.somemore.global.auth.jwt.usecase.GenerateTokensOnLoginUseCase;
import jakarta.servlet.FilterChain;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -26,6 +28,7 @@ public class IdPwAuthFilter extends UsernamePasswordAuthenticationFilter {

private final AuthenticationManager authenticationManager;
private final GenerateTokensOnLoginUseCase generateTokensOnLoginUseCase;
private final CookieUseCase cookieUseCase;
private final ObjectMapper objectMapper;

@Override
Expand All @@ -42,9 +45,8 @@ protected void successfulAuthentication(HttpServletRequest request, HttpServletR
response.setStatus(HttpServletResponse.SC_OK);

UserIdentity userIdentity = (UserIdentity) authResult.getPrincipal();
EncodedToken accessToken = generateTokensOnLoginUseCase.generateAuthTokensAndReturnAccessToken(userIdentity);

response.setHeader("Authorization", accessToken.getValueWithPrefix());
processToken(response, userIdentity);
}

@Override
Expand All @@ -55,6 +57,14 @@ protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServle
objectMapper.writeValue(response.getWriter(), problemDetail);
}

private void processToken(HttpServletResponse response, UserIdentity userIdentity) {
generateTokensOnLoginUseCase.generateAuthTokensAndReturnAccessToken(userIdentity);

EncodedToken loginToken = generateTokensOnLoginUseCase.generateLoginToken(userIdentity);

cookieUseCase.setToken(response, loginToken.value(), TokenType.SIGN_IN);
}

private void configureUnauthorizedResponse(HttpServletResponse response) {
response.setStatus(HttpStatus.UNAUTHORIZED.value());
response.setContentType(MediaType.APPLICATION_PROBLEM_JSON_VALUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@RequiredArgsConstructor
public enum TokenType {
ACCESS(Duration.ofMinutes(30)),
ACCESS(Duration.ofMinutes(1)),
REFRESH(Duration.ofDays(7)),
SIGN_IN(Duration.ofMinutes(1)),
SIGN_OUT(Duration.ZERO);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/somemore/global/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.somemore.global.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.somemore.global.auth.cookie.CookieUseCase;
import com.somemore.global.auth.idpw.filter.IdPwAuthFilter;
import com.somemore.global.auth.jwt.filter.JwtAuthFilter;
import com.somemore.global.auth.jwt.filter.JwtExceptionFilter;
Expand Down Expand Up @@ -45,10 +46,10 @@ public AuthenticationManager authenticationManager(AuthenticationConfiguration a
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity,
AuthenticationManager authenticationManager,
GenerateTokensOnLoginUseCase generateTokensOnLoginUseCase,
// CookieUseCase cookieUseCase,
CookieUseCase cookieUseCase,
ObjectMapper objectMapper) throws Exception {

IdPwAuthFilter idPwAuthFilter = new IdPwAuthFilter(authenticationManager, generateTokensOnLoginUseCase, objectMapper);
IdPwAuthFilter idPwAuthFilter = new IdPwAuthFilter(authenticationManager, generateTokensOnLoginUseCase, cookieUseCase, objectMapper);
idPwAuthFilter.setFilterProcessesUrl("/api/sign-in/id-pw");

httpSecurity
Expand Down

0 comments on commit 9fd9245

Please sign in to comment.