gpt4 book ai didi

带有加密 JWT 访问 token 的 Spring Boot OAuth2

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

在我的 Spring Bott 应用程序中,我使用授权/资源服务器配置了自己的 OAuth2。

我已经实现了以下 JwtAccessTokenConverter:

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter() {

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DBUserDetails user = (DBUserDetails) authentication.getUserAuthentication().getPrincipal();
final Map<String, Object> additionalInfo = new HashMap<>();
additionalInfo.put("user_id", user.getUser().getId());
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInfo);
OAuth2AccessToken enhancedToken = super.enhance(accessToken, authentication);
return enhancedToken;
}

};

converter.setSigningKey("123");

DefaultAccessTokenConverter accessTokenConverter = new DefaultAccessTokenConverter();
DefaultUserAuthenticationConverter userTokenConverter = new DefaultUserAuthenticationConverter();
userTokenConverter.setUserDetailsService(userDetailsService);
accessTokenConverter.setUserTokenConverter(userTokenConverter);

converter.setAccessTokenConverter(accessTokenConverter);

return converter;
}

现在我的应用程序生成以下 token ,例如:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxNzgzNTEsInVzZXJfbmFtZSI6ImFkbWluIiwic2NvcGUiOlsicmVhZCIsIndyaXRlIl0sImV4cCI6MTQ3NTA5NDA4NSwiYXV0aG9yaXRpZXMiOlsiUEVSTUlTU0lPTl9ERUxFVEVfT1dOX0NSSVRFUklPTiIsIlBFUk1JU1NJT05fREVMRVRFX09XTl9DT01NRU5UIiwiUEVSTUlTU0lPTl9VUERBVEVfT1dOX0NSSVRFUklPTiIsIlBFUk1JU1NJT05fVVBEQVRFX0FOWV9DUklURVJJT04iLCJQRVJNSVNTSU9OX0RFTEVURV9PV05fQ1JJVEVSSU9OX0dST1VQIiwiUEVSTUlTU0lPTl9ERUxF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

可以在此处使用 JWT 调试器解码此 token https://jwt.io/

我不想将此 token 的内部结构暴露给外部世界,并希望以某种方式对此 token 进行编码。

如何使用 Spring Boot、OAuth2、JWT 实现?

最佳答案

我试试这个,它对我有用:https://gist.github.com/salgmachine/352799a6052b02901982dcbf85d30346
创建自定义 JwtAccessTokenConverter

public class JwtJweAccessTokenConverter extends JwtAccessTokenConverter {

RSAKey recipientJWK, recipientPublicJWK;

public JwtJweAccessTokenConverter() {
try {
recipientJWK = new RSAKeyGenerator(2048).keyID("456").keyUse(KeyUse.ENCRYPTION).generate();
recipientPublicJWK = recipientJWK.toPublicJWK();
} catch (JOSEException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
protected String encode(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
String jwt = super.encode(accessToken, authentication);

try {
// jwt is already signed at this point (by JwtAccessTokenConverter)
SignedJWT parsed = SignedJWT.parse(jwt);

// Create JWE object with signed JWT as payload
JWEObject jweObject = new JWEObject(
new JWEHeader.Builder(JWEAlgorithm.RSA_OAEP_256, EncryptionMethod.A256GCM).contentType("JWT") // required
// to
// indicate
// nested
// JWT
.build(),
new Payload(parsed));

// Encrypt with the recipient's public key
jweObject.encrypt(new RSAEncrypter(recipientPublicJWK));

// Serialise to JWE compact form
String jweString = jweObject.serialize();

return jweString;
} catch (Exception e) {
e.printStackTrace();
}

return jwt;
}

@Override
protected Map<String, Object> decode(String token) {
try {
// basically treat the incoming token as an encrypted JWT
EncryptedJWT parse = EncryptedJWT.parse(token);
// decrypt it
RSADecrypter dec = new RSADecrypter(recipientJWK);
parse.decrypt(dec);
// content of the encrypted token is a signed JWT (signed by
// JwtAccessTokenConverter)
SignedJWT signedJWT = parse.getPayload().toSignedJWT();
// pass on the serialized, signed JWT to JwtAccessTokenConverter
return super.decode(signedJWT.serialize());

} catch (ParseException e) {
e.printStackTrace();
} catch (JOSEException e) {
e.printStackTrace();
}

return super.decode(token);
}
}
并配置您的 Oauth2 身份验证服务器和资源以使用您的自定义 JwtAccessTokenConverter
@Bean
public TokenStore tokenStore() {

return new JwtTokenStore(accessTokenConverter());
}

@Bean
public JwtAccessTokenConverter accessTokenConverter() {
final JwtAccessTokenConverter converter = new JwtJweAccessTokenConverter();
final KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(new ClassPathResource("mytest.jks"),
"mypass".toCharArray());
converter.setKeyPair(keyStoreKeyFactory.getKeyPair("mytest"));
return converter;
}
检查 github 链接以获取完整的代码示例

关于带有加密 JWT 访问 token 的 Spring Boot OAuth2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39742182/

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