提交 2f194111 编写于 作者: 武汉红喜's avatar 武汉红喜

spring-boot session

上级 916692c1
......@@ -42,6 +42,8 @@
<commons-collections.version>3.2.2</commons-collections.version>
<commons-codec.version>1.11</commons-codec.version>
<commons-fileupload.version>1.3.3</commons-fileupload.version>
<commons-lang3.version>3.7</commons-lang3.version>
<commons-beanutils.version>1.9.3</commons-beanutils.version>
<log4j.version>1.2.17</log4j.version>
<servlet.version>3.1.0</servlet.version>
<velocity.version>1.7</velocity.version>
......@@ -51,7 +53,6 @@
<slf4j.version>1.7.25</slf4j.version>
<logback.version>1.2.3</logback.version>
<jackson.version>2.9.3</jackson.version>
<commons-beanutils.version>1.9.3</commons-beanutils.version>
<fastjson.version>1.2.44</fastjson.version>
<aspectj.version>1.8.13</aspectj.version>
<httpcomponents.version>4.5.3</httpcomponents.version>
......@@ -186,6 +187,12 @@
<artifactId>commons-beanutils</artifactId>
<version>${commons-beanutils.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<!-- logging -->
<dependency>
......
......@@ -18,9 +18,9 @@
<module>whatsmars-boot-sample-session</module>
</modules>
<properties>
<!--<properties>
<spring-boot.version>2.0.0.M7</spring-boot.version>
</properties>
</properties>-->
<dependencyManagement>
<dependencies>
......@@ -43,7 +43,7 @@
</plugins>
</build>
<repositories>
<!--<repositories>
<repository>
<id>spring-repo</id>
<name>Spring Repo</name>
......@@ -68,7 +68,7 @@
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</pluginRepositories>-->
</project>
\ No newline at end of file
......@@ -13,98 +13,22 @@
<name>${project.artifactId}</name>
<dependencies>
<!-- Compile -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>redis</id>
<!--<activation>-->
<!--<activeByDefault>true</activeByDefault>-->
<!--</activation>-->
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>jdbc</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>hazelcast</id>
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-hazelcast</artifactId>
</dependency>
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>mongodb</id>
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
\ No newline at end of file
package org.hongxi.whatsmars.boot.sample.session;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.alibaba.fastjson.JSON;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloRestController {
private static final String SESSION_KEY = "user";
@GetMapping("/")
String uid(HttpSession session) {
return session.getId();
if (session.getAttribute(SESSION_KEY) == null) return "not logged in";
User user = (User) session.getAttribute(SESSION_KEY);
return session.getId() + "," + JSON.toJSONString(user);
}
@GetMapping("/login")
HttpStatus login(HttpServletRequest request) {
User user = new User("qwert54321", "hongxi", 1, 28);
request.getSession(true).setAttribute(SESSION_KEY, user);
return HttpStatus.OK;
}
}
\ No newline at end of file
......@@ -2,12 +2,14 @@ package org.hongxi.whatsmars.boot.sample.session;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@EnableRedisHttpSession(redisNamespace = "sample")
@SpringBootApplication
public class SampleSessionApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleSessionApplication.class);
public static void main(String[] args) {
SpringApplication.run(SampleSessionApplication.class, args);
}
}
\ No newline at end of file
package org.hongxi.whatsmars.boot.sample.session;
import java.io.Serializable;
/**
* Created by shenhongxi on 2018/1/8.
*/
public class User implements Serializable {
private static final long serialVersionUID = -6332350121099606299L;
private String id;
private String name;
private int gender;
private int age;
public User(String id, String name, int gender, int age) {
this.id = id;
this.name = name;
this.gender = gender;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
---
spring:
security:
user:
password: password
name: user
management:
endpoints:
web:
expose: *
redis:
host: 127.0.0.1
port: 6379
password:
timeout: 0
pool:
max-idle: 8
min-idle: 1
max-active: 8
max-wait: -1
session:
store-type: redis
server:
session:
timeout: 1800
\ No newline at end of file
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.9.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<map name="spring:session:sessions">
<attributes>
<attribute extractor="org.springframework.session.hazelcast.PrincipalNameExtractor">principalName</attribute>
</attributes>
<indexes>
<index>principalName</index>
</indexes>
</map>
<network>
<join>
<multicast enabled="false"/>
</join>
</network>
</hazelcast>
\ No newline at end of file
package org.hongxi.whatsmars.boot.sample.session;
import java.net.URI;
import java.util.Base64;
import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link SampleSessionApplication}.
*
* @author Andy Wilkinson
* @author Vedran Pavic
*/
public class SampleSessionApplicationTests {
@Test
public void sessionExpiry() throws Exception {
ConfigurableApplicationContext context = createContext();
String port = context.getEnvironment().getProperty("local.server.port");
URI uri = URI.create("http://localhost:" + port + "/");
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> firstResponse = firstRequest(restTemplate, uri);
String sessionId1 = firstResponse.getBody();
String cookie = firstResponse.getHeaders().getFirst("Set-Cookie");
String sessionId2 = nextRequest(restTemplate, uri, cookie).getBody();
assertThat(sessionId1).isEqualTo(sessionId2);
Thread.sleep(1000);
String loginPage = nextRequest(restTemplate, uri, cookie).getBody();
assertThat(loginPage).containsIgnoringCase("login");
}
private ConfigurableApplicationContext createContext() {
ConfigurableApplicationContext context = new SpringApplicationBuilder()
.sources(SampleSessionApplication.class)
.properties("server.port:0", "server.session.timeout:1")
.initializers(new ServerPortInfoApplicationContextInitializer()).run();
return context;
}
private ResponseEntity<String> firstRequest(RestTemplate restTemplate, URI uri) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Basic "
+ Base64.getEncoder().encodeToString("user:password".getBytes()));
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
return restTemplate.exchange(request, String.class);
}
private ResponseEntity<String> nextRequest(RestTemplate restTemplate, URI uri,
String cookie) {
HttpHeaders headers = new HttpHeaders();
headers.set("Cookie", cookie);
RequestEntity<Object> request = new RequestEntity<>(headers, HttpMethod.GET, uri);
return restTemplate.exchange(request, String.class);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册