Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在我的Spring boot应用程序中实例化org.springframework.session.MapSession_Java_Spring_Spring Boot_Spring Session - Fatal编程技术网

Java 如何在我的Spring boot应用程序中实例化org.springframework.session.MapSession

Java 如何在我的Spring boot应用程序中实例化org.springframework.session.MapSession,java,spring,spring-boot,spring-session,Java,Spring,Spring Boot,Spring Session,我正在为Spring boot应用程序管理会话。在这种情况下,我想用自己生成的id在应用程序中实例化MapSession对象,这样我就可以覆盖其当前对MapSession对象的实例化,如下所示,这有潜在的风险(): 是否可以从应用程序中执行此操作?如果是,请任何人提供一些相同的指针/代码参考?谢谢 编辑 在这方面,我经历了:和相关链接:和 现在看来,没有办法覆盖它,我的意思是我无法更改作为上述代码段生成的会话id。如果我错了,请纠正我 我已经阅读了相关的SO post:,但对于如何实现自定义会话

我正在为Spring boot应用程序管理会话。在这种情况下,我想用自己生成的
id
在应用程序中实例化
MapSession
对象,这样我就可以覆盖其当前对
MapSession
对象的实例化,如下所示,这有潜在的风险():

是否可以从应用程序中执行此操作?如果是,请任何人提供一些相同的指针/代码参考?谢谢

编辑

在这方面,我经历了:和相关链接:和

现在看来,没有办法覆盖它,我的意思是我无法更改作为上述代码段生成的会话id。如果我错了,请纠正我

我已经阅读了相关的SO post:,但对于如何实现自定义会话存储库没有任何清晰的想法

我已尝试实现自定义会话存储库,如下所示:

@EnableSpringHttpSession
public class CustomSessionRepository implements SessionRepository<MapSession> {

  private Integer defaultMaxInactiveInterval;
  private  Map<String, Session> sessions = new ConcurrentHashMap<>();

  public CustomSessionRepository() {
      this.sessions = sessions;
  }

  public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
    this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
  }

  public void save(MapSession session) {
    if (!session.getId().equals(session.getOriginalId())) {
      this.sessions.remove(session.getOriginalId());
    }

    this.sessions.put(session.getId(), new MapSession(session));
  }

  public MapSession findById(String id) {
    Session saved = (Session)this.sessions.get(id);
    if (saved == null) {
      return null;
    } else if (saved.isExpired()) {
      this.deleteById(saved.getId());
      return null;
    } else {
      return new MapSession(saved);
    }
  }

  public void deleteById(String id) {
    this.sessions.remove(id);
  }

  public MapSession createSession() {
    MapSession result = new MapSession(myCustomId());
    if (this.defaultMaxInactiveInterval != null) {
      result.setMaxInactiveInterval(Duration.ofSeconds((long)this.defaultMaxInactiveInterval));
    }

    return result;
  }
}
@EnableSpringHttpSession
公共类CustomSessionRepository实现SessionRepository{
私有整数defaultMaxInactiveInterval;
私有映射会话=新的ConcurrentHashMap();
公共CustomSessionRepository(){
这个。会话=会话;
}
public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval){
this.defaultMaxInactiveInterval=defaultMaxInactiveInterval;
}
公共作废保存(MapSession会话){
如果(!session.getId().equals(session.getOriginalId())){
this.sessions.remove(session.getOriginalId());
}
this.sessions.put(session.getId(),new-MapSession(session));
}
公共映射会话findById(字符串id){
会话已保存=(会话)this.sessions.get(id);
如果(已保存==null){
返回null;
}else if(saved.isExpired()){
this.deleteById(saved.getId());
返回null;
}否则{
返回新的MapSession(已保存);
}
}
公共void deleteById(字符串id){
此.sessions.remove(id);
}
公共映射会话createSession(){
MapSession结果=新的MapSession(myCustomId());
if(this.defaultMaxInactiveInterval!=null){
result.setMaxInactiveInterval(持续时间为秒((长)this.defaultMaxInactiveInterval));
}
返回结果;
}
}

但是在这里我错过了一些东西,因为现在会话没有像预期的那样持久化到Redis。

@Xenotheracide,你对此有什么想法吗,因为我看到了你的一些帖子,所以想检查一下。这有什么用?public MapSession(){this(generateId());}这段代码的最终目标是什么。。。Spring boot最有可能具有该功能。您能告诉我们您迄今为止尝试了哪些功能吗?你的步骤的结果是什么?您的问题似乎有点模糊。@实际上,到目前为止,zee会话id是用我在OP中发布的代码片段生成的。此代码是Spring会话框架的一部分。正如我在OP中提到的,以这种方式生成的会话id有潜在的风险。因此,我试图在将生成的会话id持久化到redis之前覆盖它。到目前为止,我还没有找到具体的方法来做到这一点。@sbsatter正如链接的so帖子中提到的,我正在尝试实现SessionRepository接口,以便在会话id持久化到redis之前覆盖它。但是我找不到任何这样的自定义实现作为Spring boot应用程序的一部分。@xenoterracide,你对此有什么想法吗?因为我在你的文章中很少看到关于这方面的内容,所以我想检查一下。这有什么用?public MapSession(){this(generateId());}这段代码的最终目标是什么。。。Spring boot最有可能具有该功能。您能告诉我们您迄今为止尝试了哪些功能吗?你的步骤的结果是什么?您的问题似乎有点模糊。@实际上,到目前为止,zee会话id是用我在OP中发布的代码片段生成的。此代码是Spring会话框架的一部分。正如我在OP中提到的,以这种方式生成的会话id有潜在的风险。因此,我试图在将生成的会话id持久化到redis之前覆盖它。到目前为止,我还没有找到具体的方法来做到这一点。@sbsatter正如链接的so帖子中提到的,我正在尝试实现SessionRepository接口,以便在会话id持久化到redis之前覆盖它。但我找不到任何这样的定制实现作为Spring boot应用程序的一部分。
@EnableSpringHttpSession
public class CustomSessionRepository implements SessionRepository<MapSession> {

  private Integer defaultMaxInactiveInterval;
  private  Map<String, Session> sessions = new ConcurrentHashMap<>();

  public CustomSessionRepository() {
      this.sessions = sessions;
  }

  public void setDefaultMaxInactiveInterval(int defaultMaxInactiveInterval) {
    this.defaultMaxInactiveInterval = defaultMaxInactiveInterval;
  }

  public void save(MapSession session) {
    if (!session.getId().equals(session.getOriginalId())) {
      this.sessions.remove(session.getOriginalId());
    }

    this.sessions.put(session.getId(), new MapSession(session));
  }

  public MapSession findById(String id) {
    Session saved = (Session)this.sessions.get(id);
    if (saved == null) {
      return null;
    } else if (saved.isExpired()) {
      this.deleteById(saved.getId());
      return null;
    } else {
      return new MapSession(saved);
    }
  }

  public void deleteById(String id) {
    this.sessions.remove(id);
  }

  public MapSession createSession() {
    MapSession result = new MapSession(myCustomId());
    if (this.defaultMaxInactiveInterval != null) {
      result.setMaxInactiveInterval(Duration.ofSeconds((long)this.defaultMaxInactiveInterval));
    }

    return result;
  }
}