Wildfly 8 基本认证

Wildfly 8 Basic Authentication

我在 Wildfly 8 上获得了一个 Java-Webapp 运行。我尝试使用 resteasy 注释来保护我的 restful 网络服务。我使用命令行工具 curl 测试其余 api。

基本身份验证设置似乎有效。带有注释“@PermitAll”的对 Web 服务的 Http 请求工作正常。卷曲说:

~ % curl -v http://localhost:8080/ItilityServer-web/rest/account 
> GET /ItilityServer-web/rest/account HTTP/1.1
> User-Agent: curl/7.40.0
> Host: localhost:8080
> Accept: */*
> 
< HTTP/1.1 200 OK
< Connection: keep-alive
< X-Powered-By: Undertow/1
< Server: WildFly/8
< Content-Length: 0
< Date: Wed, 28 Jan 2015 10:47:11 GMT
< 
* Connection #0 to host localhost left intact

但是包含有效用户名和密码的 http 请求被拒绝,状态码为 401 未授权。 Wildfly 记录错误 unmatched password:

2015-01-28 11:42:43,565 TRACE [org.jboss.security] (default task-5) PBOX000263: Executing query SELECT a.password FROM Account a WHERE a.name = ? with username hans
2015-01-28 11:42:43,566 DEBUG [org.jboss.security] (default task-5) PBOX000283: Bad password for username hans

但这不是真的。解密的授权详细信息 "aGFuczpoZWxtaWhlbG1paGVsbWk=" 是 hans:helmihelmihelmi 并且此用户名和密码存储在我的数据库中。与安全域使用的相同的 jpql 查询导致 unsername "hans" 和他的密码 "helmihelmihelmi".

这是我的设置:

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
     version="3.0">

<context-param>
    <param-name>resteasy.role.based.security</param-name>
    <param-value>true</param-value>
</context-param>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Application</realm-name>
</login-config>

<security-role>
    <role-name>store</role-name>
</security-role>

</web-app>

(我不知道安全领域是什么,所以我只是把这个 属性 留在了 login-config 标签中)

我的安全域在 standalone.xml

<security-domain name="DBLogin" cache-type="default">
    <authentication>
        <login-module code="Database" flag="required">
            <module-option name="dsJndiName" value="java:jboss/datasources/ExampleDS"/>
            <module-option name="principalsQuery" value="SELECT a.password FROM Account a WHERE a.name = ?"/>
            <module-option name="rolesQuery" value="SELECT a.userRole FROM Account a WHERE a.name = ?"/>
            <module-option name="hashAlgorithm" value="SHA-256"/>
            <module-option name="hashEncoding" value="Base64"/>
            <module-option name="hashCharset" value="UTF-8"/>
            <module-option name="unauthenticatedIdentity" value="guest"/>
        </login-module>
    </authentication>
</security-domain>

Restful 网络服务

@GET
@RolesAllowed(AuthRole.STORE)
@Produces(MediaType.APPLICATION_JSON)
public Response getAccountByName() {
    Response.ResponseBuilder builder = Response.ok();
    return builder.build();
}

和import.xml在启动时创建用户

insert into Account(id, name, email, password, user_role) values (0, 'hans', 'john.smith@mailinator.com', 'helmihelmihelmi', 'store') 
insert into Store(id, name, zipcode, street, housenumber, town, account_id) values(0, 'Edeka', 72622, 'stephanstraße', 10, 'Reudern', 0);

不知道如何找到解决方案,因为我什至不知道问题所在。希望有人能帮忙。

您不得将未加密的密码存储在数据库中。 WildFly 希望您使用 login-module 配置中指定的散列算法和编码来存储 散列 密码。

创建新 Account 时,使用

org.jboss.security.auth.spi.Util.createPasswordHash()

获取要存储的散列密码。

通常,存储原始密码存在安全风险。