1.新建Maven项目,pom.xml依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<weixin-java-mp.version>2.8.0</weixin-java-mp.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.locales>zh_CN</project.build.locales>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId> </exclusion> </exclusions> -->
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- Testing Dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--spring-session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动Spring boot会报错 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<maimClass>com.meiteedu.WxMpApplication</maimClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.新建session的配置类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/**
* 这个类用配置redis服务器的连接
*
* @author mayn
*
*/
// maxInactiveIntervalInSeconds为SpringSession的过期时间(单位:秒)
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class SessionConfig {
// 冒号后的值为没有配置文件时,制动装载的默认值
@Value("${redis.hostname:localhost}")
String hostName;
@Value("${redis.port}")
int port;
@Value("${redis.password}")
String password;
@SuppressWarnings("deprecation")
@Bean
public JedisConnectionFactory connectionFactory() {
JedisConnectionFactory connection = new JedisConnectionFactory();
connection.setPort(port);
connection.setHostName(hostName);
connection.setPassword(password);
return connection;
}
}
3.新建session初始化类
import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer;
//初始化Session配置
public class SessionInitializer extends AbstractHttpSessionApplicationInitializer{
public SessionInitializer() {
super(SessionConfig.class);
}
}
4.新建一个启动类
@SpringBootApplication
@RestController
public class TestSessionApp {
@Value("${server.port}")
private String serverPort;
// 创建session 会话
// http://127.0.0.1:8080/createSession?name=aaa
@RequestMapping("/createSession")
public String createSession(HttpServletRequest request, String name) {
HttpSession session = request.getSession();
System.out.println("存入Session sessionid:信息" + session.getId() + ",name:" + name + ",serverPort:" + serverPort);
session.setAttribute("name", name);
return serverPort + "success";
}
// 获取session 会话
@RequestMapping("/getSession")
public Object getSession(HttpServletRequest request) {
// 参数为true的情况下,客户端使用对应的sessionid查不到对应的session,会创建一个新的session
// 参数为false的情况下,客户端使用对应的sessionid查不到对应的session,不会创建一个新的session
HttpSession session = request.getSession(false);
if (session == null) {
return serverPort + "服务器上没有session...";
}
System.out.println("获取Session sessionid:信息" + session.getId() + ",serverPort:" + serverPort);
Object value = session.getAttribute("name");
return serverPort + "-" + value;
}
@RequestMapping("/getServerPort")
public String getServerPort () {
return serverPort;
}
public static void main(String[] args) {
SpringApplication.run(TestSessionApp.class, args);
}
}
这里创建session和获取session可以用于单点登录,用户登录是将用户名与session关联起来,当访问其他服务的时候根据用户名查询session是否存在,存在即已登录,否则就是没有登录,另外还可以是同用户挤下线功能(这里的用户栈可以使用ServletContext,其作用域为application,在用户登录成功的时候,将sessionId存入ServletContext,并在拦截过程中,以用户名为Key值判断sessionId的一致性),当登录用户名相同,session不同时,清除原session值,并跳到首页,新用户登录,并重新保存session值。