|
@@ -1,8 +1,13 @@
|
|
|
package com.ruoyi.project.system.controller;
|
|
|
|
|
|
+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.*;
|
|
|
+import org.springframework.util.Assert;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
@@ -18,6 +23,7 @@ import com.ruoyi.framework.web.domain.AjaxResult;
|
|
|
import com.ruoyi.project.system.domain.SysMenu;
|
|
|
import com.ruoyi.project.system.domain.SysUser;
|
|
|
import com.ruoyi.project.system.service.ISysMenuService;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
/**
|
|
|
* 登录验证
|
|
@@ -38,7 +44,8 @@ public class SysLoginController
|
|
|
|
|
|
@Autowired
|
|
|
private TokenService tokenService;
|
|
|
-
|
|
|
+ // @Resource // 可优化,注册一个 RestTemplate Bean,然后注入
|
|
|
+ private final RestTemplate restTemplate = new RestTemplate();
|
|
|
/**
|
|
|
* 登录方法
|
|
|
*
|
|
@@ -91,4 +98,46 @@ public class SysLoginController
|
|
|
List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
|
|
|
return AjaxResult.success(menuService.buildMenus(menus));
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 登录方法
|
|
|
+ *
|
|
|
+ * @param loginBody 登录信息
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @PostMapping("/getAccessToken")
|
|
|
+ public AjaxResult getAccessToken(@RequestBody LoginBody loginBody)
|
|
|
+ {
|
|
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
+ // 生成令牌
|
|
|
+ String code = loginBody.getCode();
|
|
|
+
|
|
|
+// 1.1 构建请求头
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+ 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" );
|
|
|
+ // 2. 执行请求
|
|
|
+ ResponseEntity<AjaxResult> exchange = restTemplate.exchange(
|
|
|
+ "https://gitee.com/oauth/token",
|
|
|
+ HttpMethod.POST,
|
|
|
+ new HttpEntity<>(body, headers),
|
|
|
+ new ParameterizedTypeReference<AjaxResult>() {}); // 解决 CommonResult 的泛型丢失
|
|
|
+ Assert.isTrue(exchange.getStatusCode().is2xxSuccessful(), "响应必须是 200 成功");
|
|
|
+ ajax = exchange.getBody();
|
|
|
+ System.out.println(ajax.toString());
|
|
|
+ ajax.get("access_token");
|
|
|
+ //进行jwt解析
|
|
|
+
|
|
|
+ //系统登录 获取系统token
|
|
|
+
|
|
|
+ return ajax;
|
|
|
+ }
|
|
|
+
|
|
|
}
|