【发布时间】:2015-09-05 16:51:27
【问题描述】:
我一直在编写一个示例 Jersey 程序来熟悉 Java Web 服务,现在我想为它添加一个安全层(服务器 - Glassfish,IDE - IntelliJ)。到目前为止,我已经实现了一个基于 FORM 的登录系统,它应该引用现有的 Sybase 数据库。问题是即使我输入了正确的凭据,它也无法进行身份验证,而且我对身份验证的了解不足,无法对其进行故障排除。希望这里有人能弄清楚我哪里出错了。以下是我采取的步骤:
- 在 Glassfish 管理控制台中创建社区池和资源
- 在 Glassfish 管理控制台中创建 JDBC 领域
- 修改 web.xml 文件以包含安全约束和登录配置
- 创建一个 login.xhtml 页面和一个 loginerror.xhtml 页面
附带说明,我尝试使用的数据库是现有的 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>
如果您已经做到了这一点,感谢您的阅读。任何帮助是极大的赞赏。
【问题讨论】:
标签: java authentication jdbc glassfish sybase