package com.cirle.scientific.config;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.RedisOperationsSessionRepository;
import org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration;
import org.springframework.session.web.http.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.DefaultCookieSerializer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.PostConstruct;


@Configuration
@Slf4j
public class RedisSessionConfiguration extends RedisHttpSessionConfiguration {


    @Value("${spring.session.cookieName:JSESSIONID}")
    private String cookieName;

    @Value("${spring.session.timeout:30}")
    int sessionTimeoutInMinuts;

    @PostConstruct
    @Override
    public void init() {
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
        cookieSerializer.setCookieName(cookieName);//sessionId名称
        cookieSerializer.setUseBase64Encoding(false);
        CookieHttpSessionIdResolver cookieHttpSessionIdResolver = new CookieHttpSessionIdResolver();
        cookieHttpSessionIdResolver.setCookieSerializer(cookieSerializer);
        super.setHttpSessionIdResolver(cookieHttpSessionIdResolver);
        super.setCookieSerializer(cookieSerializer);
    }

    @Autowired
    private RedisOperationsSessionRepository sessionRepository;
    @PostConstruct
    private void afterPropertiesSet() {
        log.info("setting sesion 超时时间 = [{}]分钟",sessionTimeoutInMinuts);
        sessionRepository.setDefaultMaxInactiveInterval(sessionTimeoutInMinuts * 60);
        //sessionRepository.setRedisKeyNamespace(redisnamespace);

    }

}
