| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- 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.HnCasService;
- 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.Base64;
- import java.util.HashMap;
- import java.util.Map;
- @Api(value = "海南单点登录", tags = "海南单点登录")
- @RestController
- @RequestMapping("/cas/hn")
- public class HnCasController extends BaseController {
- private Logger logger = LoggerFactory.getLogger(getClass());
- @Value("${hn.auth.baseurl}")
- private String hnAuthBaseUrl;
- @Value("${hn.auth.client_id}")
- private String hnAuthClientId;
- @Value("${hn.auth.client_screct}")
- private String hnAuthClientScrect;
- @Value("${hn.auth.redirect}")
- private String redirect;
- @Autowired
- HnCasService hnCasService;
- @RequestMapping(value = "/", method = RequestMethod.GET)
- public BaseResponse<Object> code(@RequestParam(value = "ticket", required = false) String ticket, HttpServletRequest request) throws Exception {
- //根据code 获取对应的 token
- Map<String, String> params = new HashMap<>();
- params.put("grant_type", "authorization_code");
- params.put("code", ticket);
- params.put("redirect_uri", redirect);
- logger.info("---------------send fj sso ---------------");
- Map<String, String> headerMap = new HashMap<>();
- String userName = "Username:";
- headerMap.put("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString((hnAuthClientId + ":" + hnAuthClientScrect).getBytes()));
- String ret = HttpClientUtils.doHttpsPost(hnAuthBaseUrl + "/auth/oauth/token", params, headerMap);
- //根据token获取用户信息
- logger.info(ret);
- //字符串转json 获取token
- String accessToken = JSONObject.fromObject(ret).get("access_token").toString();
- String refreshToken = JSONObject.fromObject(ret).get("refresh_token").toString();
- //根据token获取用户信息
- Map<String, String> tokenParam = new HashMap<>();
- tokenParam.put("access_token", accessToken);
- headerMap.clear();
- headerMap.put("Authorization", "Bearer " + accessToken);
- String user = HttpClientUtils.simpleGetInvoke(hnAuthBaseUrl + "/auth/v1/user/me", tokenParam, headerMap, "UTF-8");
- logger.info(user);
- String mobile = JSONObject.fromObject(user).get("userId").toString();
- //手机号默认登录,如果没有则增加用户设置督查权限。参数,有用户名,密码,手机号,默认角色
- Map<String, Object> ssoUserMap = jsonToMap(JSONObject.fromObject(user).toString());
- logger.info("---------------sso login-------------------------------");
- if (ssoUserMap != null) {
- logger.info("---------------sso user not null -------------------------------");
- BisInspAllRlationPers bisInspAllRlationPers = hnCasService.validateUser(ssoUserMap);
- if (bisInspAllRlationPers == null) {
- return buildFailResponse(1001, "登陆名或密码错误", "", "");
- } else {
- logger.info("---------------sso user null -------------------------------");
- String uuid = UuidUtil.uuid();
- bisInspAllRlationPers = hnCasService.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;
- }
- /**
- * 根据token获取 用户信息
- * @param token
- * @param request
- * @return
- * @throws Exception
- */
- @RequestMapping(value = "/loginByToken", method = RequestMethod.GET)
- public BaseResponse<Object> loginByToken(@RequestParam(value = "token", required = false) String token, HttpServletRequest request) throws Exception {
- Map<String, String> headerMap = new HashMap<>();
- Map<String, String> tokenParam = new HashMap<>();
- tokenParam.put("access_token", token);
- headerMap.put("Authorization", "Bearer " + token);
- String user = HttpClientUtils.simpleGetInvoke(hnAuthBaseUrl + "/auth/v1/user/me", tokenParam, headerMap, "UTF-8");
- logger.info(user);
- String mobile = JSONObject.fromObject(user).get("userId").toString();
- //手机号默认登录,如果没有则增加用户设置督查权限。参数,有用户名,密码,手机号,默认角色
- Map<String, Object> ssoUserMap = jsonToMap(JSONObject.fromObject(user).toString());
- logger.info("---------------sso login-------------------------------");
- if (ssoUserMap != null) {
- logger.info("---------------sso user not null -------------------------------");
- BisInspAllRlationPers bisInspAllRlationPers = hnCasService.validateUser(ssoUserMap);
- if (bisInspAllRlationPers == null) {
- return buildFailResponse(1001, "登陆名或密码错误", "", "");
- } else {
- logger.info("---------------sso user null -------------------------------");
- String uuid = UuidUtil.uuid();
- bisInspAllRlationPers = hnCasService.validateTicket(bisInspAllRlationPers, uuid, request);
- logger.info("---------------sso add -------------------------------");
- return buildSuccessResponse(bisInspAllRlationPers, uuid);
- }
- } else {
- logger.info("---------------sso no user -------------------------------");
- return buildFailResponse("无法获取用户信息");
- }
- }
- }
|