| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package cn.com.goldenwater.dcproj.controller.sso;
- import cn.com.goldenwater.core.web.BaseController;
- import cn.com.goldenwater.core.web.BaseResponse;
- import cn.com.goldenwater.dcproj.model.BisInspAllRlationPers;
- import cn.com.goldenwater.dcproj.service.SdCasService;
- import cn.com.goldenwater.dcproj.service.SdTCasService;
- import cn.com.goldenwater.dcproj.utils.HttpClientUtils;
- import cn.com.goldenwater.id.util.UuidUtil;
- import com.alibaba.fastjson.JSON;
- import io.swagger.annotations.Api;
- import net.sf.json.JSONObject;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
- import javax.servlet.http.HttpServletRequest;
- import java.util.HashMap;
- import java.util.Map;
- import static cn.com.goldenwater.dcproj.utils.HttpClientUtils.CONTENT_CHARSET;
- @Api(value = "山东单点登录", tags = "山东单点登录")
- @RestController
- @RequestMapping("/cas/sdt")
- public class SdTCasController extends BaseController {
- private Logger logger = LoggerFactory.getLogger(getClass());
- @Value("${sdt.auth.baseurl}")
- private String sdtAuthBaseUrl;
- @Value("${sdt.auth.client_agentid}")
- private String sdtAuthClientAgentId;
- @Value("${sdt.auth.client_screct}")
- private String sdtAuthClientScrect;
- @Value("${sdt.auth.client_corpid}")
- private String sdtAuthClientCorpid;
- @Autowired
- SdTCasService sdtCasService;
- @RequestMapping(value = "/validateTicketKey1", method = RequestMethod.GET)
- public BaseResponse<Object> code(@RequestParam(value = "code", required = false) String code,HttpServletRequest request) throws Exception {
- //根据corpid获取token
- Map<String, String> headerParams = new HashMap<>();
- headerParams.put("", "");
- Map<String, String> params = new HashMap<>();
- params.put("corpid", sdtAuthClientCorpid);
- params.put("corpsecret", sdtAuthClientScrect);
- String ret = HttpClientUtils.simpleGetInvoke(sdtAuthBaseUrl + "/cgi-bin/gettoken", params);
- // String ret = "{\"errcode\":0, \"errmsg\":\"ok\", \"access_token\":\"accesstoken000001\", \"expires_in\":72006}";
- logger.info("sessionId ret-------------------------" + ret + "-------------------------");
- //字符串转json 获取token
- String accessToken = JSONObject.fromObject(ret).get("access_token").toString();
- // 根据accessToken、code调用 调用6.4 获取 用户编码
- //根据token获取用户信息
- Map<String, String> tokenParam = new HashMap<>();
- tokenParam.put("access_token", accessToken);
- tokenParam.put("code", code);
- String user = HttpClientUtils.simpleGetInvoke(sdtAuthBaseUrl + "/cgi-bin/user/getuserinfo", tokenParam);
- // String user = "{\"errcode\":0, \"errmsg\":\"ok\", \"UserId\":\"USERID\", \"DeviceId\":\"DEVICEID\", \"user_ticket\":\"USER_TICKET\", \"expires_in\":7200, \"usertype\":2}";
- // user 用户编码信息 解析
- logger.info("-----------------------------get sdt user " + user + "-----------------------------");
- String useriId = JSONObject.fromObject(user).get("UserId").toString();
- // String mobile = JSONObject.fromObject(JSONObject.fromObject(user).get("user").toString()).get("mobile").toString();
- logger.info("-----------------------------get sdt mobile " + useriId + "-----------------------------");
- // 根据用户编码 获取用户信息
- Map<String, String> userIdParam = new HashMap<>();
- userIdParam.put("access_token",accessToken);
- userIdParam.put("userid",useriId);
- // userIdParam.put("avatar_addr",avatarAddr);
- String userInfo = HttpClientUtils.simpleGetInvoke(sdtAuthBaseUrl + "/cgi-bin/user/get", userIdParam);
- // String userInfo = "{\"errcode\":0, \"errmsg\":\"ok\", \"userid\":\"lihaichao\", \"name\":\"李海超\", \"mobile\":\"13401079738\", \"hide_mobile\":0}";
- logger.info("-----------------------------get sdt userInfo " + userInfo + "-----------------------------");
- //
- //手机号默认登录,如果没有则增加用户设置督查权限。参数,有用户名,密码,手机号,默认角色
- Map<String, Object> ssoUserMap = jsonToMap(userInfo);
- logger.info("---------------sso login-------------------------------");
- if (ssoUserMap != null) {
- logger.info("---------------sso user not null -------------------------------");
- BisInspAllRlationPers bisInspAllRlationPers = sdtCasService.validateUser(ssoUserMap);
- if (bisInspAllRlationPers == null) {
- return buildFailResponse(1001, "登陆名或密码错误", "", "");
- } else {
- logger.info("---------------sso user null -------------------------------");
- String uuid = UuidUtil.uuid();
- bisInspAllRlationPers = sdtCasService.validateTicket(bisInspAllRlationPers, uuid, request);
- logger.info("---------------sso add -------------------------------");
- return buildSuccessResponse(bisInspAllRlationPers, uuid);
- }
- } else {
- logger.info("---------------sso no user -------------------------------");
- return buildFailResponse("无法获取用户信息");
- }
- }
- public Map<String, Object> jsonToMap(String json) {
- Map<String, Object> map = new HashMap<>();
- map = JSON.parseObject(JSON.parse(json).toString(), HashMap.class);
- return map;
- }
- }
|