Explorar o código

只在controller层用reactive

yufeng0528 %!s(int64=4) %!d(string=hai) anos
pai
achega
1de77a59a2

+ 13 - 12
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/controller/LoginController.java

@@ -1,6 +1,5 @@
1 1
 package com.yaozhitech.spring5.controller;
2 2
 
3
-import javax.servlet.http.HttpServletRequest;
4 3
 import javax.servlet.http.HttpServletResponse;
5 4
 
6 5
 import org.apache.shiro.SecurityUtils;
@@ -10,18 +9,19 @@ import org.apache.shiro.subject.Subject;
10 9
 import org.springframework.beans.factory.annotation.Autowired;
11 10
 import org.springframework.http.HttpStatus;
12 11
 import org.springframework.http.ResponseEntity;
13
-import org.springframework.stereotype.Controller;
14 12
 import org.springframework.web.bind.annotation.GetMapping;
15 13
 import org.springframework.web.bind.annotation.PostMapping;
16 14
 import org.springframework.web.bind.annotation.RequestBody;
15
+import org.springframework.web.bind.annotation.RestController;
17 16
 
18 17
 import com.yaozhitech.spring5.dto.UserDto;
19 18
 import com.yaozhitech.spring5.service.UserService;
20 19
 
21 20
 import lombok.extern.slf4j.Slf4j;
21
+import reactor.core.publisher.Mono;
22 22
 
23 23
 @Slf4j
24
-@Controller
24
+@RestController
25 25
 public class LoginController {
26 26
 
27 27
 	@Autowired
@@ -34,18 +34,18 @@ public class LoginController {
34 34
 	 * @return token
35 35
 	 */
36 36
 	@PostMapping(value = "/login")
37
-	public ResponseEntity<Void> login(@RequestBody UserDto loginInfo, HttpServletRequest request,
38
-			HttpServletResponse response) {
37
+	public ResponseEntity<Mono<UserDto>> login(@RequestBody UserDto loginInfo, HttpServletResponse response) {
39 38
 		Subject subject = SecurityUtils.getSubject();
40 39
 		try {
41 40
 			UsernamePasswordToken token = new UsernamePasswordToken(loginInfo.getUsername(), loginInfo.getPassword());
42 41
 			subject.login(token);
43
-
44 42
 			UserDto user = (UserDto) subject.getPrincipal();
45
-			String newToken = userService.generateJwtToken(user.getUsername());
46
-			response.setHeader("x-auth-token", newToken);
47
-
48
-			return ResponseEntity.ok().build();
43
+			
44
+			Mono<UserDto> userMono = Mono.just(user).map(u -> {return userService.generateJwtToken(user.getUsername());})
45
+						   .doOnNext(u -> {response.setHeader("x-auth-token", u);})
46
+						   .map(u -> {return userService.getJwtTokenInfo(loginInfo.getUsername());});
47
+			
48
+			return ResponseEntity.ok(userMono);
49 49
 		} catch (AuthenticationException e) {
50 50
 			log.error("User {} login fail, Reason:{}", loginInfo.getUsername(), e.getMessage());
51 51
 			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
@@ -60,13 +60,14 @@ public class LoginController {
60 60
 	 * @return
61 61
 	 */
62 62
 	@GetMapping(value = "/logout")
63
-	public ResponseEntity<Void> logout() {
63
+	public Mono<String> logout() {
64 64
 		Subject subject = SecurityUtils.getSubject();
65 65
 		if (subject.getPrincipals() != null) {
66 66
 			UserDto user = (UserDto) subject.getPrincipals().getPrimaryPrincipal();
67 67
 			userService.deleteLoginInfo(user.getUsername());
68 68
 		}
69 69
 		SecurityUtils.getSubject().logout();
70
-		return ResponseEntity.ok().build();
70
+		return Mono.empty();
71 71
 	}
72
+	
72 73
 }

+ 5 - 7
spring5-auth/spring5-auth-server/src/main/java/com/yaozhitech/spring5/service/UserService.java

@@ -7,14 +7,12 @@ import java.util.List;
7 7
 import org.apache.shiro.crypto.hash.Sha256Hash;
8 8
 import org.springframework.beans.factory.annotation.Autowired;
9 9
 import org.springframework.beans.factory.annotation.Value;
10
-import org.springframework.data.redis.core.ReactiveStringRedisTemplate;
10
+import org.springframework.data.redis.core.StringRedisTemplate;
11 11
 import org.springframework.stereotype.Service;
12 12
 
13 13
 import com.yaozhitech.spring5.dto.UserDto;
14 14
 import com.yaozhitech.spring5.utils.JwtUtils;
15 15
 
16
-import reactor.core.publisher.Mono;
17
-
18 16
 @Service
19 17
 public class UserService {
20 18
 
@@ -22,7 +20,7 @@ public class UserService {
22 20
 	private String encryptSalt;
23 21
 
24 22
 	@Autowired
25
-	private ReactiveStringRedisTemplate redisTemplate;
23
+	private StringRedisTemplate redisTemplate;
26 24
 
27 25
 	/**
28 26
 	 * 保存user登录信息,返回token
@@ -31,7 +29,7 @@ public class UserService {
31 29
 	 */
32 30
 	public String generateJwtToken(String username) {
33 31
 		String salt = JwtUtils.generateSalt();
34
-		redisTemplate.opsForValue().set("token:"+username, salt, Duration.ofSeconds(3600)).block();
32
+		redisTemplate.opsForValue().set("token:"+username, salt, Duration.ofSeconds(3600));
35 33
 		return JwtUtils.sign(username, salt, 3600); // 生成jwt token,设置过期时间为1小时
36 34
 	}
37 35
 
@@ -42,9 +40,9 @@ public class UserService {
42 40
 	 * @return
43 41
 	 */
44 42
 	public UserDto getJwtTokenInfo(String username) {
45
-		Mono<String> salt = redisTemplate.opsForValue().get("token:"+username);
43
+		String salt = redisTemplate.opsForValue().get("token:"+username);
46 44
 		UserDto user = getUserInfo(username);
47
-		user.setSalt(salt.block());
45
+		user.setSalt(salt);
48 46
 		return user;
49 47
 	}
50 48