Spring session + SpringBoot + redis 实现session共享

redis 安装参考: https://blog.csdn.net/u013792404/article/details/93873585

nginx安装参考: https://mp.csdn.net/postedit/93863306

 

springboot 版本:2.0.6

maven依赖:   spring-boot-starter-data-redis   和   spring-session-data-redis

<?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>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.mei.springboot.springSession</groupId>
	<artifactId>SpringBoot-SpringSession</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<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-data-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-data-redis</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
		</dependency>

	</dependencies>


	<build>
		<!-- -->
		<finalName>${project.artifactId}</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<configuration>
					<encoding>UTF-8</encoding>
					<useDefaultDelimiters>true</useDefaultDelimiters>
				</configuration>
			</plugin>
			<plugin>
				<!-- maven插件 -->
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<!-- 此处必须指定为1.4.2.RELEASE , 为SpringBoot的bug -->
				<!-- 打包只能用1.4.2.RELEASE 版本的,其它版本的都不可以。这个目前是springboot的bug。目前还未看到官方的解说。 -->
				<version>1.4.2.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
				<configuration>
					<mainClass>com.mei.springboot.rocket.SpringBootTestApplication</mainClass>
					<includeSystemScope>true</includeSystemScope>
				</configuration>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
			<!-- java编译插件 -->
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<!-- <version>3.3</version> -->
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
					<encoding>UTF-8</encoding>
				</configuration>
			</plugin>
			<!-- 避免报错: pom.xml报错 web.xml is missing and <failOnMissingWebXml> is set 
				to true -->
			<!-- <plugin> -->
			<!-- <groupId>org.apache.maven.plugins</groupId> -->
			<!-- <artifactId>maven-war-plugin</artifactId> -->
			<!-- <configuration> -->
			<!-- <failOnMissingWebXml>false</failOnMissingWebXml> -->
			<!-- </configuration> -->
			<!-- </plugin> -->
		</plugins>
		<!-- 此处最为重要-否则打包后没有你的JSP页面模板,一定要记得写为资源文件 targetPath 设置必须是 META-INF/resources 
			我项目做出来的时候因为没写resources导致打包运行后正常启动,各个接口也都正常访问,但是所有的jsp页面都会报404,找不到页面 ,因为这个问题浪费了我大半天时间 -->
		<resources>
			<!-- 打包时将jsp文件拷贝到META-INF目录下 -->
			<resource>
				<!-- 指定resources插件处理哪个目录下的资源文件 -->
				<directory>src/main/webapp</directory>
				<!--注意此次必须要放在此目录下才能被访问到 -->
				<targetPath>META-INF/resources</targetPath>
				<includes>
					<include>**/**</include>
				</includes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/**</include>
				</includes>
				<!-- 这个要加上,不然fontswone等字体库读不到 -->
				<filtering>false</filtering>
			</resource>
			<resource>
				<directory>src/main/java</directory>
				<excludes>
					<exclude>**/*.java</exclude>
				</excludes>
			</resource>
		</resources>
	</build>


</project>

 

RedisSessionConfig.java

package com.springboot.rocket.controller;

import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@Configuration  
// 配置session过期时间 , 30分钟
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class RedisSessionConfig {

}

 

session 测试类

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping(value = "/session", method = RequestMethod.GET)  
    public Object sessions (HttpServletRequest request){  
        Map<String, Object> map = new HashMap<>();  
        map.put("sessionId", request.getSession().getId());  
        map.put("host.port", "8081"); 
        return map;  
    } 
}

application.yml


server:
  port: 8081

      
spring:
  redis:
    host: 192.168.8.107 
    port: 6379
  application:
    name: SpringSession

      

 

将项目打包成2个jar包,springboot打jar包运行参考: https://blog.csdn.net/u013792404/article/details/88145646

 

项目启动后配置到nginx;nginx.conf配置如下:

 

 

访问: http://192.168.8.107 , 结果如下:

发现端口不同(即不同应用), 但sessionId 是相同的, 即实现了session共享。 

 

 

2、简单的登陆案例: 

打2个jar包, 其中只有端口不同。 分别为8080和8081

package com.mei.springboot.rocket.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@RequestMapping(value = "/login", method = RequestMethod.GET)  
	public Object login (String name,String pass ,HttpServletRequest request){  
		
		if("zhangsan".equals(name) && "123456".equals(pass)) {
			HttpSession session = request.getSession();
			session.setAttribute("user", "zhangsan");
		
			Map<String, Object> map = new HashMap<>();  
			map.put("login", name + " , 登陆成功" );  
			map.put("host.port", "8080"); 
			return map; 
		} else {
			Map<String, Object> map = new HashMap<>();  
			map.put("login", name + " , 失败!" );  
			map.put("host.port", "8080"); 
			return map; 
		}
		
	} 
    @RequestMapping(value = "/operate", method = RequestMethod.GET)  
    public Object operate (HttpServletRequest request){  
    	HttpSession session = request.getSession();
    	String user = (String)session.getAttribute("user");
    	if(StringUtils.isNotBlank(user) && !"null".equals(user)) {
    		Map<String, Object> map = new HashMap<>();  
			map.put("operate", user + " , 操作成功!" );  
			map.put("host.port", "8080"); 
			return map; 
    	} else {
    		Map<String, Object> map = new HashMap<>();  
			map.put("operate", user + " , 操作失败!" );  
			map.put("host.port", "8080"); 
			return map; 
    	}
    } 
}

 

配合nginx部署启动后, 进行模拟登陆: http://192.168.8.107/login?name=zhangsan&pass=123456

是在8080端口登陆的, 

但是在8081上也能访问登陆用户信息, 证明session共享实现。 

 

 

 

springboot + spring session + redis集群参考:  https://blog.csdn.net/qq_39089022/article/details/82347843

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值