|
@@ -1,20 +1,23 @@
|
|
|
package com.css.simulation.oauth.client.controller;
|
|
|
|
|
|
import api.common.pojo.common.ResponseBodyVO;
|
|
|
-import api.common.pojo.common.SimulationTokenVO;
|
|
|
+import api.common.pojo.param.SignUsernameParameter;
|
|
|
+import api.common.pojo.param.SignSingleParameter;
|
|
|
import api.common.pojo.po.UserPO;
|
|
|
+import api.common.pojo.vo.SimulationTokenVO;
|
|
|
import api.common.util.EncryptUtil;
|
|
|
import api.common.util.HttpUtil;
|
|
|
import api.common.util.JsonUtil;
|
|
|
import com.css.simulation.oauth.client.configuration.oauth.OauthParameter;
|
|
|
import com.css.simulation.oauth.client.mapper.UserMapper;
|
|
|
-import com.css.simulation.oauth.client.pojo.parameter.SingleParameter;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import lombok.SneakyThrows;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
-import org.springframework.boot.configurationprocessor.json.JSONObject;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
@@ -64,62 +67,49 @@ public class SignController {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 重定向到登录页,并将 token 放在 cookie 中传给前端
|
|
|
+ * 单点登录
|
|
|
*
|
|
|
* @return token 信息
|
|
|
*/
|
|
|
@RequestMapping("/single")
|
|
|
@SneakyThrows
|
|
|
@ResponseBody
|
|
|
- public ResponseBodyVO<SimulationTokenVO> home(@RequestBody SingleParameter singleParameter) {
|
|
|
- String code = singleParameter.getCode();
|
|
|
- String ticket = singleParameter.getTicket();
|
|
|
+ public ResponseBodyVO<SimulationTokenVO> single(@RequestBody SignSingleParameter signSingleParameter) {
|
|
|
+ String code = signSingleParameter.getCode();
|
|
|
+ String ticket = signSingleParameter.getTicket();
|
|
|
//1 根据统一凭条 code 获取统一平台 access_token
|
|
|
- /*
|
|
|
- 响应体
|
|
|
- {
|
|
|
- "access_token":"{ACCESS_TOKEN}",
|
|
|
- "expires_in_sec":30,
|
|
|
- "openid":"{OPENID}",
|
|
|
- "scope":"{SCOPE}",
|
|
|
- "appid":"{APPID}"
|
|
|
- }
|
|
|
- */
|
|
|
+
|
|
|
String zoogooyTokenUrl = oauthParameter.getZoogooyTokenUri() + "?appid=" + oauthParameter.getZoogooyAppid() +
|
|
|
"&secret=" + oauthParameter.getZoogooyAppSecret() +
|
|
|
"&code=" + code +
|
|
|
"&grant_type=authorization_code";
|
|
|
- JSONObject zoogooyToken = new JSONObject(HttpUtil.post(closeableHttpClient, requestConfig, zoogooyTokenUrl, null, null));
|
|
|
- String accessToken = zoogooyToken.optString("access_token");
|
|
|
- String openid = zoogooyToken.optString("openid");
|
|
|
- log.info("------- 统一平台令牌信息为:" + zoogooyToken);
|
|
|
+
|
|
|
+
|
|
|
+ String zoogooyTokenJson = HttpUtil.post(closeableHttpClient, requestConfig, zoogooyTokenUrl, null, null);
|
|
|
+ ObjectMapper objectMapper1 = new ObjectMapper();
|
|
|
+ JsonNode tokenRoot = objectMapper1.readTree(zoogooyTokenJson);
|
|
|
+ String accessToken = tokenRoot.path("access_token").asText();
|
|
|
+ String openid = tokenRoot.path("openid").asText();
|
|
|
+ log.info("------- 统一平台令牌信息为:" + accessToken);
|
|
|
|
|
|
//2 根据统一平台 access_token、openid、ticket 获取统一平台用户信息
|
|
|
- /*
|
|
|
- {
|
|
|
- "openid":"{OPENID}",
|
|
|
- "nickname": "{NICKNAME}",
|
|
|
- "photoId":"2016091919310100025281cb87dcbdc74a09be41",
|
|
|
- "role":["ROLE1" "ROLE2"],
|
|
|
- "unionid": "{UNIONID}"
|
|
|
- }
|
|
|
- */
|
|
|
+
|
|
|
String zoogooyUserUrl = oauthParameter.getZoogooyUserUri() +
|
|
|
"?access_token=" + accessToken +
|
|
|
"&openid=" + openid +
|
|
|
"&ticket=" + ticket;
|
|
|
- JSONObject userInfo = new JSONObject(HttpUtil.post(closeableHttpClient, requestConfig, zoogooyUserUrl, null, null));
|
|
|
+ String zoogooyUserJson = HttpUtil.post(closeableHttpClient, requestConfig, zoogooyUserUrl, null, null);
|
|
|
+ ObjectMapper objectMapper2 = new ObjectMapper();
|
|
|
+ JsonNode userRoot = objectMapper2.readTree(zoogooyUserJson);
|
|
|
|
|
|
-
|
|
|
- String unionid = userInfo.optString("unionid");
|
|
|
- String nickname = userInfo.optString("nickname");
|
|
|
- log.info("------- 统一平台用户信息为:" + userInfo);
|
|
|
+ String unionid = userRoot.path("unionid").asText();
|
|
|
+ String nickname = userRoot.path("nickname").asText();
|
|
|
+ log.info("------- 统一平台用户信息为:" + unionid);
|
|
|
|
|
|
//3 使用 union_id 查询数据库,是否已在仿真平台存在该用户
|
|
|
String username;
|
|
|
String password;
|
|
|
UserPO oldUser = userMapper.selectByIdIgnoreDelete(unionid);
|
|
|
- System.out.println(oldUser);
|
|
|
if (oldUser == null) { //3-1 仿真平台不存在用户,直接创建新的
|
|
|
password = EncryptUtil.getLowerMD5(oauthParameter.getSimulationDefaultPassword());
|
|
|
UserPO newUser = new UserPO();
|
|
@@ -138,7 +128,6 @@ public class SignController {
|
|
|
password = oldUser.getPassword();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//4 根据仿真平台用户名密码颁发仿真平台 token,返回给前端
|
|
|
String simulationTokenUrl = oauthParameter.getSimulationTokenUri() +
|
|
|
"?grant_type=password" +
|
|
@@ -152,4 +141,41 @@ public class SignController {
|
|
|
return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, simulationTokenVO);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户名密码登录
|
|
|
+ *
|
|
|
+ * @return token 信息
|
|
|
+ */
|
|
|
+ @RequestMapping("/username")
|
|
|
+ @SneakyThrows
|
|
|
+ @ResponseBody
|
|
|
+ public ResponseBodyVO<SimulationTokenVO> username(@RequestBody @Validated SignUsernameParameter signSingleParameter) {
|
|
|
+
|
|
|
+ //1 使用 username 查询数据库,是否已在仿真平台存在该用户
|
|
|
+ String username = signSingleParameter.getUsername();
|
|
|
+ String password = signSingleParameter.getPassword();
|
|
|
+ log.info("登录参数为:"+signSingleParameter);
|
|
|
+ UserPO oldUser = userMapper.selectByUsername(username);
|
|
|
+ if (oldUser == null) { //2-1 仿真平台不存在用户,直接创建新的
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "不存在该用户!");
|
|
|
+ } else { //2-2 仿真平台存在用户判断密码
|
|
|
+ if (!oldUser.getPassword().equals(password)) {
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "密码错误!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //3 根据仿真平台用户名密码颁发仿真平台 token,返回给前端
|
|
|
+ String simulationTokenUrl = oauthParameter.getSimulationTokenUri() +
|
|
|
+ "?grant_type=password" +
|
|
|
+ "&client_id=" + oauthParameter.getSimulationClientId() +
|
|
|
+ "&client_secret=" + oauthParameter.getSimulationClientSecret() +
|
|
|
+ "&username=" + username +
|
|
|
+ "&password=" + password;
|
|
|
+ String simulationToken = HttpUtil.post(closeableHttpClient, requestConfig, simulationTokenUrl, null, null);
|
|
|
+ System.out.println("------- 仿真平台令牌信息为:" + simulationToken);
|
|
|
+ SimulationTokenVO simulationTokenVO = JsonUtil.jsonToBean(simulationToken, SimulationTokenVO.class);
|
|
|
+ return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, simulationTokenVO);
|
|
|
+ }
|
|
|
+
|
|
|
}
|