Код: Выделить всё
import io.jsonwebtoken.Jwts;
import org.springframework.stereotype.Service;
import java.security.KeyPair;
import java.sql.Date;
import java.time.DayOfWeek;
import java.time.LocalDate;
@Service
public class JwtTokenService implements TokenService {
private final KeyPair keyPair;
public JwtTokenService(KeyPair keyPair) {
this.keyPair = keyPair;
}
@Override
public String generateTokenFor(User user) {
return Jwts.builder()
.setSubject(user.getName())
.claim(AppClaims.USERID, user.getId())
.setExpiration(Date.valueOf(LocalDate.now()
.plusDays(DayOfWeek.values().length)))
.signWith(keyPair.getPrivate())
.compact();
}
}
< /code>
io.jsonwebtoken
jjwt-api
${jsonwebtoken.version}
io.jsonwebtoken
jjwt-impl
${jsonwebtoken.version}
runtime
io.jsonwebtoken
jjwt-jackson
${jsonwebtoken.version}
runtime
Код: Выделить всё
@Bean
@RouterOperation(beanClass = TokenHandler.class, beanMethod = "jwks")
public RouterFunction jwks() {
return RouterFunctions.route()
.GET("/oauth2/jwks", tokenHandler::jwks)
.build();
}
< /code>
@Override
@ApiResponse(responseCode = "200", content = @Content(mediaType = "application/json"))
public Mono jwks(ServerRequest request) {
return Mono.just(keyPair.getPublic())
.ofType(RSAPublicKey.class) // these are Nimbus types
.map(RSAKey.Builder::new)
.map(RSAKey.Builder::build)
.map(JWKSet::new)
.map(JWKSet::toJSONObject)
.transform(jwks -> ServerResponse.status(HttpStatus.OK).body(jwks, Map.class));
}
< /code>
com.nimbusds
nimbus-jose-jwt
10.3
Код: Выделить всё
// what parsing looks like with io.jsonwebtoken
Claims jwtClaims = Jwts.parserBuilder()
.setSigningKey(keyPair.getPublic())
.build()
.parseClaimsJws(jwt)
.getBody();
Подробнее здесь: https://stackoverflow.com/questions/796 ... ith-nimbus