新增用例
This commit is contained in:
@ -0,0 +1,16 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800) //开启redis session支持,并配置session过期时间
|
||||
public class SpringBootSessionApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootSessionApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.springboot.bean;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
*/
|
||||
@Data
|
||||
public class User implements Serializable {
|
||||
|
||||
private long userId;
|
||||
private String username;
|
||||
private String password;
|
||||
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
package com.heibaiying.springboot.controller;
|
||||
|
||||
|
||||
import com.heibaiying.springboot.bean.User;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 登录
|
||||
*/
|
||||
@Controller
|
||||
public class LoginController {
|
||||
|
||||
@RequestMapping
|
||||
public String index() {
|
||||
return "index";
|
||||
}
|
||||
|
||||
@RequestMapping("home")
|
||||
public String home() {
|
||||
return "home";
|
||||
}
|
||||
|
||||
@PostMapping("login")
|
||||
public String login(User user, HttpSession session) {
|
||||
// 随机生成用户id
|
||||
user.setUserId(Math.round(Math.floor(Math.random() * 10 * 1000)));
|
||||
// 将用户信息保存到id中
|
||||
session.setAttribute("USER", user);
|
||||
return "home";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
spring:
|
||||
redis:
|
||||
host: 127.0.0.1
|
||||
port: 6379
|
||||
jedis:
|
||||
pool:
|
||||
# 连接池最大连接数,使用负值表示无限制。
|
||||
max-active: 8
|
||||
# 连接池最大阻塞等待时间,使用负值表示无限制。
|
||||
max-wait: -1s
|
||||
# 连接池最大空闲数,使用负值表示无限制。
|
||||
max-idle: 8
|
||||
# 连接池最小空闲连接,只有设置为正值时候才有效
|
||||
min-idle: 1
|
||||
timeout: 300ms
|
||||
session:
|
||||
# session 存储方式 支持redis、mongo、jdbc、hazelcast
|
||||
store-type: redis
|
||||
|
||||
# 如果是集群节点 采用如下配置指定节点
|
||||
#spring.redis.cluster.nodes
|
@ -0,0 +1,10 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>主页面</title>
|
||||
</head>
|
||||
<body>
|
||||
<h5>登录用户: ${Session["USER"].username} </h5>
|
||||
<h5>用户编号: ${Session["USER"].userId} </h5>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,13 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>登录页面</title>
|
||||
</head>
|
||||
<body>
|
||||
<form action="/login" method="post">
|
||||
用户:<input type="text" name="username"><br/>
|
||||
密码:<input type="password" name="password"><br/>
|
||||
<button type="submit">登录</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1,17 @@
|
||||
package com.heibaiying.springboot;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringBootSessionApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user