Skip to content

Commit

Permalink
Task 54 : Implement JwtRecordConverter in product service
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Jul 19, 2024
1 parent 52ebd2f commit 6257b8c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.springbootmicroservices.productservice.filter;

import com.springbootmicroservices.productservice.client.UserServiceClient;
import com.springbootmicroservices.productservice.model.auth.JwtRecord;
import com.springbootmicroservices.productservice.model.auth.Token;
import feign.FeignException;
import jakarta.servlet.FilterChain;
Expand All @@ -14,6 +15,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

Expand Down Expand Up @@ -67,4 +69,10 @@ protected void doFilterInternal(@NonNull final HttpServletRequest httpServletReq
// Proceed with the filter chain in any case
filterChain.doFilter(httpServletRequest, httpServletResponse);
}


private Jwt convertJwtRecordToJwt(JwtRecord jwtRecord) {
return new Jwt(jwtRecord.tokenValue(), jwtRecord.issuedAt(), jwtRecord.expiresAt(), jwtRecord.headers(), jwtRecord.claims());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.springbootmicroservices.productservice.model.auth.JwtRecord;
import com.springbootmicroservices.productservice.utils.JwtRecordConverter;
import lombok.RequiredArgsConstructor;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
Expand Down Expand Up @@ -35,7 +36,10 @@ public UsernamePasswordAuthenticationToken deserialize(JsonParser p, Deserializa

// Extract the nested principal object and deserialize it into a JwtRecord object
JsonNode principalNode = node.get("principal");
JwtRecord principal = objectMapper.treeToValue(principalNode, JwtRecord.class);
JwtRecord jwtRecord = objectMapper.treeToValue(principalNode, JwtRecord.class);

// Convert JwtRecord to Jwt
Jwt principal = JwtRecordConverter.convertJwtRecordToJwt(jwtRecord);

// Extracting the credentials
String credentials = node.get("credentials").isNull() ? null : node.get("credentials").asText();
Expand All @@ -50,4 +54,5 @@ public UsernamePasswordAuthenticationToken deserialize(JsonParser p, Deserializa

return new UsernamePasswordAuthenticationToken(principal, credentials, authorities);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.springbootmicroservices.productservice.utils;

import com.springbootmicroservices.productservice.model.auth.JwtRecord;
import lombok.experimental.UtilityClass;
import org.springframework.security.oauth2.jwt.Jwt;

@UtilityClass
public class JwtRecordConverter {

public JwtRecord convertJwtToJwtRecord(Jwt jwt) {
return new JwtRecord(
jwt.getTokenValue(),
jwt.getHeaders(),
jwt.getClaims(),
jwt.getIssuedAt(),
jwt.getExpiresAt(),
jwt.getClaimAsString("sub"),
jwt.getClaimAsString("iss"),
jwt.getAudience().toString()
);
}

public Jwt convertJwtRecordToJwt(JwtRecord jwtRecord) {
return new Jwt(
jwtRecord.tokenValue(),
jwtRecord.issuedAt(),
jwtRecord.expiresAt(),
jwtRecord.headers(),
jwtRecord.claims()
);
}
}

0 comments on commit 6257b8c

Please sign in to comment.