|
@@ -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
|
}
|