Skip to content

Commit

Permalink
Merge pull request #36 from K-Hackathon-Fledge/34-refactoring-token-a…
Browse files Browse the repository at this point in the history
…uth-refactoring

34 refactoring token auth refactoring
  • Loading branch information
gaguriee authored Aug 9, 2024
2 parents 2428b5b + df2dcb0 commit 325c55b
Show file tree
Hide file tree
Showing 24 changed files with 368 additions and 264 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ dependencies {


// JWT
implementation 'io.jsonwebtoken:jjwt-api:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.3'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.3'
implementation 'io.jsonwebtoken:jjwt-api:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.2'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.2'

// AWS
implementation 'io.awspring.cloud:spring-cloud-starter-aws:2.4.4'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.fledge.fledgeserver.auth.controller;

import com.fledge.fledgeserver.auth.dto.TokenResponse;
import com.fledge.fledgeserver.auth.service.AuthService;
import com.fledge.fledgeserver.response.ApiResponse;
import com.fledge.fledgeserver.response.SuccessStatus;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "인증 관련 API", description = "인증과 관련된 API")
@Slf4j
@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
public class AuthController {

private final AuthService authService;

@Operation(summary = "로그아웃", description = "현재 사용자를 로그아웃 합니다.")
@PostMapping("/logout")
public ResponseEntity<ApiResponse<String>> logout(HttpServletRequest request, HttpServletResponse response) {
authService.logout(request, response);
return ApiResponse.success(SuccessStatus.LOGOUT_SUCCESS);
}

@Operation(summary = "토큰 재발급", description = "만료된 JWT 토큰을 재발급 합니다.")
@GetMapping(value = "/tokenRefresh", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ApiResponse<TokenResponse>> refresh() {
TokenResponse tokenResponse = authService.refreshToken();
return ApiResponse.success(SuccessStatus.TOKEN_REFRESH_SUCCESS, tokenResponse);
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/fledge/fledgeserver/auth/dto/TokenResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fledge.fledgeserver.auth.dto;

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

@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
public class TokenResponse {

@Schema(description = "액세스 토큰", example = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...")
private String accessToken;

@Schema(description = "리프레시 토큰", example = "dGhpc2lzYXJlZnJlc2h0b2tlbg==")
private String refreshToken;

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fledge.fledgeserver.auth.handler;

import com.fledge.fledgeserver.auth.dto.TokenResponse;
import com.fledge.fledgeserver.auth.jwt.TokenProvider;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -21,11 +22,11 @@ public class OAuth2SuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {
String accessToken = tokenProvider.generateAccessToken(authentication);
tokenProvider.generateRefreshToken(authentication, accessToken);
TokenResponse tokenResponse = tokenProvider.createToken(authentication);

String redirectUrl = UriComponentsBuilder.fromUriString(oauthRedirectUrl)
.queryParam("accessToken", accessToken)
.queryParam("accessToken", tokenResponse.getAccessToken())
.queryParam("refreshToken", tokenResponse.getRefreshToken())
.build().toUriString();

response.sendRedirect(redirectUrl);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.fledge.fledgeserver.auth.jwt;

public class JwtConstants {
public static final String BEARER_PREFIX = "Bearer ";
public static final String AUTHORITIES = "role";
}
31 changes: 0 additions & 31 deletions src/main/java/com/fledge/fledgeserver/auth/jwt/Token.java

This file was deleted.

Loading

0 comments on commit 325c55b

Please sign in to comment.