【发布时间】: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