使用 Java EE 表单登录模块登录后,我需要将用户 ID 设置为 session 属性。
现在,登录后,我发送另一个 HTTP 请求,该请求将 ID 设置为 session 属性,但我需要一步完成。最好的方法是什么?
standalone.xml中的登录模块配置:
<login-module code="com.MyLoginModule" flag="required">
<module-option name="dsJndiName" value="java:/PostgresDS"/>
<module-option name="principalsQuery" value="select password from appuser where email=?"/>
<module-option name="rolesQuery" value="select 'AUTHENTICATED', 'Roles' from appuser where email=?"/>
<module-option name="hashAlgorithm" value="custom"/>
</login-module>
登录后的附加请求(RESTEasy):
@GET
@Path("/web")
@Produces(MediaType.APPLICATION_JSON)
public User getUser(@Context HttpServletRequest hsr) throws MyRuntimeException{
User u;
HttpSession session = hsr.getSession();
u = um.getUserByMail(hsr.getUserPrincipal().getName());
session.setAttribute("userId", u.getId());
return u;
}
我的登录模块:
public class MyLoginModule extends DatabaseServerLoginModule {
@Override
public String createPasswordHash(String username, String password, String digestOption){code}
}
最佳答案
我假设您在这里使用 JBoss...
例如,您可以增强
org.apache.catalina.authenticator.FormAuthenticator
(在 JBoss 中用作标准 validator )并在那里实现您的附加逻辑。
public class MyFormAuthenticator extends FormAuthenticator{
@Override
public boolean authenticate(Request request,HttpServletResponse response,LoginConfig config){
boolean success = super.authenticate(request,response,config);
if(success){
Session session = request.getSessionInternal(false); // Use the existing session
session.put .... // the action which you want to do
}
return success;
}
不要忘记调整 jboss-web.xml(放置在 WEB-INF 文件夹中):
<jboss-web>
<context-root>myContext</context-root>
<valve>
<class-name>MyFormAuthenticator</class-name>
</valve>
</jboss-web>
Maven
确保您使用此 Maven 依赖项(而不是 jboss-as-web):
<dependency>
<groupId>org.jboss.web</groupId>
<artifactId>jbossweb</artifactId>
<version>7.5.10.Final</version>
</dependency>
关于java - 登录后设置 session 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37372723/