且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用RESTEasy显示用于基本身份验证的浏览器登录表单

更新时间:2023-12-05 22:10:40

您需要添加

You need to add the WWW-Authenticate header to the response that you abort with. This header tells the browser that it should present the default browser login form.

private static final String CHALLENGE_FORMAT = "%s realm=\"%s\"";

private Response createUnauthorizedResponse() {
    return Response.status(Response.Status.UNAUTHORIZED)
            .header(HttpHeaders.WWW_AUTHENTICATE, String.format(CHALLENGE_FORMAT, "Basic", "Access"))
            .type(MediaType.TEXT_PLAIN_TYPE)
            .entity("Credentials are required to access this resource.")
            .build();

这是Chrome上的登录外观

And here's what the login should look like on Chrome