Skip to content

Commit

Permalink
Refactor | #4 | @lcomment | 인증 Provider 관련 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
lcomment committed May 3, 2024
1 parent 123aaf6 commit b8533e8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 11 deletions.
17 changes: 12 additions & 5 deletions cakk-api/src/main/java/com/cakk/api/provider/jwt/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
Expand All @@ -32,14 +34,18 @@ public class JwtProvider {

@Value("${jwt.expiration.access-token}")
private Long accessTokenExpiredSecond;
@Value("${jwt.expiration.access-token}")
@Value("${jwt.expiration.refresh-token}")
private Long refreshTokenExpiredSecond;
@Value("${jwt.grant-type}")
private String grantType;
@Value("${jwt.user-key}")
private String userKey;

public JsonWebToken generateToken(final User user) {
if (isNull(user)) {
throw new CakkException(EMPTY_USER);
}

final String accessToken = Jwts.builder()
.claim(userKey, user)
.setExpiration(new Date(System.currentTimeMillis() + accessTokenExpiredSecond))
Expand All @@ -58,14 +64,15 @@ public JsonWebToken generateToken(final User user) {
.build();
}

public Authentication getAuthentication(String token) {
final Claims claims = parseClaims(token);
public Authentication getAuthentication(String accessToken) {
final Claims claims = parseClaims(accessToken);

if (isNull(claims.get(userKey)) || !(claims.get(userKey) instanceof User)) {
if (isNull(claims.get(userKey))) {
throw new CakkException(EMPTY_AUTH_JWT);
}

OAuthUserDetails userDetails = new OAuthUserDetails((User) claims.get(userKey));
final User user = new ObjectMapper().convertValue(claims.get(userKey), User.class);
OAuthUserDetails userDetails = new OAuthUserDetails(user);

return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.cakk.api.provider.oauth.impl;

import java.io.IOException;
import java.security.PublicKey;

import org.springframework.stereotype.Component;
Expand Down
13 changes: 13 additions & 0 deletions cakk-api/src/main/resources/app-banner.dat
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
==========================================

,-----. ,---. ,--. ,--.,--. ,--.
' .--./ / O \ | .' /| .' /
| | | .-. || . ' | . '
' '--'\| | | || |\ \| |\ \
`-----'`--' `--'`--' '--'`--' '--'

${spring.application.title} ${spring.application.version}
Powered by Spring Boot ${spring-boot.version}

==========================================

10 changes: 10 additions & 0 deletions cakk-api/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
jwt:
secret: localocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocalocal
expiration:
access-token: 1814400000
refresh-token: 2592000000

slack:
webhook:
is-enable: false
url: url
46 changes: 41 additions & 5 deletions cakk-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
spring:
profiles:
default: ${APPLICATION_PROFILE}
application:
title: Cakk
version: 1.0.0
banner:
location: classpath:/app-banner.dat
config:
import:
- db-${spring.profiles.default}.yml
servlet:
multipart:
max-file-size: 50MB
max-request-size: 50MB
jpa:
open-in-view: false
hibernate:
ddl-auto: update
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
format_sql: true
show_sql: true
flyway:
enabled: true
baseline-on-migrate: true

storage:
datasource:
core:
jdbc-url: jdbc:mysql://localhost:3306/cakk
username: ${DB_USERNAME}
password: ${DB_PASSWORD}
driver-class-name: com.mysql.cj.jdbc.Driver
data-source-properties:
rewriteBatchedStatements: true

decorator:
datasource:
p6spy:
enable-logging: true

oauth:
kakao:
public-key-info: https://kauth.kakao.com/.well-known/jwks.json
apple:
public-key-url: https://appleid.apple.com/auth/keys
google:
client-id: ${GOOGLE_CLIENT_ID}

jwt:
access-header: Authorization
refresh-header: Refresh
grant-type: Bearer
user-key: USER
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ReturnCode {
WRONG_JWT_TOKEN("1101", "잘못된 jwt 토큰입니다."),
EXPIRED_JWT_TOKEN("1102", "만료된 jwt 토큰입니다."),
EMPTY_AUTH_JWT("1103", "인증 정보가 비어있는 jwt 토큰입니다."),
EMPTY_USER("1104", "비어있는 유저 정보로 jwt 토큰을 생성할 수 없습니다."),

// 서버 에러 (9998, 9999)
INTERNAL_SERVER_ERROR("9998", "내부 서버 에러 입니다."),
Expand Down

0 comments on commit b8533e8

Please sign in to comment.