Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jwt token bind error 수정 , toy project 마무리 #6

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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