我一直在寻找一种方法来告诉Micronaut (1.3.5)在http基本身份验证设置中身份验证失败的情况下添加"WWW-Authenticate“头。默认情况下,此标头缺失,并且仅返回状态代码401。
发布于 2020-06-23 20:24:01
答案是提供一个自定义的ExceptionHandler,如下所示:
/**
* AuthorizationException Handler, which adds header for showing browser basic auth dialogue.
*/
@Singleton
@Primary
public class HttpBasicAuthorizationExceptionHandler
implements ExceptionHandler<AuthorizationException, MutableHttpResponse<?>> {
@Override
public MutableHttpResponse<?> handle(HttpRequest request, AuthorizationException exception) {
return Flowable.fromPublisher(reject(exception.isForbidden())).blockingFirst();
}
/**
* @param forbidden true if the status is HttpStatus.FORBIDDEN, HttpStatus.UNAUTHORIZED otherwise.
* @return the http response.
*/
private Publisher<MutableHttpResponse<?>> reject(boolean forbidden) {
if (forbidden) {
return Publishers.just(HttpResponse.status(HttpStatus.FORBIDDEN));
}
return Publishers.just(
HttpResponse.status(HttpStatus.UNAUTHORIZED)
.header(HttpHeaders.WWW_AUTHENTICATE, "Basic")
);
}
}https://stackoverflow.com/questions/62534662
复制相似问题