日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

基于SpringBoot,SpringSession和redis的会话共享

發(fā)布時間:2024/2/28 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于SpringBoot,SpringSession和redis的会话共享 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

安裝redis

$ wget http://download.redis.io/releases/redis-4.0.1.tar.gz $ tar xzf redis-4.0.1.tar.gz $ cd redis-4.0.1 $ make 配置redis.conf,允許外部連接




使用IntelliJ IDEA創(chuàng)建Spring Boot工程,選擇web, redis, session.

LoginController.java

package com.example.springsessiondemo;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession;@Controller public class LoginController {@RequestMapping("/")@ResponseBodypublic String home(HttpServletRequest request, HttpServletResponse response) {HttpSession session = request.getSession();User user = (User) session.getAttribute("user");if (user == null){return "用好未登錄" + session.getId();} else {return "用好已登錄" + session.getId();}}@RequestMapping("/login")@ResponseBodypublic String login(HttpServletRequest request){User user = new User();user.setUsername("chf");user.setPassword("pass");request.getSession().setAttribute("user", user);return "登錄成功 " + request.getSession().getId();} }


創(chuàng)建文件config.java

@Configuration @ImportResource(locations={"classpath:spring.xml"}) public class Config { }

配置文件application.yaml

server:port: 8000 spring:session:store-type: redisredis:host: 10.0.0.201port: 6379password:
配置文件spring.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="defaultCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer"><property name="domainName" value="example.com"/><property name="cookieName" value="JSESSIONID"/><property name="cookiePath" value="/" /><!-- <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"></property>--></bean><bean id="redisHttpSessionConfiguration" class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"><property name="maxInactiveIntervalInSeconds" value="1800" /><property name="cookieSerializer" ref="defaultCookieSerializer"/></bean> </beans>
配置hosts文件

127.0.0.1 www.example.com
127.0.0.1 passport.example.com


運行程序,通過瀏覽器訪問passport.example.com:8000



訪問www.example.com:8000


JSESSIONID的內(nèi)容相同,說明實現(xiàn)了會話共享。



接下來在passport進(jìn)行登錄



切換到www


一個簡單的單點登錄系統(tǒng)就實現(xiàn)了。

源代碼工程

https://github.com/chenhaifeng2016/springsessiondemo

總結(jié)

以上是生活随笔為你收集整理的基于SpringBoot,SpringSession和redis的会话共享的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。