配置web.xml和glassfish容器实现javaEE表单验证

时间:2022-05-22 18:09:04

web.xml配置:

   <!-- 声明用于安全约束的角色 -->
<security-role>
<role-name>ReimUser</role-name>
</security-role>
<security-role>
<role-name>ReimAdmin</role-name>
</security-role> <security-constraint>
<web-resource-collection>
<web-resource-name>root</web-resource-name>
<url-pattern>/service/*</url-pattern>
<url-pattern>/rest/*</url-pattern>
<http-method>POST</http-method>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>ReimUser</role-name>
</auth-constraint>
</security-constraint> <!-- 配置表单验证 -->
<login-config>
<auth-method>FORM</auth-method>
<!-- 指定HTTP Basic验证中使用的领域名 -->
<realm-name>ReimApp</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/login.jsp</form-error-page>
</form-login-config>
</login-config>

表单的action地址j_security_check  j_username j_password不可更改

配置web.xml和glassfish容器实现javaEE表单验证

新建用户表、组表并在glassfish的领域中配置对应的用户表、组表,用于校验用户是否合法。合法时从组表中读取用户对应的组设置到 container 中。

配置web.xml和glassfish容器实现javaEE表单验证配置web.xml和glassfish容器实现javaEE表单验证

其实这就是容器实现的sam模块,他会从配置的 组表 里面查当前登录的用户有哪些组,然后设置  GroupPrincipal 和 sam中 从 token 解析 scope 出来去设置一样。

默认获取 UserPrincipal 对应的类,从里面解析出来的用户信息只有一个账号信息。如果需要获取完成的用户信息,可以自己实现ServerAuthModule模块来实现。

这里配置的参数就是ServerAuthModule类所用到的参数。

要支持这种,在 ServerAuthModule 的 validateRequest 里面加上对应的代码就可以了:

if(request.getServletPath().startsWith('/j_security_check')) {
String username = request.getParameter('j_username');
String password = request.getParameter('j_password');
//TODO: check username and password
//TODO: set user's groups
}

这样action 和 对应的参数名称就都可以自己定义了,。