DropWizard Auth领域

IAm*_*aja 7 java authentication jax-rs jersey dropwizard

在DropWizard中,我可以像这样设置基本身份验证(在Application#runimpl中):

BasicAuthProvider<SimplePrincipal> authProvider = new BasicAuthProvider(authenticator, "SECRET_REALM");
environment.jersey().register(authProvider);
Run Code Online (Sandbox Code Playgroud)

我想知道String realm(" SECRET_REALM ")的重要性是什么?

从一般安全概念来看,我理解一个"领域"是一个存储用户和角色/权限的地方(数据库,目录,文件,密钥库等).

一个领域在DropWizard中意味着什么,以及在内部指定它的意义是什么BasicAuthProvider?它是否在引擎盖下创造了这个领域的东西?

Pau*_*tha 13

从某种意义上说,领域是服务器中的一些受保护区域/空间.领域应该有一个名字.如果我们从这篇文章运行示例,使用cURL(我建议下载,因为它在开发中很有用),没有任何用户凭据,我们将看到以下内容.

C:\>curl -i  http://localhost:8080/simple
HTTP/1.1 401 Unauthorized
Date: Thu, 11 Dec 2014 18:55:02 GMT
WWW-Authenticate: Basic realm="Basic Example Realm"
Content-Type: text/plain
Transfer-Encoding: chunked

Credentials are required to access this resource.
Run Code Online (Sandbox Code Playgroud)

这就是基本身份验证协议的工作原理.当服务器希望用户代理进行身份验证时,为了访问安全资源,它将发送回"401 Unauthorized",以及类似于

WWW-Authenticate: Basic realm="Basic Example Realm"
Run Code Online (Sandbox Code Playgroud)

您提供的名称BasicAuthProviderrealm将在报头中提供.你可以在源代码中看到

if (required) {
    final String challenge = String.format(CHALLENGE_FORMAT, realm);
    throw new WebApplicationException(
                                    Response.status(Response.Status.UNAUTHORIZED)
                    .header(HttpHeaders.WWW_AUTHENTICATE, challenge)
                    .entity("Credentials are required to access this resource.")
                    .type(MediaType.TEXT_PLAIN_TYPE)
                    .build());
Run Code Online (Sandbox Code Playgroud)

现在尝试从浏览器访问资源.你会看见

在此输入图像描述

您还可以在那里看到领域名称.在RFC 2617只规定(关于realm):

realm:
要显示给用户的字符串,以便他们知道要使用的用户名和密码.此字符串应至少包含执行身份验证的主机的名称,并可能另外指示可能具有访问权限的用户集合.一个例子可能是"registered_users@gotham.news.com".

  • 再次感谢@peeskillet(+1)!我已经发布了很多关于SO的问题**,而且你是迄今为止我遇到的最全面,最敏感,最有帮助的用户.再次感谢! (2认同)