Skip to content

Commit

Permalink
Merge pull request #191 from Kusitms-28th-MeetUp-C/feature/159-global
Browse files Browse the repository at this point in the history
[refactor]:Socket #159 코드 정렬 및 불필요 메서드 제거
  • Loading branch information
RyuKwanKon committed May 3, 2024
2 parents 2d2fa6e + 9ee3d70 commit eb3f5f1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
3 changes: 3 additions & 0 deletions SocketService/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ dependencies {

//mongoDB
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

// Spring Messaging for STOMP over WebSocket
implementation 'org.springframework:spring-messaging:5.3.18'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.kusitms.socketservice;

import com.kusitms.socketservice.global.common.JwtProvider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SocketServiceApplication {

public static void main(String[] args) {
JwtProvider jwtProvider = new JwtProvider();

System.out.println(jwtProvider.generateToken(1L, true));
SpringApplication.run(SocketServiceApplication.class, args);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.kusitms.socketservice.global.common;

import com.kusitms.socketservice.global.error.httpException.UnauthorizedException;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.util.Base64;
import java.util.Date;

import static com.kusitms.socketservice.global.error.ErrorCode.EXPIRED_ACCESS_TOKEN;
import static com.kusitms.socketservice.global.error.ErrorCode.INVALID_ACCESS_TOKEN_VALUE;
Expand Down Expand Up @@ -52,5 +51,17 @@ private Key getSigningKey() {
String encoded = Base64.getEncoder().encodeToString(secretKey.getBytes());
return Keys.hmacShaKeyFor(encoded.getBytes());
}

public String generateToken(Long userId, boolean isAccessToken) {
final Date now = new Date();
final Date expiration = new Date(now.getTime() + (isAccessToken ? ACCESS_TOKEN_EXPIRE_TIME : REFRESH_TOKEN_EXPIRE_TIME));
return Jwts.builder()
.setHeaderParam(Header.TYPE, Header.JWT_TYPE)
.setSubject(String.valueOf(userId))
.setIssuedAt(now)
.setExpiration(expiration)
.signWith(getSigningKey(), SignatureAlgorithm.HS256)
.compact();
}
}

0 comments on commit eb3f5f1

Please sign in to comment.