Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot spring boot OAuth资源服务器JWT AudienceValidator 401消息为空_Spring Boot_Oauth 2.0_Jwt_Audience - Fatal编程技术网

Spring boot spring boot OAuth资源服务器JWT AudienceValidator 401消息为空

Spring boot spring boot OAuth资源服务器JWT AudienceValidator 401消息为空,spring-boot,oauth-2.0,jwt,audience,Spring Boot,Oauth 2.0,Jwt,Audience,我使用Spring Boot OAuth资源服务器(JOSE)来验证来自Auth0的JWT。 流程运行良好,除非我检查JWT的受众声明,如果失败,401响应状态为return,但消息体为空 我定制了exceptionHandling来发送消息正文,它使用authenticationEntryPoint和accessDeniedHandler运行良好,但是这些处理没有为OAuth2TokenValidator触发 我只想在发生受众验证错误时接收401和401的自定义消息正文 以下是我使用的代码:

我使用Spring Boot OAuth资源服务器(JOSE)来验证来自Auth0的JWT。 流程运行良好,除非我检查JWT的受众声明,如果失败,401响应状态为return,但消息体为空

我定制了exceptionHandling来发送消息正文,它使用authenticationEntryPoint和accessDeniedHandler运行良好,但是这些处理没有为OAuth2TokenValidator触发

我只想在发生受众验证错误时接收401和401的自定义消息正文

以下是我使用的代码:

public static class AudienceValidator implements OAuth2TokenValidator<Jwt> {
    private Logger logger = LoggerFactory.getLogger(AudienceValidator.class);
    private final String audience;
    OAuth2Error error = new OAuth2Error("invalid_token", "The required audience is missing", null);
    public AudienceValidator(String audience) {
        this.audience = audience;
    }    
    public OAuth2TokenValidatorResult validate(Jwt jwt) {
        logger.debug("validating audience: " + jwt.getAudience());
        if (jwt.getAudience().contains(audience)) {
            logger.debug("audience success");
            return OAuth2TokenValidatorResult.success();
        } else {
            logger.debug("invalid audience");
            return OAuth2TokenValidatorResult.failure(error);
        }
    }
}

@Override
protected void configure(HttpSecurity http) throws Exception {  
        http
        .cors().and()
            .oauth2ResourceServer(oauth2 -> oauth2
                    .jwt(jwt -> jwt
                        .jwtAuthenticationConverter(grantedAuthoritiesExtractor())
                    )
                );
        logger.debug("authenticatedOnly:"+authenticatedOnly);
        if (authenticatedOnly) http.authorizeRequests().anyRequest().authenticated();
        else http.authorizeRequests().anyRequest().permitAll();

       http
       .exceptionHandling()
        .authenticationEntryPoint((request, response, e) -> {
            response.setStatus(HttpStatus.UNAUTHORIZED.value());
            response.setContentType("application/json");
            response.getWriter().write("{\"status\":401,\"error\":\"You are not authenticated.\",\"message\":\"\"}");
        })
        .accessDeniedHandler((request, response, e) -> {
            response.setStatus(HttpStatus.FORBIDDEN.value());
            response.setContentType("application/json");
            response.getWriter().write("{\"status\":403,\"error\":\"You are not authorized.\",\"message\":\"\"}");
        })
        ;
}   
公共静态类AudienceValidator实现OAuth2TokenValidator{
私有记录器Logger=LoggerFactory.getLogger(audencevalidator.class);
私人终场观众;
OAuth2Error error=新的OAuth2Error(“无效的_令牌”,“缺少所需的访问群体”,null);
公众听众(弦乐观众){
这个。观众=观众;
}    
公共OAuth2TokenValidatorResult验证(Jwt Jwt){
debug(“验证访问群体:+jwt.getAudience());
if(jwt.getAudience().contains(观众)){
logger.debug(“受众成功”);
返回OAuth2TokenValidatorResult.success();
}否则{
logger.debug(“无效访问群体”);
返回OAuth2TokenValidatorResult.failure(错误);
}
}
}
@凌驾
受保护的无效配置(HttpSecurity http)引发异常{
http
.cors()和()
.oauth2ResourceServer(oauth2->oauth2
.jwt(jwt->jwt
.jwtAuthenticationConverter(授权权限提取器())
)
);
debug(“authenticatedOnly:+authenticatedOnly”);
如果(仅验证)http.authorizedRequests().anyRequest().authorized();
else http.authorizeRequests().anyRequest().permitAll();
http
.例外处理()
.authenticationEntryPoint((请求、响应、e)->{
response.setStatus(HttpStatus.UNAUTHORIZED.value());
setContentType(“应用程序/json”);
response.getWriter().write(“{\”状态\“:401,\”错误\“:\”您未通过身份验证。\”,\“消息\“:\”);
})
.accessDeniedHandler((请求、响应、e)->{
response.setStatus(HttpStatus.probled.value());
setContentType(“应用程序/json”);
response.getWriter().write(“{\”状态\“:403,\”错误\“:\”您未被授权。\”,\“消息\“:\”);
})
;
}