package com.doumee.api.business;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.doumee.api.BaseController;
|
import com.doumee.biz.system.SystemDictDataBiz;
|
import com.doumee.core.constants.ResponseStatus;
|
import com.doumee.core.exception.BusinessException;
|
import com.doumee.core.model.LoginUserInfo;
|
import com.doumee.core.utils.Constants;
|
import com.doumee.core.utils.HtmlUtil;
|
import com.doumee.core.utils.HttpUtils;
|
import com.doumee.core.utils.Utils;
|
import com.doumee.dao.system.model.SystemLoginLog;
|
import com.doumee.dao.system.model.SystemUser;
|
import com.doumee.service.system.SystemLoginLogService;
|
import com.doumee.service.system.SystemUserService;
|
import io.swagger.annotations.Api;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.apache.shiro.SecurityUtils;
|
import org.apache.shiro.authc.AuthenticationException;
|
import org.apache.shiro.authc.UsernamePasswordToken;
|
import org.apache.shiro.subject.Subject;
|
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.RestController;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.util.Date;
|
import java.util.Objects;
|
|
@Api(tags = "单点登录")
|
@RestController
|
@RequestMapping("/business/web")
|
@Slf4j
|
public class WebController extends BaseController {
|
|
@Autowired
|
private SystemDictDataBiz systemDictDataBiz;
|
|
@Autowired
|
private SystemLoginLogService systemLoginLogService;
|
@Autowired
|
private SystemUserService systemUserService;
|
@Value("${project.version}")
|
private String systemVersion;
|
|
/**
|
* location.assign('https://sso.gongfuhf.cn/iamsso/oauth2/authorize?response_type=code&client_id=CmUngBQPfmzRNuUGLmqqQo&redirect_uri=http://ggfw.gongfuhf.cn:80/web/loginAuth.shtml');
|
* 放在单点登录检测页
|
*
|
* @param testId
|
* @param code
|
* @param request
|
* @param response
|
* @throws Exception
|
*/
|
@RequestMapping("/loginAuth")
|
public void loginAuth(String testId, String code, HttpServletRequest request,
|
HttpServletResponse response) throws Exception {
|
|
SystemLoginLog loginLog = new SystemLoginLog();
|
loginLog.setSystemVersion(systemVersion);
|
loginLog.setLoginTime(new Date());
|
loginLog.setIp(Utils.User_Client.getIP(request));
|
loginLog.setLocation(Utils.Location.getLocationString(loginLog.getIp()));
|
loginLog.setPlatform(Utils.User_Client.getPlatform(request));
|
loginLog.setClientInfo(Utils.User_Client.getBrowser(request));
|
loginLog.setOsInfo(Utils.User_Client.getOS(request));
|
loginLog.setServerIp(Utils.Server.getIP());
|
|
SystemUser systemUser = new SystemUser();
|
|
if (Constants.IS_DEBUG) {
|
//测试模式查询用户信息
|
SystemUser queryDto = new SystemUser();
|
// queryDto.setUsername(username);
|
queryDto.setDeleted(Boolean.FALSE);
|
SystemUser sysresult = systemUserService.findOne(queryDto);
|
systemUser = sysresult;
|
} else {
|
String params = "grant_type=authorization_code&code=" + code;
|
// 换成你的客户端 id 和密钥
|
params += String.format("&client_id=%s&client_secret=%s",
|
systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.GONGFU_CAS_CLIENT_ID).getCode(),
|
systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.GONGFU_CAS_CLIENT_KEY).getCode());
|
String result = HttpUtils.doHttpPost(
|
systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.GONGFU_CAS_DOMAIN).getCode(), params);
|
if (StringUtils.isNotBlank(result)) {
|
|
JSONObject resultDetails = JSONObject.parseObject(result);
|
String userInfoJson = "";
|
String accessToken = resultDetails.getString("access_token");
|
String uid = resultDetails.getString("uid");
|
System.out.println("accessToken: " + accessToken);
|
System.out.println("id: " + uid);
|
userInfoJson = HttpUtils
|
.doHttpGet(systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.GONGFU_CAS_INTFACE_URL).getCode()
|
+ uid + "?access_token=" + accessToken);
|
if (StringUtils.isNotBlank(userInfoJson)) {
|
JSONObject user = JSONObject.parseObject(userInfoJson);
|
|
// 根据ID查询用户信息[查询sys_user ssoUserId]
|
String gfUserid = user.getString("id");
|
SystemUser queryDto = new SystemUser();
|
// queryDto.setUsername(username);
|
queryDto.setDeleted(Boolean.FALSE);
|
SystemUser sysresult = systemUserService.findOne(queryDto);
|
systemUser = sysresult;
|
} else {
|
throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "单点登录获取用户信息异常");
|
}
|
} else {
|
throw new BusinessException(ResponseStatus.DATA_ERRO.getCode(), "单点登录获取token异常");
|
}
|
}
|
|
|
if (systemUser == null) {
|
//用户不存在新增用户
|
}
|
|
loginLog.setLoginUsername(systemUser.getUsername());
|
Subject subject = SecurityUtils.getSubject();
|
UsernamePasswordToken token = new UsernamePasswordToken(systemUser.getUsername(), systemUser.getPassword());
|
subject.login(token);
|
String url = "https://hefei.dtkey.cn/hfznzz_test";
|
HtmlUtil.writerHtml(response,
|
"<html><script type=\"text/javascript\">window.open ('" + request.getScheme() + "://"
|
+ request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/"
|
+ url + "','_top')</script></html>");
|
try {
|
subject.login(token);
|
loginLog.setUserId(((LoginUserInfo) subject.getPrincipal()).getId());
|
loginLog.setSuccess(Boolean.TRUE);
|
systemLoginLogService.create(loginLog);
|
} catch (AuthenticationException e) {
|
log.error(ResponseStatus.ACCOUNT_INCORRECT.getMessage(), e);
|
loginLog.setReason(e.getMessage().length() > 200 ? (e.getMessage().substring(0, 190) + "...") : e.getMessage());
|
loginLog.setSuccess(Boolean.FALSE);
|
systemLoginLogService.create(loginLog);
|
throw new BusinessException(ResponseStatus.ACCOUNT_INCORRECT.getCode(), !(e.getCause() instanceof BusinessException) ? "账号或密码不正确" : e.getCause().getMessage());
|
}
|
|
|
}
|
}
|