无法设置GlassFish JDBCRealm以进行基本身份验证

我正在尝试为基本身份验证设置JDBCRealm。

我首先用文件领域实现了登录过程,并且一切都按计划进行。现在,我想切换到JDBCRealm。我正在使用GlassFish 5.193

使用正确的用户名+密码时,我得到状态码: 401未经授权,并且该密码在server.log中:

[2019-12-29T23:32:51.111+0100] [Payara 5.193] [WARNUNG] [] [javax.enterprise.system.container.web.com.sun.web.security] [tid: _ThreadID=37 _ThreadName=http-thread-pool::http-listener-1(1)] [timeMillis: 1577658771111] [levelValue: 900] [[
  WEB9102: Web Login Failed: com.sun.enterprise.security.auth.login.common.LoginException: Login failed: Failed jdbc login for user01.]]

这是我的代码:

JDBC连接池(Ping成功):

无法设置GlassFish JDBCRealm以进行基本身份验证

无法设置GlassFish JDBCRealm以进行基本身份验证

JDBC资源:

无法设置GlassFish JDBCRealm以进行基本身份验证

安全领域:

无法设置GlassFish JDBCRealm以进行基本身份验证

无法设置GlassFish JDBCRealm以进行基本身份验证

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>jdbcRealm</realm-name>
    </login-config>
    <security-role>
        <role-name>admin</role-name>
    </security-role>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <security-constraint>
        <web-resource-collection>
            <url-pattern>/api/path/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
        </auth-constraint>
    </security-constraint>
</web-app>

glassfish-web.xml:

<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD
GlassFish Application Server 3.1 Servlet 3.0//EN"
        "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
    <security-role-mapping>
        <role-name>admin</role-name>
        <group-name>admin</group-name>
    </security-role-mapping>
</glassfish-web-app>

persistence-xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
             xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
                        http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="MeineJpaPU" transaction-type="JTA">
        <jta-data-source>jdbc/postgresqlpool</jta-data-source>
        <properties>
            <property name="eclipselink.logging.level" value="ALL"/>
            <property name="eclipselink.ddl-generation" value="create-tables" />
            <property name="eclipselink.cache.shared.default" value="false" />
        </properties>
    </persistence-unit>
</persistence>

我的表格SQL:

CREATE TABLE player_login (
    player_id varchar(255) PRIMARY KEY,password varchar(255) NOT NULL,FOREIGN KEY (player_id) REFERENCES player (player_id)
);

CREATE TABLE player_groups (
    group_id varchar(255) PRIMARY KEY,player_id varchar(255) NOT NULL,FOREIGN KEY (player_id) REFERENCES player (player_id)
);

表的内容:

无法设置GlassFish JDBCRealm以进行基本身份验证

无法设置GlassFish JDBCRealm以进行基本身份验证

Request-Header:

Method: GET
URL: http://localhost:8080/myproject/api/path/1/1
Host: localhost:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0
accept: */*
accept-Language: en-US,en;q=0.5
accept-Encoding: gzip,deflate
Authorization: Basic dXNlcjAxOmluaXQxMjMx
Connection: keep-alive
Cookie: Idea-c27a0faf=cb309478-7d45-44c8-9dd1-62cc4d7b9108; JSESSIONID=c73fe0f00be13caba1d2ad121d75; treeForm_tree-hi=treeForm:tree:configurations:server-config:security:realms:jdbcRealm

响应标题:

HTTP/1.1 401 Unauthorized
Server: Payara Server  5.193 #badassfish
X-Powered-By: Servlet/4.0 JSP/2.3 (Payara Server  5.193 #badassfish Java/Oracle Corporation/1.8)
Pragma: No-cache
Cache-Control: no-cache
Expires: Thu,01 Jan 1970 01:00:00 CET
WWW-Authenticate: Basic realm="jdbcRealm"
Content-Language: 
Content-Type: text/html
Content-Length: 1070
X-Frame-Options: SAMEORIGIN

* UPDATE 29/12/19:

  • 仍然无法解决它,我尝试了一些其他事情:
  • 启用了安全管理器,并将默认领域设置为我的jdbcRealm
  • 将安全性的日志级别更改为最佳级别,但错误消息未更改(仍然是“登录失败:user01的jdbc登录失败。]]”)
  • 回到文件领域,一切正常...
  • 尝试使用空编码,没有任何变化。我完全不了解这一点,数据库保存的密码是sha256加密的,并且由于我使用基本身份验证,因此我发送了以base64编码的纯密码-所以我想我必须将摘要算法设置为SHA-256并编码为Base64?

如果您需要任何其他信息,请告诉我...我真的不知道为什么它不起作用。

guoyi110 回答:无法设置GlassFish JDBCRealm以进行基本身份验证

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2848687.html

大家都在问