Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9f32f71

Browse files
committedNov 29, 2020
Spring Boot集成 Spring Session
1 parent faf578f commit 9f32f71

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed
 

‎Chapter08/SpringSessionDemo/src/main/java/com/dxtd/SpringSessionDemo/RedisHttpSessionConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
55

66
@Configuration
7-
//maxInactiveIntervalInSeconds 默认是1800秒过期,这里测试修改为60秒
7+
//maxInactiveIntervalInSeconds 默认是 1800 秒过期,这里测试修改为 120 秒
88
@EnableRedisHttpSession(maxInactiveIntervalInSeconds=120)
99
public class RedisHttpSessionConfiguration {
1010

‎Chapter08/SpringSessionDemo/src/main/java/com/dxtd/SpringSessionDemo/controller/SessionController.java

+35-22
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,57 @@
99
@RestController
1010
public class SessionController {
1111

12-
// http://127.0.0.1:8080/session/login?username=xinping&password=123
13-
// http://127.0.0.1:8081/session/login?username=xinping&password=123
12+
/**
13+
* 登录系统
14+
*
15+
* http://127.0.0.1:8080/session/login?username=xinping&password=123
16+
* http://127.0.0.1:8081/session/login?username=xinping&password=123
17+
* */
1418
@RequestMapping(value="/session/login", method = RequestMethod.GET)
1519
@ResponseBody
1620
public Map login(@RequestParam("username") String username,@RequestParam("password") String password,HttpServletRequest request, HttpSession session) {
17-
System.out.println("===============登录成功===============");
18-
User user = new User();
19-
user.setName(username);
20-
user.setPassword(password);
21-
request.getSession().setAttribute("admin", user);
22-
String sessionId = session.getId();
23-
System.out.println("sessionId="+sessionId);
24-
Map result = new HashMap();
25-
result.put("message" , user);
26-
result.put("sessionId",sessionId);
21+
String message = "login failure";
22+
Map<String, Object> result = new HashMap<String, Object>();
23+
if(username != null && "xinping".equals(username) && "123".equals(password)){
24+
User user = new User();
25+
user.setName(username);
26+
user.setPassword(password);
27+
request.getSession().setAttribute("admin", user);
28+
message = "login success";
29+
result.put("message" , message);
30+
result.put("sessionId",session.getId());
31+
}else{
32+
result.put("message" , message);
33+
}
34+
2735
return result;
2836
}
2937

30-
// http://127.0.0.1:8080/session/get?username=xinping
31-
// http://127.0.0.1:8082/session/get?username=xinping
38+
/**
39+
* 查询用户
40+
*
41+
* http://127.0.0.1:8080/session/get?username=xinping
42+
* http://127.0.0.1:8082/session/get?username=xinping
43+
* */
3244
@RequestMapping(value="/session/get", method = RequestMethod.GET)
3345
@ResponseBody
3446
public Map get(@RequestParam("username") String username,HttpServletRequest request, HttpSession session) {
35-
System.out.println("=============== 查询用户信息 ===============");
3647
Object value = request.getSession().getAttribute("admin");
37-
Map result = new HashMap();
38-
String sessionId = session.getId();
48+
Map<String,Object> result = new HashMap<String,Object>();
3949
result.put("message" ,value);
40-
result.put("sessionId",sessionId);
50+
result.put("sessionId",session.getId());
4151
return result;
4252
}
4353

44-
// http://127.0.0.1:8080/session/logout
45-
@RequestMapping(value = "logout", method = RequestMethod.GET)
54+
/**
55+
* 退出系统
56+
*
57+
* http://127.0.0.1:8080/session/logout
58+
* http://127.0.0.1:8081/session/logout
59+
* */
60+
@RequestMapping(value = "/session/logout", method = RequestMethod.GET)
4661
@ResponseBody
4762
public Map logout(HttpSession session) {
48-
System.out.println("============退出系统=============");
49-
System.out.println(session.getId());
5063
session.removeAttribute("admin");
5164
session.invalidate();
5265
Map result = new HashMap();

0 commit comments

Comments
 (0)
Please sign in to comment.