Skip to content

Commit

Permalink
Merge pull request #39 from tukcomCD2024/ARCH-114-fix/jwt
Browse files Browse the repository at this point in the history
fix: jwt 인증 필터
  • Loading branch information
GaBaljaintheroom authored Jan 17, 2024
2 parents 9696aff + aed2949 commit 81a8f05
Show file tree
Hide file tree
Showing 22 changed files with 315 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import site.timecapsulearchive.core.domain.auth.dto.response.OAuthUrlResponse;
import site.timecapsulearchive.core.domain.auth.dto.response.TemporaryTokenResponse;
import site.timecapsulearchive.core.domain.auth.dto.response.TokenResponse;
import site.timecapsulearchive.core.global.common.response.ApiSpec;

public interface AuthApi {

Expand Down Expand Up @@ -111,18 +112,14 @@ public interface AuthApi {
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "ok",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = TemporaryTokenResponse.class)
)
description = "ok"
)
})
@GetMapping(
value = "/sign-up",
produces = {"application/json"}
)
ResponseEntity<TemporaryTokenResponse> signUpWithSocialProvider(SignUpRequest request);
ResponseEntity<ApiSpec<TemporaryTokenResponse>> signUpWithSocialProvider(SignUpRequest request);

@Operation(
summary = "액세스 토큰 재발급",
Expand All @@ -132,19 +129,15 @@ public interface AuthApi {
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "ok",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = TokenResponse.class)
)
description = "ok"
)
})
@PostMapping(
value = "/token/re-issue",
produces = {"application/json"},
consumes = {"application/json"}
)
ResponseEntity<TokenResponse> reIssueAccessToken(TokenReIssueRequest request);
ResponseEntity<ApiSpec<TokenResponse>> reIssueAccessToken(TokenReIssueRequest request);


@Operation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import site.timecapsulearchive.core.domain.auth.service.TokenService;
import site.timecapsulearchive.core.domain.member.dto.mapper.MemberMapper;
import site.timecapsulearchive.core.domain.member.service.MemberService;
import site.timecapsulearchive.core.global.common.response.ApiSpec;
import site.timecapsulearchive.core.global.common.response.SuccessCode;

@RestController
@RequiredArgsConstructor
Expand Down Expand Up @@ -45,17 +47,29 @@ public ResponseEntity<TemporaryTokenResponse> getTemporaryTokenResponseByGoogle(
}

