srpingboot 自定义session存储在header中

package com.tfe.sell.common.config;

import org.springframework.session.Session;
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;

public class UserDefindeHttpSessionStrategy extends HeaderHttpSessionIdResolver {

    private static final String HEADER_X_AUTH_TOKEN = "x-auth-token";

    private static final String HEADER_AUTHENTICATION_INFO = "Authentication-Info";

    private final String headerName;

    /**
     * Convenience factory to create {@link HeaderHttpSessionIdResolver} that uses
     * "X-Auth-AccessToken" header.
     * @return the instance configured to use "X-Auth-AccessToken" header
     */
    public static HeaderHttpSessionIdResolver xAuthToken() {
        return new HeaderHttpSessionIdResolver(HEADER_X_AUTH_TOKEN);
    }

    /**
     * Convenience factory to create {@link HeaderHttpSessionIdResolver} that uses
     * "Authentication-Info" header.
     * @return the instance configured to use "Authentication-Info" header
     */
    public static HeaderHttpSessionIdResolver authenticationInfo() {
        return new HeaderHttpSessionIdResolver(HEADER_AUTHENTICATION_INFO);
    }


    /**
     * The name of the header to obtain the session id from.
     * @param headerName the name of the header to obtain the session id from.
     */
    public UserDefindeHttpSessionStrategy(String headerName) {
        super(headerName);
        this.headerName = headerName;
    }

    @Override
    public List resolveSessionIds(HttpServletRequest request) {
        String headerValue = request.getHeader(this.headerName);
        return headerValue != null ? Collections.singletonList(headerValue)
                : Collections.emptyList();
    }

    @Override
    public void setSessionId(HttpServletRequest request, HttpServletResponse response,
                             String sessionId) {
        response.setHeader(this.headerName, sessionId);
    }

    @Override
    public void expireSession(HttpServletRequest request, HttpServletResponse response) {
        response.setHeader(this.headerName, "");
    }

}

配置到

package com.tfe.sell.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
import org.springframework.session.web.http.HttpSessionIdResolver;

@Configuration
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 60*60)
public class RedisSessionConfig {
/**
     * 使用Header进行传输
     * @return
     */
    @Bean
    public HttpSessionIdResolver httpSessionStrategy() {
        return new UserDefindeHttpSessionStrategy("x-auth-token");
    }

}

 

转载于:https://www.cnblogs.com/powerwu/articles/10137246.html

你可能感兴趣的:(srpingboot 自定义session存储在header中)