|
@@ -23,8 +23,12 @@ import org.apache.http.client.config.RequestConfig;
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@@ -32,7 +36,8 @@ import java.time.Duration;
|
|
|
import java.util.List;
|
|
|
import java.util.Set;
|
|
|
|
|
|
-@RestController
|
|
|
+//@RestController 不能用 RestController,重定向接口 /entry 不能
|
|
|
+@Controller
|
|
|
@RequestMapping("/sign")
|
|
|
@Slf4j
|
|
|
public class SignController {
|
|
@@ -57,12 +62,12 @@ public class SignController {
|
|
|
@RequestMapping("/entry")
|
|
|
@SneakyThrows
|
|
|
public String entry(@RequestParam("ticket") String ticket) {
|
|
|
- log.info("------- /entry 接收到的 ticket 为:" + ticket);
|
|
|
+ log.info("接收到的 ticket 为:" + ticket);
|
|
|
String encodeTicket = EncodeUtil.utf8(ticket);
|
|
|
- log.info("------- /entry ticket 编码之后为:" + encodeTicket);
|
|
|
- log.info("------- /entry 重定向地址为:" + oauthParameter.getZoogooyRedirectUri());
|
|
|
+ log.info("ticket 编码之后为:" + encodeTicket);
|
|
|
+ log.info("重定向地址为:" + oauthParameter.getZoogooyRedirectUri());
|
|
|
String encodeRedirect = EncodeUtil.utf8(oauthParameter.getZoogooyRedirectUri());
|
|
|
- log.info("------- /entry 重定向地址编码之后为:" + encodeRedirect);
|
|
|
+ log.info("重定向地址编码之后为:" + encodeRedirect);
|
|
|
|
|
|
String zoogooyAuthorizeUrl = oauthParameter.getZoogooyAuthorizeUri() +
|
|
|
"?appid=" + oauthParameter.getZoogooyAppid() +
|
|
@@ -70,7 +75,7 @@ public class SignController {
|
|
|
"&ticket=" + encodeTicket +
|
|
|
"&response_type=code" +
|
|
|
"&scope=snsapi_userinfo";
|
|
|
- log.info("------- /entry 需要访问的授权地址为:" + zoogooyAuthorizeUrl);
|
|
|
+ log.info("需要访问的授权地址为:" + zoogooyAuthorizeUrl);
|
|
|
return "redirect:" + zoogooyAuthorizeUrl;
|
|
|
}
|
|
|
|
|
@@ -82,6 +87,7 @@ public class SignController {
|
|
|
*/
|
|
|
@RequestMapping("/single")
|
|
|
@SneakyThrows
|
|
|
+ @ResponseBody
|
|
|
public ResponseBodyVO<SimulationTokenVO> single(@RequestBody SignSingleParameter signSingleParameter, HttpServletRequest request) {
|
|
|
|
|
|
String code = signSingleParameter.getCode();
|
|
@@ -97,24 +103,24 @@ public class SignController {
|
|
|
|
|
|
String encodeCode = EncodeUtil.utf8(code);
|
|
|
String encodeTicket = EncodeUtil.utf8(ticket);
|
|
|
- log.info("------- /single 接收到的 code 为:" + code);
|
|
|
- log.info("------- /single code 编码之后为:" + encodeCode);
|
|
|
- log.info("------- /single 接收到的 ticket 为:" + ticket);
|
|
|
- log.info("------- /single ticket 编码之后为:" + encodeTicket);
|
|
|
+ log.info("接收到的 code 为:" + code);
|
|
|
+ log.info("code 编码之后为:" + encodeCode);
|
|
|
+ log.info("接收到的 ticket 为:" + ticket);
|
|
|
+ log.info("ticket 编码之后为:" + encodeTicket);
|
|
|
String zoogooyTokenUrl = oauthParameter.getZoogooyTokenUri() +
|
|
|
"?appid=" + oauthParameter.getZoogooyAppid() +
|
|
|
"&secret=" + oauthParameter.getZoogooyAppSecret() +
|
|
|
"&code=" + encodeCode +
|
|
|
"&grant_type=authorization_code";
|
|
|
- log.info("------- /single 根据编码之后的 code 获取众工业平台 access_token 的地址为:" + zoogooyTokenUrl);
|
|
|
+ log.info("根据编码之后的 code 获取众工业平台 access_token 的地址为:" + zoogooyTokenUrl);
|
|
|
String zoogooyTokenJson = HttpUtil.get(closeableHttpClient, requestConfig, zoogooyTokenUrl);
|
|
|
- log.info("------- /single 根据编码之后的 code 获取众工业平台 access_token 的结果为:" + zoogooyTokenJson);
|
|
|
+ log.info("根据编码之后的 code 获取众工业平台 access_token 的结果为:" + zoogooyTokenJson);
|
|
|
ObjectMapper objectMapper1 = new ObjectMapper();
|
|
|
JsonNode tokenRoot = objectMapper1.readTree(zoogooyTokenJson);
|
|
|
if (!tokenRoot.path("success").asBoolean()) {
|
|
|
throw new RuntimeException(tokenRoot.path("message").asText());
|
|
|
}
|
|
|
- log.info("------- /single 众工业平台 token 信息为:" + zoogooyTokenJson);
|
|
|
+ log.info("众工业平台 token 信息为:" + zoogooyTokenJson);
|
|
|
/*
|
|
|
{
|
|
|
"data" : {
|
|
@@ -138,15 +144,15 @@ public class SignController {
|
|
|
String openid = tokenRoot.path("data").path("openid").asText();
|
|
|
String encodeOpenid = EncodeUtil.utf8(openid);
|
|
|
|
|
|
- log.info("------- /single 众工业平台 access_token 为:" + accessToken);
|
|
|
- log.info("------- /single 众工业平台 access_token 编码之后为:" + encodeAccessToken);
|
|
|
- log.info("------- /single 众工业平台 openid 为:" + openid);
|
|
|
- log.info("------- /single 众工业平台 openid 编码之后为:" + encodeOpenid);
|
|
|
+ log.info("众工业平台 access_token 为:" + accessToken);
|
|
|
+ log.info("众工业平台 access_token 编码之后为:" + encodeAccessToken);
|
|
|
+ log.info("众工业平台 openid 为:" + openid);
|
|
|
+ log.info("众工业平台 openid 编码之后为:" + encodeOpenid);
|
|
|
String zoogooyUserUrl = oauthParameter.getZoogooyUserUri() +
|
|
|
"?access_token=" + encodeAccessToken +
|
|
|
"&openid=" + encodeOpenid +
|
|
|
"&ticket=" + encodeTicket;
|
|
|
- log.info("------- /single 根据编码之后的 access_token、openid、ticket 获取众工业平台用户信息:" + zoogooyUserUrl);
|
|
|
+ log.info("根据编码之后的 access_token、openid、ticket 获取众工业平台用户信息:" + zoogooyUserUrl);
|
|
|
/*
|
|
|
{
|
|
|
"data" : {
|
|
@@ -166,9 +172,9 @@ public class SignController {
|
|
|
ObjectMapper objectMapper2 = new ObjectMapper();
|
|
|
JsonNode userRoot = objectMapper2.readTree(zoogooyUserJson);
|
|
|
if (!userRoot.path("success").asBoolean()) {
|
|
|
- throw new RuntimeException("------- /single 获取众工业平台用户信息出错:" + zoogooyUserJson);
|
|
|
+ throw new RuntimeException("获取众工业平台用户信息出错:" + zoogooyUserJson);
|
|
|
}
|
|
|
- log.info("------- /single 众工业平台用户信息为:" + zoogooyUserJson);
|
|
|
+ log.info("众工业平台用户信息为:" + zoogooyUserJson);
|
|
|
|
|
|
username = "zoogooy-" + userRoot.path("data").path("loginName").asText();
|
|
|
String nickname = userRoot.path("data").path("nickname").asText();
|
|
@@ -195,7 +201,7 @@ public class SignController {
|
|
|
newUser.setIsDeleted("0");
|
|
|
userMapper.insert(newUser);
|
|
|
userId = newUser.getId();
|
|
|
- log.info("------- /single 成功插入新用户信息:" + newUser);
|
|
|
+ log.info("成功插入新用户信息:" + newUser);
|
|
|
} else {
|
|
|
if ("1".equals(oldUser.getIsDeleted())) { //3-2 仿真平台存在删除状态用户,改为未删除
|
|
|
userMapper.updateIsDeleted(openid, "0");
|
|
@@ -214,9 +220,9 @@ public class SignController {
|
|
|
"&client_secret=" + oauthParameter.getSimulationClientSecret() +
|
|
|
"&username=" + username +
|
|
|
"&password=" + password;
|
|
|
- log.info("------- /single 获取仿真云平台 token 信息:" + simulationTokenUrl);
|
|
|
+ log.info("获取仿真云平台 token 信息:" + simulationTokenUrl);
|
|
|
simulationToken = HttpUtil.get(closeableHttpClient, requestConfig, simulationTokenUrl);
|
|
|
- log.info("------- /single 仿真云平台 token 信息为:" + simulationToken);
|
|
|
+ log.info("仿真云平台 token 信息为:" + simulationToken);
|
|
|
redisTemplate.opsForValue().set("code:" + code + ":ticket:" + ticket, simulationToken, Duration.ofMinutes(30L));
|
|
|
}
|
|
|
|
|
@@ -243,6 +249,7 @@ public class SignController {
|
|
|
*/
|
|
|
@RequestMapping("/username")
|
|
|
@SneakyThrows
|
|
|
+ @ResponseBody
|
|
|
public ResponseBodyVO<SimulationTokenVO> username(@RequestBody @Validated SignUsernameParameter signSingleParameter, HttpServletRequest request) {
|
|
|
log.info("登录参数为:" + signSingleParameter);
|
|
|
String username = signSingleParameter.getUsername();
|
|
@@ -317,6 +324,7 @@ public class SignController {
|
|
|
* @return token 信息
|
|
|
*/
|
|
|
@RequestMapping("/refreshToken")
|
|
|
+ @ResponseBody
|
|
|
public ResponseBodyVO<SimulationTokenVO> refreshToken(@RequestBody @Validated SignRefreshParameter signRefreshParameter) {
|
|
|
String refreshToken = signRefreshParameter.getRefreshToken();
|
|
|
//刷新令牌
|
|
@@ -340,6 +348,7 @@ public class SignController {
|
|
|
* 服务监控
|
|
|
*/
|
|
|
@RequestMapping("online")
|
|
|
+ @ResponseBody
|
|
|
public ResponseBodyVO<Integer> online() {
|
|
|
int online;
|
|
|
|