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

时间:2015-06-19 16:09:30

标签: java authentication jdbc glassfish sybase

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

  1. 在Glassfish管理控制台中创建社区池和资源
  2. 在Glassfish管理控制台中创建JDBC领域
  3. 修改web.xml文件以包含安全约束和登录配置
  4. 创建login.xhtml页面和loginerror.xhtml页面
  5. 作为旁注,我试图使用的数据库是现有的Sybase数据库,它包含各种信息(不仅包括用户名,还包括电子邮件,主管,电话分机等)。此数据库没有明确的密码字段,但我尝试使用其中一个现有字段(即一个名为supervisorEmail)作为密码字段。因此,用户可以使用自己的电子邮件和主管的电子邮件进行身份验证。

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

    JAAS上下文:jdbcRealm

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

    用户表:EmployeeList(数据库中表的名称)

    用户名栏:电子邮件

    密码栏:supervisorEmail

    组表:组(不知道放在这里的内容)

    组名列:名称(不知道放在这里的内容)

    密码加密算法:AES

    这将导致我的第二个问题,即“如果所有用户都获得相同的权限,我是否需要一个组数据库”?我目前没有。

    最后,这里有任何可用于故障排除的xml / html文件。对于这篇长篇文章感到抱歉,我希望尽可能具体。

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

    如果你已经做到这一点,谢谢你的阅读。任何帮助是极大的赞赏。

1 个答案:

答案 0 :(得分:0)

我明白了。当您在Glassfish中使用jdbc域时,您必须拥有两个单独的数据库:一个具有用户/密码列表,另一个具有用户列表(与先前数据库相同)和&amp;他们属于哪个群体。如果您不使用群组,Glassfish将不会对您进行身份验证,即使您希望每个人都拥有相同的权限。

相关问题