Skip to content

Commit

Permalink
Merge pull request #6 from sazzeo/mas-toy-project/1
Browse files Browse the repository at this point in the history
jwt token bind error 수정  , toy project 마무리
  • Loading branch information
sazzeo authored Aug 9, 2022
2 parents f3a316d + 542dc77 commit 7f202ee
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
3 changes: 3 additions & 0 deletions d-gateway/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway:3.1.3'
implementation 'io.jsonwebtoken:jjwt:0.9.1'

//jwt토큰 parsing 하는데 Base64로 인코딩하는데 자바 11버전 부터는 기본 설정이 되지 않아 추가해야함.
implementation 'javax.xml.bind:jaxb-api:2.3.1'
}

//버전 알아서 찾아주는 애
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public GatewayFilter apply(Config config) {

}


//Mono 객체 : webflux에서 데이터 전달하는 객체임
//여기서 설정하는 httpstatus가 실제 response 되는 status가 됨.
private Mono<Void> onError( ServerHttpResponse response , String err, HttpStatus httpStatus) {
response.setStatusCode(httpStatus);
log.error(err);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.gateway.security;

import com.example.gateway.vo.UserDto;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
Expand All @@ -14,7 +16,10 @@ public class JwtTokenParser {

public String getTokenSubject(String token) {

String subject = Jwts.parser().parseClaimsJws(token).getBody().getSubject();
String subject = Jwts.parser()
.setSigningKey(SECRET_KEY)
.parseClaimsJws(token)
.getBody().getSubject();
return subject;

}
Expand All @@ -32,11 +37,14 @@ public boolean isBearerToken(String bearerToken) {
public boolean isValidToken(String validBearerToken) {

try {
String subject = Jwts.parser().parseClaimsJws(validBearerToken).getBody().getSubject();
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(SECRET_KEY)
.parseClaimsJws(validBearerToken);
return true;
} catch (Exception e) {
log.info(e.getMessage());
return false;
}
return true;
}

public String getToken(String validBearerToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public String getBearerToken(UserDto userDto) {

String token = Jwts.builder()
.setSubject(userDto.getUserId()) //아이디 설정
.setIssuedAt(new Date())
.setExpiration(new Date(now.getTime() + EXPIRATION_TIME)) //만료 시간
.signWith(SignatureAlgorithm.HS512 , SECRET_KEY)
.compact();
Expand Down

0 comments on commit 7f202ee

Please sign in to comment.