@Override
public ResponseEntity<TokenResponse> reIssueAccessToken(
@Valid @RequestBody final TokenReIssueRequest request) {
return ResponseEntity.ok(tokenService.reIssueToken(request.refreshToken()));
public ResponseEntity<ApiSpec<TokenResponse>> reIssueAccessToken(
@Valid @RequestBody final TokenReIssueRequest request
) {
return ResponseEntity.ok(
ApiSpec.success(
SuccessCode.SUCCESS,
tokenService.reIssueToken(request.refreshToken())
)
);
}

@Override
public ResponseEntity<TemporaryTokenResponse> signUpWithSocialProvider(
@RequestBody final SignUpRequest request) {
public ResponseEntity<ApiSpec<TemporaryTokenResponse>> signUpWithSocialProvider(
@RequestBody final SignUpRequest request
) {
Long id = memberService.createMember(memberMapper.signUpRequestToEntity(request));

return ResponseEntity.ok(tokenService.createTemporaryToken(id));
return ResponseEntity.ok(
ApiSpec.success(
SuccessCode.SUCCESS,
tokenService.createTemporaryToken(id)
)
);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.validation.annotation.Validated;

@Schema(description = "임시 인증 토큰")
@Validated
public record TemporaryTokenResponse(

@Schema(description = "임시 액세스 토큰")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.validation.annotation.Validated;

@Schema(description = "완전한 인증 토큰")
@Validated
public record TokenResponse(

@Schema(description = "액세스 토큰")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package site.timecapsulearchive.core.domain.auth.exception;

import site.timecapsulearchive.core.global.common.response.ErrorCode;
import site.timecapsulearchive.core.global.error.ErrorCode;
import site.timecapsulearchive.core.global.error.exception.BusinessException;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import site.timecapsulearchive.core.global.common.response.ErrorCode;
import site.timecapsulearchive.core.global.common.response.ErrorResponse;
import site.timecapsulearchive.core.global.error.ErrorCode;
import site.timecapsulearchive.core.global.error.ErrorResponse;

@Slf4j
@Component
@Qualifier("oauth2LoginFailureHandler")
@RequiredArgsConstructor
public class OAuth2LoginFailureHandler implements AuthenticationFailureHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import org.springframework.stereotype.Component;
import site.timecapsulearchive.core.domain.auth.dto.oauth.CustomOAuth2User;
import site.timecapsulearchive.core.domain.auth.service.TokenService;
import site.timecapsulearchive.core.global.common.response.ErrorCode;
import site.timecapsulearchive.core.global.common.response.ErrorResponse;
import site.timecapsulearchive.core.global.error.ErrorCode;
import site.timecapsulearchive.core.global.error.ErrorResponse;

@Slf4j
@Component
Expand All @@ -25,13 +25,6 @@ public class OAuth2LoginSuccessHandler implements AuthenticationSuccessHandler {
private final TokenService tokenService;
private final ObjectMapper objectMapper;

/**
* @param request the request which caused the successful authentication
* @param response the response
* @param authentication the <tt>Authentication</tt> object which was created during the
* authentication process.
* @throws IOException
*/
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import site.timecapsulearchive.core.domain.member.dto.reqeust.MemberDetailUpdateRequest;
import site.timecapsulearchive.core.domain.member.dto.response.MemberDetailResponse;
import site.timecapsulearchive.core.domain.member.dto.response.MemberStatusResponse;
import site.timecapsulearchive.core.global.common.response.ApiSpec;

public interface MemberApi {

Expand Down Expand Up @@ -65,17 +66,13 @@ public interface MemberApi {
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = "ok",
content = @Content(
mediaType = "application/json",
schema = @Schema(implementation = MemberStatusResponse.class)
)
description = "ok"
)
})
@GetMapping(
value = "/status",
value = "/me/status",
produces = {"application/json"}
)
ResponseEntity<MemberStatusResponse> checkStatus(
ResponseEntity<ApiSpec<MemberStatusResponse>> checkStatus(
CheckStatusRequest request);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import site.timecapsulearchive.core.domain.member.dto.response.MemberDetailResponse;
import site.timecapsulearchive.core.domain.member.dto.response.MemberStatusResponse;
import site.timecapsulearchive.core.domain.member.service.MemberService;
import site.timecapsulearchive.core.global.common.response.ApiSpec;
import site.timecapsulearchive.core.global.common.response.SuccessCode;

@RestController
@RequiredArgsConstructor
Expand All @@ -30,12 +32,17 @@ public ResponseEntity<Void> updateMemberById(MemberDetailUpdateRequest request)
}

@Override
public ResponseEntity<MemberStatusResponse> checkStatus(
public ResponseEntity<ApiSpec<MemberStatusResponse>> checkStatus(
@Valid @RequestBody CheckStatusRequest request
) {
return ResponseEntity.ok(memberService.checkStatus(
request.authId(),
request.socialType())
return ResponseEntity.ok(
ApiSpec.success(
SuccessCode.SUCCESS,
memberService.checkStatus(
request.authId(),
request.socialType()
)
)
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package site.timecapsulearchive.core.global.common.response;

import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "api 공통 응답 ")
public record ApiSpec<T>(

@Schema(description = "응답 코드")
String code,

@Schema(description = "응답 메시지")
String message,

@Schema(description = "응답 데이터")
T result
) {

public static <T> ApiSpec<T> empty(SuccessCode code) {
return new ApiSpec<>(code.getMessage(), code.getCode(), null);
}

public static <T> ApiSpec<T> success(SuccessCode code, T result) {
return new ApiSpec<>(code.getCode(), code.getMessage(), result);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package site.timecapsulearchive.core.global.common.response;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum SuccessCode {
//success handle
SUCCESS("00", "요청 처리에 성공했습니다.");

private final String code;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
package site.timecapsulearchive.core.global.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.util.matcher.RequestMatcher;
import site.timecapsulearchive.core.global.security.jwt.JwtAuthenticationFilter;

@RequiredArgsConstructor
public class JwtDsl extends AbstractHttpConfigurer<JwtDsl, HttpSecurity> {

private final AuthenticationConfiguration authenticationConfiguration;
private final AuthenticationProvider jwtAuthenticationProvider;
private final AuthenticationFailureHandler authenticationFailureHandler;
private final ObjectMapper objectMapper;
private final RequestMatcher notRequireAuthenticationMatcher;

public static JwtDsl jwtDsl(
AuthenticationConfiguration authenticationConfiguration,
AuthenticationProvider authenticationProvider,
AuthenticationFailureHandler authenticationEntryPoint
ObjectMapper objectMapper,
RequestMatcher requestMatcher
) {
return new JwtDsl(
authenticationConfiguration,
authenticationProvider,
authenticationEntryPoint
objectMapper,
requestMatcher
);
}

@Override
public void init(HttpSecurity http) throws Exception {
public void configure(HttpSecurity http) {
http
.authenticationProvider(jwtAuthenticationProvider)
.addFilterBefore(
jwtAuthenticationFilter(),
jwtAuthenticationFilter(http.getSharedObject(AuthenticationManager.class)),
UsernamePasswordAuthenticationFilter.class
);
}

private JwtAuthenticationFilter jwtAuthenticationFilter() throws Exception {
private JwtAuthenticationFilter jwtAuthenticationFilter(
AuthenticationManager authenticationManager) {
return new JwtAuthenticationFilter(
authenticationConfiguration.getAuthenticationManager(),
authenticationFailureHandler
authenticationManager,
objectMapper,
notRequireAuthenticationMatcher
);
}
}
Loading

0 comments on commit 81a8f05

Please sign in to comment.