authentication - Wildfly 8 基本身份验证

标签 authentication jax-rs resteasy wildfly

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

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

~ % 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 记录错误不匹配的密码:
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”。

这是我的设置:

网页.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()

获取要存储的散列密码。

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

关于authentication - Wildfly 8 基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28191336/

相关文章:

node.js - Nodejs/mongodb - 检查用户是否具有管理员权限(基于 token 的身份验证)

.net - FormsAuthenticationModule 是否检测是否……?

sql-server - 无法连接到 SQL Server Management Studio

java - 不使用Keycloak的Wildfly中仅承载身份验证

java - RESTEASY 抛出查询参数为空的异常

Resteasy Bean 验证在远程服务器上不起作用

Django/Auth : Can request. 用户被利用并指向其他用户?

java - 在单元测试期间注入(inject)模拟 HttpServletResponse

java - 如何在Java中使用JacksonJSONProvider ObjectMapper()类?

java - 是否必须在 JBOSS AS 上将 RESTEasy 与 Spring 集成?