【问题标题】:customization of dropwizard initial unauthorized message to json format in Java在 Java 中将 dropwizard 初始未授权消息自定义为 json 格式
【发布时间】:2017-02-06 17:52:00
【问题描述】:

我们正在使用 dropwizard 0.9.X,我们已经编写了自定义类,用于在检索数据之前验证用户输入的令牌。我想自定义初始未经授权的消息“访问此资源需要凭据”。转换为 JSON 格式,我想显示如下。

{
  "code": 401,
  "message": "HTTP 401 Unauthorized, Credentials are required to access this resource."
}

我已经编写了 AbstractStatusType 来实现 StatusType,并且每当用户输入无效令牌或过期令牌时,我都能够打印上述格式消息。示例承载 03cb9ebe-1c66-4d08-b9a4-afe69989ef21

但我仍然收到纯文本响应正文,因为“访问此资源需要凭据”。当用户仅输入“bearer”或仅输入令牌“03cb9ebe-1c66-4d08-b9a4-afe69989ef21”时。

你能帮我把上面的文本格式消息定制成上面的JSON格式吗?

以下是设置响应“访问此资源需要凭据”的自定义代码。在内部使用 dropwizard OAuthCredentialAuthFilter 类

public AuthDynamicFeature getAuthFilter(CachingAuthenticator<String, AuthenticatedPrincipal> cachingAuthenticator) {
    OAuthCredentialAuthFilter<AuthenticatedPrincipal> authFilter = new OAuthCredentialAuthFilter.Builder<AuthenticatedPrincipal>()
            .setAuthenticator(cachingAuthenticator)
            .setAuthorizer(new OAuthAuthorizer())
            .setRealm(REALM)
            .setPrefix(PREFIX)
            .buildAuthFilter();
    return new AuthDynamicFeature(authFilter);
}

提前感谢您的帮助!

【问题讨论】:

    标签: java json jersey dropwizard


    【解决方案1】:

    为此,需要在DW上设置UnauthorizedHandler:

    public AuthDynamicFeature getAuthFilter(CachingAuthenticator<String, AuthenticatedPrincipal> cachingAuthenticator) {
        OAuthCredentialAuthFilter<AuthenticatedPrincipal> authFilter = new OAuthCredentialAuthFilter.Builder<AuthenticatedPrincipal>()
                .setAuthenticator(cachingAuthenticator)
                .setAuthorizer(new OAuthAuthorizer())
                .setRealm(REALM)
                .setPrefix(PREFIX)
                .setUnauthorizedHandler(new MyUnauthorizedHandler())
                .buildAuthFilter();
        return new AuthDynamicFeature(authFilter);
    }
    

    setUnauthorizedHandler 方法设置此属性。

    然后您可以通过实现该接口来自定义响应:

    public static class MyUnauthorizedHandler implements UnauthorizedHandler {
    
            @Override
            public Response buildResponse(String prefix, String realm) {
                return Response.status(Status.UNAUTHORIZED).entity("").build();
            }
    
        }
    

    希望对你有帮助,

    阿图尔

    【讨论】:

    • @Artur,感谢您的建议,通过上述解决方案,我将能够打印我想要的消息,但我没有收到 JSON 格式的消息。我只是得到计划文本格式。你能建议我如何打印 JSON 格式的消息吗?谢谢。有没有办法设置 JSON 格式的消息而不是计划文本格式?提前感谢您的帮助!
    【解决方案2】:

    @亚瑟,

    我已经使用 JsonUnauthorizedHandler 实现了 UnauthorizedHandler,如下所示,它运行良好。

    public class JsonUnauthorizedHandler implements UnauthorizedHandler {
        private static final String CHALLENGE_FORMAT = "%s realm=\"%s\"";
    
        @Override
        public Response buildResponse(String prefix, String realm) {
            // Create an HTTP 401 Unauthorized response with a JSON payload of a
            // human readable error
            return Response.status(Response.Status.UNAUTHORIZED)
                    .header(HttpHeaders.WWW_AUTHENTICATE,
                            String.format(CHALLENGE_FORMAT, prefix, realm))
                    .type(MediaType.APPLICATION_JSON_TYPE)
                    .entity(ImmutableMap.of("code","401",
                            "message",
                            "HTTP 401 Unauthorized, Credentials are required to access this resource."))
                    .build();
        }
    }
    

    然后我将它设置为如下 getAuthFilter。

    public AuthDynamicFeature getAuthFilter(CachingAuthenticator<String, AuthenticatedPrincipal> cachingAuthenticator) {
        OAuthCredentialAuthFilter<AuthenticatedPrincipal> authFilter = new OAuthCredentialAuthFilter.Builder<AuthenticatedPrincipal>()
                .setAuthenticator(cachingAuthenticator)
                .setAuthorizer(new OAuthAuthorizer())
                .setRealm(REALM)
                .setPrefix(PREFIX)
                .setUnauthorizedHandler(new JsonUnauthorizedHandler())
                .buildAuthFilter();
        return new AuthDynamicFeature(authFilter);
    }
    

    以下是我在大张旗鼓发布上述更改后收到的回复。

    {
      "code": 401,
      "message": "HTTP 401 Unauthorized, Credentials are required to access this resource."
    }
    

    感谢您对此提出的建议。

    谢谢 破布

    【讨论】:

    • 很高兴你找到它 :) 标记我 @Artur 不会给我通知,这就是为什么我没有看到你的跟进。您需要为此使用我的用户名:)
    猜你喜欢
    • 2016-03-22
    • 2019-10-21
    • 2020-09-23
    • 1970-01-01
    • 2016-05-07
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多