如何在一个应用程序中配置弹簧会议以支持HeaderHttpSessionstrategy和CookiehttpSessi



我有一个项目要支持Android,iOS,PC Web。我尝试使用https://github.com/spring-projects/spring-session,可以在应用程序中进行配置以支持httpsession和REST代币?

如果可以的话,我可以如何配置?

对我有用的一件事是使用cookie和header实现的代码重新进来***HttpSessionStrategy

您可以在此处看到这些类的具体实现:

  • https://github.com/spring-projects/spring-session/blob/master/spring-session/src/src/main/java/java/java/springframework/session/sessess/web/web/http/headerhheaderhheaderhheaderhttpsessessionstrategy.java
  • https://raw.githubusercontent.com/spring-projects/spring-session/master/master/spring-session/src/main/java/java/java/java/springframework/sessess/web/web/web/http/http/http/cookiehtppsesseststrategy.java

所以getRequestedSessionId变为:

public String getRequestedSessionId(HttpServletRequest request) {
        // header part
        String sessionId = request.getHeader(headerName);
        if(sessionId != null && !sessionId.isEmpty())
            return sessionId;
        // cookie part
        Map<String,String> sessionIds = getSessionIds(request);
        String sessionAlias = getCurrentSessionAlias(request);
        return sessionIds.get(sessionAlias);
    }

onInvalidateSession变为:

public void onInvalidateSession(HttpServletRequest request, HttpServletResponse response) {
    // header part
    response.setHeader(headerName, "");
    // cookie part
    Map<String,String> sessionIds = getSessionIds(request);
    String requestedAlias = getCurrentSessionAlias(request);
    sessionIds.remove(requestedAlias);
    Cookie sessionCookie = createSessionCookie(request, sessionIds);
    response.addCookie(sessionCookie);
}

onNewSession变为:

public void onNewSession(Session session, HttpServletRequest request, HttpServletResponse response) {
        // header part
        response.setHeader(headerName, session.getId());
        // cookie part
        Set<String> sessionIdsWritten = getSessionIdsWritten(request);
        if(sessionIdsWritten.contains(session.getId())) {
            return;
        }
        sessionIdsWritten.add(session.getId());
        Map<String,String> sessionIds = getSessionIds(request);
        String sessionAlias = getCurrentSessionAlias(request);
        sessionIds.put(sessionAlias, session.getId());
        Cookie sessionCookie = createSessionCookie(request, sessionIds);
        response.addCookie(sessionCookie);
    }

我将其用于浏览器/移动REST API,它适合我的所有需求。

您可以利用Spring的BeanFactoryPostProcessor,覆盖后期fercessafterinitialization()来配置SesterRepositoryFilter的httpsessionstrategy,通过它的sethttpsessionstategy()在春季的初始化之后。

相关内容

  • 没有找到相关文章

最新更新