我正在探索基于 glassfish 服务器的 jdbc 领域的纯 Java EE 方法来实现编程安全性,特别是登录用户。
基本上,在我的登录 servlet 中我正在做
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
request.login(username, password);
....
无需在 web.xml 中执行任何操作,就使用默认领域(文件)。
我不想那样,我想使用名为 jdbcsecurerealm 的 jdbcRealm。
所以我将以下内容添加到我的 web.xml 中
<login-config>
<auth-method>FORM</auth-method>
<realm-name>jdbcsecurerealm</realm-name>
</login-config>
请注意,我没有添加任何表单登录配置来定义表单登录页面和表单错误页面。
然后,如果我定义安全约束,例如
<security-constraint>
<web-resource-collection>
<web-resource-name>Admin Pages</web-resource-name>
<description></description>
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>administrator</role-name>
</auth-constraint>
</security-constraint>
嗯...它有效! request.login 检查我的 jdbcRealm,如果我尝试在未登录的情况下访问安全页面,那么我会得到一个不错的 403。
但我似乎混合了声明性安全性和编程性安全性,因为我觉得我不应该在 web.xml 中声明任何内容,而应该使用 request.isUserInRole。
Question:
我是否遇到了 glassfish 特定行为,或者是否允许使用编程安全性(request.login)与 web.xml 中定义的 jdbc 领域而不使用 form-login-config ?
Update我刚刚看到可以在 glassfish-application.xml 中指定一个领域,这是构建一个耳朵而不是战争来指定领域的更好方法吗?