SpringBoot 利用SQLServer管理Session與Session Cookie

最近的項目要求指定的數據庫只能是SQLServer,後端開發採用了SpringBoot,所以SpringBoot自帶的利用Redis的Session管理機制就用不了,查找了很多的資料後,找到了利用SQLServer管理Session的解決辦法。
首先,需要導入一些依賴,因爲我是maven管理的項目,所以在pom.xml下面添加如下依賴

        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.1.0.jre8</version>
        </dependency>

然後,創建一個類叫做HttpSessionConfig,內容如下:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

@Configuration
@EnableJdbcHttpSession
public class HttpSessionConfig {

}

修改配置文件application.properties如下:

spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://ipaddress:port;databaseName = xxx
spring.datasource.username=xxx
spring.datasource.password=xxxxx
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2008Dialect
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
spring.session.store-type=jdbc

因爲,基於SQLServer的Session管理,SpringBoot不會給我們自動創建存儲Session的表,所以這些表我們需要自己創建。
進入SQLServer數據,執行如下SQL代碼:

use xxx[你的數據名稱];
CREATE TABLE SPRING_SESSION (
        SESSION_ID CHAR(36) NOT NULL,
        CREATION_TIME BIGINT NOT NULL,
        LAST_ACCESS_TIME BIGINT NOT NULL,
        MAX_INACTIVE_INTERVAL INT NOT NULL,
        PRINCIPAL_NAME VARCHAR(100),
        CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (SESSION_ID)
);

CREATE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (LAST_ACCESS_TIME);

CREATE TABLE SPRING_SESSION_ATTRIBUTES (
        SESSION_ID CHAR(36) NOT NULL,
        ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
        ATTRIBUTE_BYTES VARBINARY(1024) NOT NULL,
        CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_ID, ATTRIBUTE_NAME),
        CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_ID) REFERENCES SPRING_SESSION(SESSION_ID) ON DELETE CASCADE
);

CREATE INDEX SPRING_SESSION_ATTRIBUTES_IX1 ON SPRING_SESSION_ATTRIBUTES (SESSION_ID);

這樣,重啓SpringBoot服務器,你的Session就會基於SQLSever數據庫管理啦。

因爲Springboot默認的Session Cookie 的時間是1800s,有時候我們想改變session cookie的一些狀態怎麼辦? 比如,我希望客戶端可以讀取到這個session cookie的信息,改變session cookie 的存在時間等等。
我們發現僅僅進行session的設置是不行的,我們需要操作的是session cookie,也就是session發送到客戶端的那個cookie。
爲了達到我們的目標,僅僅對剛纔新建的類HttpSessionConfig做一些更改即可,下面的更改之後的HttpSessionConfig類:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

@Configuration
@EnableJdbcHttpSession
public class HttpSessionConfig {

    //自定義session cookie信息
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookiePath("/");
        serializer.setUseHttpOnlyCookie(false);
        serializer.setCookieMaxAge(259200);
        return serializer;
    }
}

這裏我麼設置了session cookie的path, 並且設置http-only爲false允許客戶端訪問這個cookie,設置最大的cookie存在時間爲259200秒也就是3天。

參考鏈接:
[1] https://docs.spring.io/spring-session/docs/current/reference/html5/guides/httpsession-jdbc.html
[2] https://stackoverflow.com/questions/37398905/spring-session-with-jdbc-configuration-table-test-spring-session-doesnt-exis
[3] https://docs.spring.io/spring-session/docs/current/reference/html5/guides/custom-cookie.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章