Java JWT解码与Spring安全
我对JWT解码有问题。 我正在为oauth2授权服务编写一个集成。 我发送请求以获取授权令牌,并得到如下响应:Java JWT解码与Spring安全,java,spring-boot,spring-security,spring-security-oauth2,Java,Spring Boot,Spring Security,Spring Security Oauth2,我对JWT解码有问题。 我正在为oauth2授权服务编写一个集成。 我发送请求以获取授权令牌,并得到如下响应: { "access_token": "c76fb018-27c9-43f7-a751-62646eda7e1a-1", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "03e0be3
{
"access_token": "c76fb018-27c9-43f7-a751-62646eda7e1a-1",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "03e0be32-e72e-47ec-b740-a00b333a8ac4-1",
"id_token": "eyJhbGciOiJnb3N0MzQtMTAuMjAxMiJ9.eyJzdWIiOiIwZDYxNTI3NDRlNDhkMTU4Y2UwMWQ3ZDQwZTdjNzUwYmZhMTVmMWVhY2NkOTQ3YmYwYTU0NzRhNDkwMGMyZTdjIiwiaXNzIjoiaXNzLWRlZmF1bHQtdmFsdWUiLCJhdWQiOiIxMTQzIiwiZXhwIjoxNTE4NzAxMDcxLCJpYXQiOjE1MTg3MDA3NzEsImF1dGhfdGltZSI6MTUxODcwMDc1NiwiYWNyIjoibG9hLTMiLCJhbXIiOiJ7cHdkLCBtY2EsIG1mYSw
gb3RwLCBzbXN9IiwiYXpwIjoiMTE0MyIsIm5vbmNlIjoiN2JlNjZhYzktZDA3Yy00OTY3LWFkZWQtY2EyNzBhMjdlOWU4In0=.EdiC77+9bO+/vRzvv71677+977+977+9eAXvv73vv73vv71E77+977+977+977+9Re+/ve+/vTNbbdm0Bu+/vRY/eO+/vRvvv70q77+977+9LO+/vU4iZO+/vSNF0oFy77+977+977+9GQnvv73vv70v77+9QO+/vXk="
}
id\u令牌-Base64编码的URL是标识用户所需的一组客户端属性。属性由“.”字符分隔,每个字符必须单独解码。
我不知道怎么做。如果有任何帮助,我将不胜感激
Application.yml
spring:
security:
oauth2:
client:
registration:
sbb:
client-id: *******
client-secret: ******
scope: openid
client-authentication-method: post
authorization-grant-type: authorization_code
redirect-uri: '{baseUrl}/login/oauth2/code/{registrationId}'
provider:
sbb:
authorization-uri: https://auth.site.com/ic/sso/api/v1/oauth/authorize
token-uri: https://auth.site.com/ic/sso/api/v1/oauth/token
user-info-uri: https://auth.site.com/ic/sso/api/v1/oauth/user-info
user-name-attribute: sub
启动应用程序时,我收到一个错误:
org.springframework.security.oauth2.core.OAuth2AuthenticationException:[缺少签名\u验证器]找不到用于客户端注册的签名验证器:“sbb”。检查以确保已配置JwkSet URI。
我的提供者不提供JwkSet URI。Filip描述了该方法。我只是把它扩大了一点
@Bean
public JwtDecoderFactory<ClientRegistration> jwtDecoderFactory() {
final JwtDecoder decoder = new JwtDecoder() {
@SneakyThrows
@Override
public Jwt decode(String token) throws JwtException {
JWT jwt = JWTParser.parse(token);
return createJwt(token, jwt);
}
private Jwt createJwt(String token, JWT parsedJwt) {
try {
Map<String, Object> headers = new LinkedHashMap<>(parsedJwt.getHeader().toJSONObject());
Map<String, Object> claims = parsedJwt.getJWTClaimsSet().getClaims();
return Jwt.withTokenValue(token)
.headers(h -> h.putAll(headers))
.claims(c -> c.putAll(claims))
.build();
} catch (Exception ex) {
if (ex.getCause() instanceof ParseException) {
throw new JwtException(String.format(DECODING_ERROR_MESSAGE_TEMPLATE, "Malformed payload"));
} else {
throw new JwtException(String.format(DECODING_ERROR_MESSAGE_TEMPLATE, ex.getMessage()), ex);
}
}
}
};
return context -> decoder;
}
@Bean
公共JwtDecoderFactory JwtDecoderFactory(){
最终JwtDecoder解码器=新JwtDecoder(){
@鬼鬼祟祟
@凌驾
公共Jwt解码(字符串令牌)引发JwtException{
JWT JWT=JWTParser.parse(令牌);
返回createJwt(令牌,jwt);
}
私有Jwt createJwt(字符串标记,Jwt parsedJwt){
试一试{
Map headers=newlinkedhashmap(parsedJwt.getHeader().toJSONObject());
Map claims=parsedJwt.getJWTClaimsSet().getClaims();
返回Jwt.withTokenValue(令牌)
.headers(h->h.putAll(headers))
.索赔(c->c.putAll(索赔))
.build();
}捕获(例外情况除外){
if(例如getCause()instanceof ParseException){
抛出新的JwtException(String.format(解码错误消息模板,“格式错误的负载”);
}否则{
抛出新的JwtException(String.format(解码错误消息模板,例如getMessage()),例如);
}
}
}
};
返回上下文->解码器;
}
粘贴您的id\u令牌。它会告诉你使用哪种算法等等
@Bean
public JwtDecoderFactory<ClientRegistration> jwtDecoderFactory() {
final JwtDecoder decoder = new JwtDecoder() {
@SneakyThrows
@Override
public Jwt decode(String token) throws JwtException {
JWT jwt = JWTParser.parse(token);
return createJwt(token, jwt);
}
private Jwt createJwt(String token, JWT parsedJwt) {
try {
Map<String, Object> headers = new LinkedHashMap<>(parsedJwt.getHeader().toJSONObject());
Map<String, Object> claims = parsedJwt.getJWTClaimsSet().getClaims();
return Jwt.withTokenValue(token)
.headers(h -> h.putAll(headers))
.claims(c -> c.putAll(claims))
.build();
} catch (Exception ex) {
if (ex.getCause() instanceof ParseException) {
throw new JwtException(String.format(DECODING_ERROR_MESSAGE_TEMPLATE, "Malformed payload"));
} else {
throw new JwtException(String.format(DECODING_ERROR_MESSAGE_TEMPLATE, ex.getMessage()), ex);
}
}
}
};
return context -> decoder;
}