提交 | 用户 | 时间
|
c18e5a
|
1 |
package com.iailab.module.sms.controller.admin.auth; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.StrUtil; |
|
4 |
import com.iailab.framework.common.pojo.CommonResult; |
|
5 |
import com.iailab.module.sms.client.OAuth2Client; |
|
6 |
import com.iailab.module.sms.client.dto.oauth2.OAuth2AccessTokenRespDTO; |
|
7 |
import com.iailab.module.sms.util.SecurityUtils; |
|
8 |
import org.springframework.web.bind.annotation.PostMapping; |
|
9 |
import org.springframework.web.bind.annotation.RequestMapping; |
|
10 |
import org.springframework.web.bind.annotation.RequestParam; |
|
11 |
import org.springframework.web.bind.annotation.RestController; |
|
12 |
|
|
13 |
import javax.annotation.Resource; |
|
14 |
import javax.annotation.security.PermitAll; |
|
15 |
import javax.servlet.http.HttpServletRequest; |
|
16 |
|
|
17 |
@RestController |
|
18 |
@RequestMapping("/sms/auth") |
|
19 |
public class AuthController { |
|
20 |
|
|
21 |
@Resource |
|
22 |
private OAuth2Client oauth2Client; |
|
23 |
|
|
24 |
/** |
|
25 |
* 使用 code 访问令牌,获得访问令牌 |
|
26 |
* |
|
27 |
* @param code 授权码 |
|
28 |
* @param redirectUri 重定向 URI |
|
29 |
* @return 访问令牌;注意,实际项目中,最好创建对应的 ResponseVO 类,只返回必要的字段 |
|
30 |
*/ |
|
31 |
@PostMapping("/loginBycode") |
|
32 |
@PermitAll |
|
33 |
public CommonResult<OAuth2AccessTokenRespDTO> loginByCode(@RequestParam("code") String code, |
|
34 |
@RequestParam("redirectUri") String redirectUri) { |
|
35 |
return oauth2Client.postAccessToken(code, redirectUri); |
|
36 |
} |
|
37 |
|
|
38 |
/** |
|
39 |
* 使用刷新令牌,获得(刷新)访问令牌 |
|
40 |
* |
|
41 |
* @param refreshToken 刷新令牌 |
|
42 |
* @return 访问令牌;注意,实际项目中,最好创建对应的 ResponseVO 类,只返回必要的字段 |
|
43 |
*/ |
|
44 |
@PostMapping("/refresh-token") |
|
45 |
public CommonResult<OAuth2AccessTokenRespDTO> refreshToken(@RequestParam("refreshToken") String refreshToken) { |
|
46 |
return oauth2Client.refreshToken(refreshToken); |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* 退出登录 |
|
51 |
* |
|
52 |
* @param request 请求 |
|
53 |
* @return 成功 |
|
54 |
*/ |
|
55 |
@PostMapping("/logout") |
|
56 |
public CommonResult<Boolean> logout(HttpServletRequest request) { |
|
57 |
String token = SecurityUtils.obtainAuthorization(request, "Authorization"); |
|
58 |
if (StrUtil.isNotBlank(token)) { |
|
59 |
return oauth2Client.revokeToken(token); |
|
60 |
} |
|
61 |
// 返回成功 |
|
62 |
return new CommonResult<>(); |
|
63 |
} |
|
64 |
|
|
65 |
} |