使用现有数据库在 Glassfish 上实施 JDBC 身份验证

Implementing JDBC Authentication on Glassfish with an existing database

我一直在研究 Jersey 程序示例以熟悉 Java Web 服务,现在我想为其添加一个安全层(服务器 - Glassfish,IDE -智能)。到目前为止,我已经实现了一个基于 FORM 的登录系统,该系统应该引用现有的 Sybase 数据库。问题是,即使我输入了正确的凭据,它也不会进行身份验证,而且我对一般身份验证的了解还不够多,无法对其进行故障排除。希望这里有人能找出我哪里出错了。以下是我采取的步骤:

  1. 在 Glassfish 管理控制台中创建社区池和资源
  2. 在 Glassfish 管理控制台中创建一个 JDBC 领域
  3. 修改 web.xml 文件以包含安全约束和登录配置
  4. 创建一个 login.xhtml 页面和一个 loginerror.xhtml 页面

附带说明一下,我尝试使用的数据库是一个现有的 Sybase 数据库,其中包含各种信息(不仅是用户名,还有电子邮件、主管、phone 扩展名等)。该数据库没有明确的密码字段,但我正在尝试使用现有字段之一(即名为 supervisorEmail 的字段)作为密码字段。因此,用户可以通过自己的电子邮件和主管的电子邮件进行身份验证。

我的第一个问题是:我在哪里指定要用作数据库中的 username/password 的列?我以为我会在 JdbcRealm 定义中这样做,但也许我错了。以下是我在这些领域的知识:

JAAS 上下文:jdbcRealm

JNDI:jdbc/__AuthDB(我之前创建的资源)

用户Table:EmployeeList(数据库中table的名字)

用户名列:email

密码栏:supervisorEmail

群组Table:群组(不知道该放什么)

组名称列:名称(不知道该放什么)

密码加密算法:AES

这会引出我的第二个问题,即 "do I need a group database if all users get the same privileges"?我目前没有。

最后,这里有任何可用于故障排除的 xml/html 文件。抱歉这么长 post,我想尽可能具体。

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">

<security-constraint>
    <display-name>Admin Pages</display-name>
    <web-resource-collection>
        <web-resource-name>Secured</web-resource-name>
        <url-pattern>/*</url-pattern>
        <http-method>GET</http-method>
        <http-method>POST</http-method>
    </web-resource-collection>

    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

<!--<deny-uncovered-http-methods/>-->

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>JdbcRealm</realm-name>
    <form-login-config>
        <form-login-page>/login.xhtml</form-login-page>
        <form-error-page>/loginerror.xhtml</form-error-page>
    </form-login-config>
</login-config>

<security-role>
    <role-name>admin</role-name>
</security-role>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>

login.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:p="http://primefaces.org/ui">
<body>
    <p:panel header="Login From">
        <form method="POST" action="j_security_check">
            Username: <input type="text" name="j_username"/>
            Password: <input type="password" name="j_password"/>
            <input type="submit" value="Login" />
            <input type="reset" value="Reset" />
        </form>
    </p:panel>
</body>

如果您已经读到这里,感谢您的阅读。任何帮助是极大的赞赏。

我想通了。当您在 Glassfish 中使用 jdbc 领域时,您必须有两个独立的数据库:一个具有 users/passwords 列表,另一个具有用户列表(与之前的数据库相同)以及他们属于哪个组。如果您不使用群组,Glassfish 将不会对您进行身份验证,即使您希望每个人都拥有相同的权限。