登录后设置会话属性

时间:2016-05-22 09:43:47

标签: java login wildfly

我需要在使用Java EE表单登录模块登录后将用户的ID设置为会话属性。

现在,登录后,我发送另一个HTTP请求,它将ID设置为会话属性,但我需要一步完成。最好的方法是什么?

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;
}

MyLoginModule:

public class MyLoginModule extends DatabaseServerLoginModule {
@Override
public String createPasswordHash(String username, String password, String digestOption){code}
}

1 个答案:

答案 0 :(得分:0)

我假设您在这里使用JBoss ....

例如,您可以增强

  

org.apache.catalina.authenticator.FormAuthenticator

(在JBoss中用作标准验证器)并在那里实现您的附加逻辑。

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>
相关问题