|
@@ -4,6 +4,7 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Set;
|
|
|
+
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.core.ParameterizedTypeReference;
|
|
|
import org.springframework.http.*;
|
|
@@ -31,8 +32,7 @@ import org.springframework.web.client.RestTemplate;
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
@RestController
|
|
|
-public class SysLoginController
|
|
|
-{
|
|
|
+public class SysLoginController {
|
|
|
@Autowired
|
|
|
private SysLoginService loginService;
|
|
|
|
|
@@ -46,6 +46,7 @@ public class SysLoginController
|
|
|
private TokenService tokenService;
|
|
|
// @Resource // 可优化,注册一个 RestTemplate Bean,然后注入
|
|
|
private final RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
/**
|
|
|
* 登录方法
|
|
|
*
|
|
@@ -53,8 +54,7 @@ public class SysLoginController
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@PostMapping("/login")
|
|
|
- public AjaxResult login(@RequestBody LoginBody loginBody)
|
|
|
- {
|
|
|
+ public AjaxResult login(@RequestBody LoginBody loginBody) {
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
// 生成令牌
|
|
|
String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
|
|
@@ -69,8 +69,7 @@ public class SysLoginController
|
|
|
* @return 用户信息
|
|
|
*/
|
|
|
@GetMapping("getInfo")
|
|
|
- public AjaxResult getInfo()
|
|
|
- {
|
|
|
+ public AjaxResult getInfo() {
|
|
|
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
|
|
SysUser user = loginUser.getUser();
|
|
|
// 角色集合
|
|
@@ -90,8 +89,7 @@ public class SysLoginController
|
|
|
* @return 路由信息
|
|
|
*/
|
|
|
@GetMapping("getRouters")
|
|
|
- public AjaxResult getRouters()
|
|
|
- {
|
|
|
+ public AjaxResult getRouters() {
|
|
|
LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
|
|
|
// 用户信息
|
|
|
SysUser user = loginUser.getUser();
|
|
@@ -106,8 +104,7 @@ public class SysLoginController
|
|
|
* @return 结果
|
|
|
*/
|
|
|
@PostMapping("/getAccessToken")
|
|
|
- public AjaxResult getAccessToken(@RequestBody LoginBody loginBody)
|
|
|
- {
|
|
|
+ public AjaxResult getAccessToken(@RequestBody LoginBody loginBody) {
|
|
|
AjaxResult ajax = AjaxResult.success();
|
|
|
// 生成令牌
|
|
|
String code = loginBody.getCode();
|
|
@@ -115,20 +112,21 @@ public class SysLoginController
|
|
|
// 1.1 构建请求头
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- headers.add("Authorization", "Bearer " );
|
|
|
+ headers.add("Authorization", "Bearer ");
|
|
|
// 1.2 构建请求参数
|
|
|
- Map<String,String> body = new HashMap<>();
|
|
|
- body.put("code" ,code );
|
|
|
- body.put("grant_type" ,"authorization_code" );
|
|
|
- body.put("client_secret" ,"12102a6a3290fd2cf3aedf631d771d48ccc474501bea71d47627fe985c34aa8c" );
|
|
|
- body.put("client_id" ,"e7faeabf239846288ee07e6c40066cbd0dcc46cb1c1dea37c602c29a2368c6b8" );
|
|
|
- body.put("redirect_uri" ,"http://localhost/cpms/index.html#/socialLogin" );
|
|
|
+ Map<String, String> body = new HashMap<>();
|
|
|
+ body.put("code", code);
|
|
|
+ body.put("grant_type", "authorization_code");
|
|
|
+ body.put("client_secret", "12102a6a3290fd2cf3aedf631d771d48ccc474501bea71d47627fe985c34aa8c");
|
|
|
+ body.put("client_id", "e7faeabf239846288ee07e6c40066cbd0dcc46cb1c1dea37c602c29a2368c6b8");
|
|
|
+ body.put("redirect_uri", "http://localhost/cpms/index.html#/socialLogin");
|
|
|
// 2. 执行请求
|
|
|
ResponseEntity<AjaxResult> exchange = restTemplate.exchange(
|
|
|
- "https://gitee.com/oauth/token",
|
|
|
+ "https://gitee.com/oauth/token",
|
|
|
HttpMethod.POST,
|
|
|
new HttpEntity<>(body, headers),
|
|
|
- new ParameterizedTypeReference<AjaxResult>() {}); // 解决 CommonResult 的泛型丢失
|
|
|
+ new ParameterizedTypeReference<AjaxResult>() {
|
|
|
+ }); // 解决 CommonResult 的泛型丢失
|
|
|
Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
|
|
|
ajax = exchange.getBody();
|
|
|
System.out.println(ajax.toString());
|