-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Link-MIND/test
[Merge] ํ์ฌ๊น์ง ๋ณ๊ฒฝ์ฌํญ develop ๋ธ๋์น ๋ฐ์
- Loading branch information
Showing
26 changed files
with
648 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.app.toaster.config; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface UserId { | ||
} |
45 changes: 45 additions & 0 deletions
45
linkmind/src/main/java/com/app/toaster/config/UserIdResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.app.toaster.config; | ||
|
||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
import com.app.toaster.config.jwt.JwtService; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class UserIdResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final JwtService jwtService; | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(UserId.class) && Long.class.equals(parameter.getParameterType()); | ||
} | ||
|
||
@Override | ||
public Object resolveArgument(@NotNull MethodParameter parameter, ModelAndViewContainer modelAndViewContainer, @NotNull NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
final HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
final String token = request.getHeader("accessToken"); | ||
|
||
// ํ ํฐ ๊ฒ์ฆ | ||
if (!jwtService.verifyToken(token)) { | ||
throw new RuntimeException(String.format("USER_ID๋ฅผ ๊ฐ์ ธ์ค์ง ๋ชปํ์ต๋๋ค. (%s - %s)", parameter.getClass(), parameter.getMethod())); | ||
} | ||
|
||
// ์ ์ ์์ด๋ ๋ฐํ | ||
final String tokenContents = jwtService.getJwtContents(token); | ||
try { | ||
return Long.parseLong(tokenContents); | ||
} catch (NumberFormatException e) { | ||
throw new RuntimeException(String.format("USER_ID๋ฅผ ๊ฐ์ ธ์ค์ง ๋ชปํ์ต๋๋ค. (%s - %s)", parameter.getClass(), parameter.getMethod())); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
linkmind/src/main/java/com/app/toaster/config/jwt/JwtService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.app.toaster.config.jwt; | ||
|
||
|
||
import static io.jsonwebtoken.Jwts.*; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Base64; | ||
import java.util.Date; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.app.toaster.exception.Error; | ||
import com.app.toaster.exception.model.NotFoundException; | ||
import com.app.toaster.exception.model.UnauthorizedException; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.ExpiredJwtException; | ||
import io.jsonwebtoken.Header; | ||
import io.jsonwebtoken.security.Keys; | ||
import jakarta.annotation.PostConstruct; | ||
|
||
@Service | ||
public class JwtService { | ||
|
||
@Value("${jwt.secret}") | ||
private String jwtSecret; | ||
|
||
@PostConstruct | ||
protected void init() { | ||
jwtSecret = Base64.getEncoder() | ||
.encodeToString(jwtSecret.getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
// JWT ํ ํฐ ๋ฐ๊ธ | ||
public String issuedToken(String userId, Long tokenExpirationTime) { | ||
final Date now = new Date(); | ||
|
||
// ํด๋ ์ ์์ฑ | ||
final Claims claims = claims() | ||
.setSubject("token") | ||
.setIssuedAt(now) | ||
.setExpiration(new Date(now.getTime() + tokenExpirationTime)); | ||
|
||
//private claim ๋ฑ๋ก | ||
claims.put("userId", userId); | ||
|
||
return builder() | ||
.setHeaderParam(Header.TYPE , Header.JWT_TYPE) | ||
.setClaims(claims) | ||
.signWith(getSigningKey()) | ||
.compact(); | ||
} | ||
|
||
private SecretKey getSigningKey() { | ||
final byte[] keyBytes = jwtSecret.getBytes(StandardCharsets.UTF_8); | ||
return Keys.hmacShaKeyFor(keyBytes); | ||
} | ||
|
||
// JWT ํ ํฐ ๊ฒ์ฆ | ||
public boolean verifyToken(String token) { | ||
try { | ||
final Claims claims = getBody(token); | ||
return true; | ||
} catch (RuntimeException e) { | ||
if (e instanceof ExpiredJwtException) { | ||
throw new UnauthorizedException(Error.TOKEN_TIME_EXPIRED_EXCEPTION, Error.TOKEN_TIME_EXPIRED_EXCEPTION.getMessage()); | ||
} | ||
throw new NotFoundException(Error.NOT_FOUND_USER_EXCEPTION, Error.NOT_FOUND_USER_EXCEPTION.getMessage()); | ||
} | ||
} | ||
|
||
private Claims getBody(final String token) { | ||
return parserBuilder() | ||
.setSigningKey(getSigningKey()) | ||
.build() | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
} | ||
|
||
// JWT ํ ํฐ ๋ด์ฉ ํ์ธ | ||
public String getJwtContents(String token) { | ||
final Claims claims = getBody(token); | ||
return (String) claims.get("userId"); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
linkmind/src/main/java/com/app/toaster/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.app.toaster.controller; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import com.app.toaster.common.dto.ApiResponse; | ||
import com.app.toaster.config.UserId; | ||
import com.app.toaster.controller.request.auth.SignInRequestDto; | ||
import com.app.toaster.controller.response.auth.SignInResponseDto; | ||
import com.app.toaster.controller.response.auth.TokenResponseDto; | ||
import com.app.toaster.exception.Success; | ||
import com.app.toaster.service.auth.AuthService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/auth") | ||
public class AuthController { | ||
private final AuthService authService; | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
public ApiResponse<SignInResponseDto> signIn( | ||
@RequestHeader("Authorization") String socialAccessToken, | ||
@RequestBody SignInRequestDto requestDto | ||
) { | ||
return ApiResponse.success(Success.LOGIN_SUCCESS, authService.signIn(socialAccessToken, requestDto)); | ||
} | ||
|
||
@PostMapping("/token") | ||
@ResponseStatus(HttpStatus.OK) | ||
public ApiResponse<TokenResponseDto> reissueToken(@RequestHeader String refreshToken) { | ||
return ApiResponse.success(Success.RE_ISSUE_TOKEN_SUCCESS, authService.issueToken(refreshToken)); | ||
} | ||
|
||
@PostMapping("/sign-out") | ||
@ResponseStatus(HttpStatus.OK) | ||
public ApiResponse signOut(@UserId Long userId) { | ||
authService.signOut(userId); | ||
return ApiResponse.success(Success.SIGNOUT_SUCCESS); | ||
} | ||
|
||
@DeleteMapping("/withdraw") | ||
@ResponseStatus(HttpStatus.OK) | ||
public ApiResponse withdraw(@UserId Long userId){ | ||
authService.withdraw(userId); | ||
return ApiResponse.success(Success.DELETE_USER_SUCCESS); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
linkmind/src/main/java/com/app/toaster/controller/request/auth/SignInRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.app.toaster.controller.request.auth; | ||
|
||
public record SignInRequestDto(String socialType, String fcmToken) { | ||
} |
8 changes: 8 additions & 0 deletions
8
linkmind/src/main/java/com/app/toaster/controller/response/auth/SignInResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.app.toaster.controller.response.auth; | ||
|
||
public record SignInResponseDto(Long userId, String accessToken, String refreshToken, String fcmToken, Boolean isRegistered,Boolean FcmIsAllowed) { | ||
public static SignInResponseDto of(Long userId, String accessToken, String refreshToken, String fcmToken, | ||
Boolean isRegistered, Boolean fcmIsAllowed){ | ||
return new SignInResponseDto(userId,accessToken, refreshToken,fcmToken,isRegistered,fcmIsAllowed); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
linkmind/src/main/java/com/app/toaster/controller/response/auth/TokenResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.app.toaster.controller.response.auth; | ||
|
||
public record TokenResponseDto(String accessToken, String refreshToken) { | ||
public static TokenResponseDto of(String accessToken, String refreshToken){ | ||
return new TokenResponseDto(accessToken,refreshToken); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
linkmind/src/main/java/com/app/toaster/exception/model/UnauthorizedException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.app.toaster.exception.model; | ||
|
||
import com.app.toaster.exception.Error; | ||
|
||
public class UnauthorizedException extends CustomException{ | ||
public UnauthorizedException(Error error, String message) { | ||
super(error, message); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
linkmind/src/main/java/com/app/toaster/exception/model/UnprocessableEntityException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.app.toaster.exception.model; | ||
|
||
import com.app.toaster.exception.Error; | ||
|
||
public class UnprocessableEntityException extends CustomException{ | ||
public UnprocessableEntityException(Error error, String message) { | ||
super(error, message); | ||
} | ||
} |
Empty file.
22 changes: 22 additions & 0 deletions
22
linkmind/src/main/java/com/app/toaster/infrastructure/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.app.toaster.infrastructure; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import com.app.toaster.domain.SocialType; | ||
import com.app.toaster.domain.User; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
Boolean existsBySocialIdAndSocialType(String socialId, SocialType socialType); | ||
|
||
Optional<User> findByUserId(Long userId); | ||
|
||
Optional<User> findBySocialIdAndSocialType(String socialId, SocialType socialType); | ||
|
||
Boolean existsByNickname(String s); | ||
|
||
Optional<User> findByRefreshToken(String refreshToken); | ||
|
||
Long deleteByUserId(Long userId); | ||
} |
Oops, something went wrong.