|
@@ -1,138 +0,0 @@
|
|
|
-package com.css.simulation.oauth.client.controller;
|
|
|
-
|
|
|
-import api.common.pojo.common.ResponseBodyVO;
|
|
|
-import api.common.pojo.common.SimulationTokenVO;
|
|
|
-import api.common.pojo.po.UserPO;
|
|
|
-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 lombok.SneakyThrows;
|
|
|
-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.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestParam;
|
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
-
|
|
|
-import javax.annotation.Resource;
|
|
|
-
|
|
|
-@Controller
|
|
|
-@RequestMapping("/test")
|
|
|
-public class TestSingleSignOnController {
|
|
|
-
|
|
|
- @Resource
|
|
|
- private UserMapper userMapper;
|
|
|
- @Resource
|
|
|
- private CloseableHttpClient closeableHttpClient;
|
|
|
- @Resource
|
|
|
- private RequestConfig requestConfig;
|
|
|
- @Resource
|
|
|
- private OauthParameter oauthParameter;
|
|
|
-
|
|
|
- /**
|
|
|
- * http://localhost:7001/simulation/test/entry?ticket=1001
|
|
|
- */
|
|
|
- @RequestMapping("/entry")
|
|
|
- public String entry(@RequestParam("ticket") String ticket) {
|
|
|
- System.out.println(oauthParameter);
|
|
|
- String zoogooyAuthorizeUrl = oauthParameter.getZoogooyAuthorizeUri() +
|
|
|
- "?appid=" + oauthParameter.getZoogooyAppid() +
|
|
|
- "&redirect_uri=" + oauthParameter.getZoogooyRedirectUri() +
|
|
|
- "&ticket=" + ticket +
|
|
|
- "&response_type=code" +
|
|
|
- "&scope=snsapi_userinfo";
|
|
|
- return "redirect:" +zoogooyAuthorizeUrl;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 登录接口首页,将 code 返回给前端
|
|
|
- *
|
|
|
- * @return token 信息
|
|
|
- */
|
|
|
- @RequestMapping("/home")
|
|
|
- @SneakyThrows
|
|
|
- @ResponseBody
|
|
|
- public ResponseBodyVO<SimulationTokenVO> home(@RequestParam("code") String code, @RequestParam("ticket") String ticket) {
|
|
|
- //1 根据统一凭条 code 获取统一平台 access_token
|
|
|
- /*
|
|
|
- 响应体
|
|
|
- {
|
|
|
- "access_token":"{ACCESS_TOKEN}",
|
|
|
- "expires_in_sec":30,
|
|
|
- "openid":"{OPENID}",
|
|
|
- "scope":"{SCOPE}",
|
|
|
- "appid":"{APPID}"
|
|
|
- }
|
|
|
- */
|
|
|
- JSONObject zoogooyToken = HttpUtil.post(closeableHttpClient, requestConfig,
|
|
|
- oauthParameter.getZoogooyTokenUri() + "?appid=" + oauthParameter.getZoogooyAppid() +
|
|
|
- "&secret=" + oauthParameter.getZoogooyAppSecret() +
|
|
|
- "&code=" + code +
|
|
|
- "&grant_type=authorization_code",
|
|
|
- null, null);
|
|
|
- String accessToken = zoogooyToken.optString("access_token");
|
|
|
- String openid = zoogooyToken.optString("openid");
|
|
|
- System.out.println("------- 统一平台令牌信息为:" + zoogooyToken);
|
|
|
-
|
|
|
- //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 = HttpUtil.post(closeableHttpClient, requestConfig, zoogooyUserUrl, null, null);
|
|
|
-
|
|
|
- String unionid = userInfo.optString("unionid");
|
|
|
- String nickname = userInfo.optString("nickname");
|
|
|
- System.out.println("------- 统一平台用户信息为:" + userInfo);
|
|
|
-
|
|
|
- //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();
|
|
|
- newUser.setId(unionid);
|
|
|
- username = openid; // 将 openid 作为登录名
|
|
|
- newUser.setUsername(username);
|
|
|
- newUser.setNickname(nickname);
|
|
|
- newUser.setPassword(password);
|
|
|
- userMapper.insert(newUser);
|
|
|
- } else {
|
|
|
- if ("1".equals(oldUser.getIsDeleted())) { //3-2 仿真平台存在删除状态用户,改为未删除
|
|
|
- userMapper.updateIsDeleted(unionid, "0");
|
|
|
- }
|
|
|
- //3-3 仿真平台用户存在未删除用户,放行
|
|
|
- username = oldUser.getUsername();
|
|
|
- password = oldUser.getPassword();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- //4 根据仿真平台用户名密码颁发仿真平台 token,返回给前端
|
|
|
- String simulationTokenUrl = oauthParameter.getSimulationTokenUri() +
|
|
|
- "?grant_type=password" +
|
|
|
- "&client_id=" + oauthParameter.getSimulationClientId() +
|
|
|
- "&client_secret=" + oauthParameter.getSimulationClientSecret() +
|
|
|
- "&username=" + username +
|
|
|
- "&password=" + password;
|
|
|
- SimulationTokenVO simulationToken = JsonUtil.jsonObjectToBean(HttpUtil.post(closeableHttpClient, requestConfig, simulationTokenUrl, null, null), SimulationTokenVO.class);
|
|
|
- System.out.println("------- 仿真平台令牌信息为:"+simulationToken);
|
|
|
-
|
|
|
-
|
|
|
- return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "请求成功!", simulationToken);
|
|
|
- }
|
|
|
-
|
|
|
-}
|