gpt4 book ai didi

java - 验证签名的 JWT

转载 作者:行者123 更新时间:2023-12-02 03:34:25 26 4
gpt4 key购买 nike

我正在使用私钥(授权服务器)签署 JWT,并且我正在使用公钥(资源服务器)来“验证”它......我如何知道 JWT 是否未被泄露?或者我该怎么做?

代码来自资源服务器

       JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
Resource resource = new ClassPathResource("public.txt");
String publicKey = null;
try {
publicKey = IOUtils.toString(resource.getInputStream());
} catch (final IOException e) {
throw new RuntimeException(e);
}
converter.setVerifierKey(publicKey);

return converter;
}

最佳答案

Spring Security将根据授权服务器中的配置对 token 进行验证。

对于独立验证,代码如下:

  RsaVerifier verifier = new RsaVerifier(RSAPublicKey);
Jwt tokenDecoded = JwtHelper.decodeAndVerify(token, verifier);
Map<String, Object> claimsMap = (Map<String, Object>) new
ObjectMapper().readValue(tokenDecoded.getClaims(), Map.class);
//Verify the claims then
// 1 Verify if the token has not already expired
// 2 Verify the issuance date ( should be before this date )
// 3 Verify if the issuer of this token is contained in verified authorities.
// 4 Verify if the token was issued for this client
// 5 Verify if the token contained any expected claims...

但是上面是Spring Security针对Oauth2认证过程实现的,客户端应用程序只需要提供配置即可。

触发器是OAuth2AuthenticationProcessingFilter在 Spring security 过滤器链中。当资源受 Oauth2 安全保护时,会添加此过滤器。

在您的应用程序中,授权服务器配置如下所示(下面仅摘录相关的指示性配置)

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

...
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
RSAPemKeyPairLoader keyPairLoader = new RSAPemKeyPairLoader();
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(...);
converter.setVerifierKey(...);
return converter;
}

@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
defaultTokenServices.setSupportRefreshToken(...);
defaultTokenServices.setAccessTokenValiditySeconds(...);
defaultTokenServices.setRefreshTokenValiditySeconds(...);
return defaultTokenServices;
}
}

在您的应用程序中,资源服务器配置如下:

@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
...
}
}

要在 Spring 实现中跟踪请求的 token 被拦截和验证的情况,请查看下面的 Spring OAUTH2 实现 - 流程详细信息,其中将尝试为成功的请求创建 Authentication 对象(即 OAuth2Authentication 的实例)。

All below Code extracts are from spring-security-oauth2-2.0.8.RELEASE implementations.

public class OAuth2AuthenticationManager implements AuthenticationManager {

....
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

if (authentication == null) {
throw new InvalidTokenException("Invalid token (token not found)");
}
String token = (String) authentication.getPrincipal();
OAuth2Authentication auth = tokenServices.loadAuthentication(token);
...
}
}

loadAuthentication 基本上会验证访问 token 并尝试将其转换为 OAuth2Authentication

public class DefaultTokenServices implements AuthorizationServerTokenServices ...{

public OAuth2Authentication loadAuthentication(String accessTokenValue) throws AuthenticationException, InvalidTokenException {

OAuth2AccessToken accessToken = tokenStore.readAccessToken(accessTokenValue);
...
}
}

JwtTokenStore 将创建 OAuth2AccessToken 并在此过程中解码并验证字符串 token 。

    public class JwtTokenStore implements TokenStore {

public OAuth2AccessToken readAccessToken(String tokenValue) {
OAuth2AccessToken accessToken = convertAccessToken(tokenValue);
if (jwtTokenEnhancer.isRefreshToken(accessToken)) {
throw new InvalidTokenException("Encoded token is a refresh token");
}
return accessToken;
}

private OAuth2AccessToken convertAccessToken(String tokenValue) {
return jwtTokenEnhancer.extractAccessToken(tokenValue, jwtTokenEnhancer.decode(tokenValue));
}

}

JWTAccessTokenConverter 进行 token 声明的解码和提取。

public class JwtAccessTokenConverter implements AccessTokenConverter {

protected Map<String, Object> decode(String token) {
try {
Jwt jwt = JwtHelper.decodeAndVerify(token, verifier);
String content = jwt.getClaims();
Map<String, Object> map = objectMapper.parseMap(content);
if (map.containsKey(EXP) && map.get(EXP) instanceof Integer) {
Integer intValue = (Integer) map.get(EXP);
map.put(EXP, new Long(intValue));
}
return map;
}
catch (Exception e) {
throw new InvalidTokenException("Cannot convert access token to JSON", e);
}
}

JwtHelper 将进行解码和请求验证。

public static Jwt decodeAndVerify(String token, SignatureVerifier verifier) {
Jwt jwt = decode(token);
jwt.verifySignature(verifier);
return jwt;
}

JwttImpl 调用 validator 。

public void verifySignature(SignatureVerifier verifier) {
verifier.verify(signingInput(), crypto);
}

例如,RSA签名 validator 最终会进行验证:

public class RsaVerifier implements SignatureVerifier {

public void verify(byte[] content, byte[] sig) {
try {
Signature signature = Signature.getInstance(algorithm);
signature.initVerify(key);
signature.update(content);

if (!signature.verify(sig)) {
throw new InvalidSignatureException("RSA Signature did not match content");
}
}
catch (GeneralSecurityException e) {
throw new RuntimeException(e);
}
}
}

关于java - 验证签名的 JWT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56868546/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com