jiangping
2024-12-10 eb275c6d06d3c27fd30bbf4975d27c93d7f56eb2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package com.doumee.api.gateway;
 
import com.doumee.config.annotation.LoginNoRequired;
import com.doumee.config.jwt.JwtProperties;
import com.doumee.config.jwt.JwtTokenUtil;
import com.doumee.core.annotation.pr.PreventRepeat;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.model.ApiResponse;
import com.doumee.core.model.LoginUserInfo;
import com.doumee.core.utils.Constants;
import com.doumee.dao.system.dto.LoginDTO;
import com.doumee.dao.system.dto.LoginPhoneDTO;
import com.doumee.service.common.CaptchaService;
import com.doumee.service.system.SystemLoginService;
import com.doumee.service.system.SystemUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;
 
import javax.annotation.Resource;
import java.util.Objects;
 
/**
 * JWT获取令牌和刷新令牌接口
 */
@RestController
@Api(tags ="鉴权登录接口")
public class JwtAuthController {
 
    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private SystemUserService systemUserService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;
 
    @Autowired
    private SystemLoginService systemLoginService;
    @Autowired
    private CaptchaService captchaService;
 
    /**
     * @author Eva.Caesar Liu
     * @date 2023/03/21 14:49
     */
    @ApiOperation("获取图片验证码")
    @GetMapping("/captcha")
    public ApiResponse<CaptchaService.Captcha> getCaptcha() {
        return ApiResponse.success(captchaService.genCaptcha());
    }
 
    @PreventRepeat(limit = 10, lockTime = 10000)
    @ApiOperation("登录")
    @PostMapping("/login")
    @LoginNoRequired
    public ApiResponse<String> login (@Validated @RequestBody LoginDTO dto, ServerWebExchange serverWebExchange) {
        try {
            ServerHttpRequest request = serverWebExchange.getRequest();
            dto.setCheckCode(true);
            LoginUserInfo user = systemLoginService.loginByPasswordNew(dto,request);
            String token = jwtTokenUtil.generateToken(user);
            return ApiResponse.success(token);
        }catch (BusinessException e){
            return ApiResponse.failed(e.getCode(),e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
        }
    }
 
 
    @PreventRepeat(limit = 10, lockTime = 10000)
    @ApiOperation("短信验证码登录")
    @PostMapping("/loginByPhone")
    @LoginNoRequired
    public ApiResponse<String> loginByPhone (@Validated @RequestBody LoginPhoneDTO dto, ServerWebExchange serverWebExchange) {
        try {
            ServerHttpRequest request = serverWebExchange.getRequest();
            LoginUserInfo user = systemLoginService.loginByPhone(dto,request);
            String token = jwtTokenUtil.generateToken(user);
            return ApiResponse.success(token);
        }catch (BusinessException e){
            return ApiResponse.failed(e.getCode(),e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
        }
    }
 
 
 
    @PreventRepeat(limit = 10, lockTime = 10000)
    @ApiOperation("H5业务登录")
    @PostMapping("/loginH5")
    @LoginNoRequired
    public ApiResponse<String> loginH5 (@Validated @RequestBody LoginDTO dto, ServerWebExchange serverWebExchange) {
        try {
            ServerHttpRequest request = serverWebExchange.getRequest();
            dto.setCheckCode(false);
            LoginUserInfo user = systemLoginService.loginByPasswordNew(dto,request);
            String token = jwtTokenUtil.generateToken(user);
            return ApiResponse.success(token);
        }catch (BusinessException e){
            return ApiResponse.failed(e.getCode(),e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
        }
    }
 
    @PreventRepeat(limit = 10, lockTime = 10000)
    @ApiOperation("司机登录")
    @PostMapping("/loginDriver")
    @LoginNoRequired
    public ApiResponse<String> loginDriver (@Validated @RequestBody LoginDTO dto, ServerWebExchange serverWebExchange) {
        try {
            ServerHttpRequest request = serverWebExchange.getRequest();
            dto.setCheckCode(false);
            LoginUserInfo user = systemLoginService.driverLogin(dto,request);
            String token = jwtTokenUtil.generateToken(user);
            return ApiResponse.success(token);
        }catch (BusinessException e){
            return ApiResponse.failed(e.getCode(),e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
        }
    }
 
    /**
     * 刷新JWT令牌,用旧的令牌换新的令牌
     */
    @PostMapping("/refreshtoken")
    @ApiOperation("刷新token")
    public ApiResponse<String> refreshtoken(@RequestHeader(Constants.HEADER_USER_TOKEN) String oldToken){
        try {
            if(!jwtTokenUtil.isTokenExpired(oldToken)){
                return ApiResponse.success(jwtTokenUtil.refreshToken(oldToken));
            }
            return ApiResponse.failed("token已失效");
        }catch (BusinessException e){
            return ApiResponse.failed(e.getCode(),e.getMessage());
        }catch (Exception e){
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
        }
    }
    /**
     * 刷新JWT令牌,用旧的令牌换新的令牌
     */
    @PostMapping("/logout")
    @ApiOperation("退出登陆")
    public ApiResponse<String> logout(@RequestHeader(Constants.HEADER_USER_TOKEN) String oldToken,Integer isH5){
        try {
            if(Objects.nonNull(isH5)){
                jwtTokenUtil.logoutForH5(oldToken);
            }else{
                jwtTokenUtil.logout(oldToken);
            }
        }catch (BusinessException e){
            return ApiResponse.failed(e.getCode(),e.getMessage());
        }catch (Exception e){
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
        }
        return ApiResponse.success(null);
 
    }
 
 
 
    /**
     * 刷新JWT令牌,用旧的令牌换新的令牌
     */
    @GetMapping("/getUserInfo")
    @ApiOperation("获取当前登陆用户")
    public ApiResponse<LoginUserInfo> getUserInfo(@RequestHeader(Constants.HEADER_USER_TOKEN) String token){
        try {
            LoginUserInfo user =jwtTokenUtil.getUserInfoByToken(token);
           if(user !=null){
               return ApiResponse.success( user);
           }
        }catch (BusinessException e){
            return ApiResponse.failed(e.getCode(),e.getMessage());
        }catch (Exception e){
            return ApiResponse.failed(ResponseStatus.SERVER_ERROR);
        }
        return ApiResponse.failed( "登录已失效");
    }
 
}