java - 登录后设置 session 属性

标签 java authentication wildfly

使用 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/

相关文章:

java - 将 ArrayList 转换为 JSON(在 Java 中)并使用 Javascript 访问它

Java Sigmoid 方法返回不正确的结果

authentication - Azure移动应用程序刷新服务器上的 token

node.js - 在 React 中呈现的 Blaze LoginButtons 模板 - 登录仅适用于主页

jquery - 失败后重做 AJAX 调用

java - 测试与多个 LDAP 服务器的连接时出错

java - 正交投影 - 使物体适合屏幕?

java - 将 Spring Security 2.5 升级到 3.1 - java.lang.NoClassDefFoundError : org/springframework/security/Authentication

wildfly - 禁用 WildFly 13 管理控制台

java - Wildfly:org.jboss.as.controller.registry.FastCopyHashMap