spring boot redis 子域 session共享

一、项目代码

1、pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>site.duanzy</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>demo</name>
	<description>spring boot 子域session共享</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!--spring session-->
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session</artifactId>
		</dependency>
		<!-- springboot整合 redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

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、Config

package 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.yml

spring:
  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_pass  http://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_pass  http://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/


2、访问:http://community.demo.com/

六、结论

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

项目代码:sb-session-redis


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值