spring boot redis 子域 session共享

2018-01-22 10:41:18来源:网络收集作者:管理员人点击

分享

[var1]

一、项目代码


1、pom文件


<?xml version="1.0" encoding="UTF-8"?>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
site.duanzy
demo
0.0.1-SNAPSHOT
war
demo
spring boot 子域session共享

org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE



UTF-8
UTF-8
1.8



org.springframework.boot
spring-boot-starter-web



org.springframework.session
spring-session



org.springframework.boot
spring-boot-starter-redis


org.springframework.session
spring-session-data-redis





org.springframework.boot
spring-boot-maven-plugin




2、DemoApplication


package site.duanzy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* 继承:SpringBootServletInitializer可以使用外部tomcat启动
*/
@RestController
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@RequestMapping("")
public String gettSeesionId(HttpServletRequest request){
HttpSession session = request.getSession();
String id = session.getId();
return id;
}
}3、Configpackage site.duanzy;
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.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
/**
* Created by dzy on 2018/1/20.
*/
@Configuration
@EnableRedisHttpSession//开启spring session支持
public class Config {
@Value("${custom.cookie.domain}")
private String cookieDomainName;
@Value("${custom.cookie.name}")
private String cookieName;
@Value("${custom.cookie.path}")
private String cookiePath;
/**
* 子域session共享
* @return
*/
@Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer();
//cookie名字
defaultCookieSerializer.setCookieName(cookieName);
//域
defaultCookieSerializer.setDomainName(cookieDomainName);
//存储路径
defaultCookieSerializer.setCookiePath(cookiePath);
return defaultCookieSerializer;
}
}
4、application.ymlspring:
session:
#设置session的保存方式
store-type: REDIS
redis:
host: 192.168.1.200
port: 6379
database: 10
pool:
max-active: 8
max-idle: 8
max-wait: -1
min-idle: 0
#自定义cookie的作用域
custom:
cookie:
domain: demo.com
name: sessionId
path: /二、nginx配置

1、www.demo.com.conf


#主站
server{listen 80;
server_name www.demo.com;
client_max_body_size 100m;location / {
proxy_passhttp://127.0.0.1:18081/demo/;
proxy_set_header Host$host;
proxy_set_header Remote_Addr$remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
}
}2、community.demo.com.conf#子域
server{listen 80;
server_name community.demo.com;
client_max_body_size 100m;location / {
proxy_passhttp://127.0.0.1:18082/demo/;
proxy_set_header Host$host;
proxy_set_header Remote_Addr$remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for;
}
}

三、HOSTS文件


127.0.0.1 www.demo.com
127.0.0.1 community.demo.com

四、部署


把项目打成war包,同时放到两个tomcat中,启动即可


五、测试

1、访问  http://www.demo.com/


spring bootredis子域session共享
2、访问:http://community.demo.com/
spring bootredis子域session共享
六、结论


 通过配置cookie的域和使用redis存放session,使得主站和和子域可以使用同一个session,达到session共享


项目代码:sb-session-redis



最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台