| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- 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.FjCasService;
- import cn.com.goldenwater.dcproj.utils.HttpClientUtils;
- import cn.com.goldenwater.dcproj.utils.StringUtils;
- import cn.com.goldenwater.dcproj.utils.UserMsgSectrityUtils;
- 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/fj")
- public class FjCasController extends BaseController {
- private Logger logger = LoggerFactory.getLogger(getClass());
- @Value("${fj.auth.baseurl}")
- private String fjAuthBaseUrl;
- @Value("${fj.auth.client_id}")
- private String fjAuthClientId;
- @Value("${fj.auth.client_screct}")
- private String fjAuthClientScrect;
- @Autowired
- FjCasService fjCasService;
- @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> headerParams = new HashMap<>();
- headerParams.put("", "");
- Map<String, String> params = new HashMap<>();
- params.put("grant_type", "authorization_code");
- params.put("client_id", fjAuthClientId);
- params.put("client_secret", fjAuthClientScrect);
- params.put("code", ticket);
- params.put("redirect_uri", "https://27.156.118.74:19901/");
- logger.info("---------------send fj sso ---------------");
- logger.info("-----------------------------get fj dz " + fjAuthBaseUrl + "/oauth2.0/token-----------------------------" + params.toString());
- String ret = HttpClientUtils.simpleGetInvoke(fjAuthBaseUrl + "/oauth2.0/token", params, headerParams,CONTENT_CHARSET);
- //根据token获取用户信息
- logger.info("-----------------------------get fj token " + ret + "-----------------------------");
- //字符串转json 获取token
- String accessToken = JSONObject.fromObject(ret).get("access_token").toString();
- String refreshToken = JSONObject.fromObject(ret).get("refresh_token").toString();
- logger.info("-----------------------------get fj accessToken " + ret + "-----------------------------");
- //根据token获取用户信息
- Map<String, String> tokenParam = new HashMap<>();
- tokenParam.put("access_token", accessToken);
- String user = HttpClientUtils.simpleGetInvoke(fjAuthBaseUrl + "/api-gateway/upms-center/userinfo", tokenParam);
- logger.info("-----------------------------get fj user " + user + "-----------------------------");
- String mobile = JSONObject.fromObject(JSONObject.fromObject(user).get("user").toString()).get("mobile").toString();
- logger.info("-----------------------------get fj mobile " + mobile + "-----------------------------");
- //手机号默认登录,如果没有则增加用户设置督查权限。参数,有用户名,密码,手机号,默认角色
- Map<String, Object> ssoUserMap = jsonToMap(JSONObject.fromObject(user).get("user").toString());
- logger.info("---------------sso login-------------------------------");
- if (ssoUserMap != null) {
- logger.info("---------------sso user not null -------------------------------");
- BisInspAllRlationPers bisInspAllRlationPers = fjCasService.validateUser(ssoUserMap);
- if (bisInspAllRlationPers == null) {
- return buildFailResponse(1001, "登陆名或密码错误", "", "");
- } else {
- logger.info("---------------sso user null -------------------------------");
- String uuid = UuidUtil.uuid();
- bisInspAllRlationPers = fjCasService.validateTicket(bisInspAllRlationPers, uuid, request);
- logger.info("---------------sso add -------------------------------");
- return buildSuccessResponse(bisInspAllRlationPers, uuid);
- }
- } else {
- logger.info("---------------sso no user -------------------------------");
- return buildFailResponse("无法获取用户信息");
- }
- }
- @RequestMapping(value = "/loginByToken", method = RequestMethod.GET)
- public BaseResponse<Object> loginByToken(@RequestParam(value = "accessToken", required = false) String accessToken, HttpServletRequest request) throws Exception {
- Map<String, String> tokenParam = new HashMap<>();
- tokenParam.put("access_token", accessToken);
- logger.info("----------" + fjAuthBaseUrl + "/api-gateway/upms-center/userinfo:" + tokenParam.get("access_token").toString() + "----------");
- String user = HttpClientUtils.simpleGetInvoke(fjAuthBaseUrl + "/api-gateway/upms-center/userinfo", tokenParam);
- String mobile = JSONObject.fromObject(JSONObject.fromObject(user).get("user").toString()).get("mobile").toString();
- logger.info("-----------------------" + mobile + "----------------------------");
- if (StringUtils.isEmpty(mobile)) {
- return buildFailResponse(5001, "账号未绑定!", "", "");
- }
- if (!UserMsgSectrityUtils.checkPhone(mobile)) {
- return buildFailResponse(5001, "账号未绑定!", "", "");
- }
- //手机号默认登录,如果没有则增加用户设置督查权限。参数,有用户名,密码,手机号,默认角色
- Map<String, Object> ssoUserMap = jsonToMap(JSONObject.fromObject(user).get("user").toString());
- logger.info("---------------sso login-------------------------------");
- if (ssoUserMap != null) {
- logger.info("---------------sso user not null -------------------------------");
- BisInspAllRlationPers bisInspAllRlationPers = fjCasService.validateUser(ssoUserMap);
- if (bisInspAllRlationPers == null) {
- return buildFailResponse(1001, "登陆名或密码错误", "", "");
- } else {
- logger.info("---------------sso user null -------------------------------");
- String uuid = UuidUtil.uuid();
- bisInspAllRlationPers = fjCasService.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;
- }
- }
|