|
|
|
@ -5,27 +5,29 @@ package com.ruoyi.jjh.declaration.single.controller;
|
|
|
|
|
* @since 2024/5/22 15:50
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
import com.alibaba.fastjson2.JSON;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.ruoyi.common.constant.Constants;
|
|
|
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
|
|
|
|
import com.ruoyi.common.core.domain.entity.SysUser;
|
|
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
|
|
import com.ruoyi.common.core.domain.model.RegisterBody;
|
|
|
|
|
import com.ruoyi.common.core.redis.RedisCache;
|
|
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
|
|
|
import com.ruoyi.common.exception.user.UserPasswordNotMatchException;
|
|
|
|
|
import com.ruoyi.common.utils.MessageUtils;
|
|
|
|
|
import com.ruoyi.common.utils.SecurityUtils;
|
|
|
|
|
import com.ruoyi.framework.manager.AsyncManager;
|
|
|
|
|
import com.ruoyi.framework.manager.factory.AsyncFactory;
|
|
|
|
|
import com.ruoyi.framework.security.context.AuthenticationContextHolder;
|
|
|
|
|
import com.ruoyi.framework.web.service.SysRegisterService;
|
|
|
|
|
import com.ruoyi.framework.web.service.TokenService;
|
|
|
|
|
import com.ruoyi.jjh.declaration.entity.BmsEnterpriseBasicInfo;
|
|
|
|
|
import com.ruoyi.jjh.declaration.mapper.BmsEnterpriseBasicInfoMapper;
|
|
|
|
|
import com.ruoyi.jjh.declaration.single.dto.reqponse.UserMainResponse;
|
|
|
|
|
import com.ruoyi.jjh.declaration.single.dto.reqponse.UserResponse;
|
|
|
|
|
import com.ruoyi.system.service.ISysUserService;
|
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
@ -45,6 +47,7 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -62,6 +65,13 @@ public class SingleLoginController {
|
|
|
|
|
@Value("${infoUrl}")
|
|
|
|
|
private String infoUrl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Value("${logoutUrl}")
|
|
|
|
|
private String logoutUrl;
|
|
|
|
|
|
|
|
|
|
@Value("${returnUrl}")
|
|
|
|
|
private String returnUrl;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private RedisCache redisCache;
|
|
|
|
|
|
|
|
|
@ -72,9 +82,13 @@ public class SingleLoginController {
|
|
|
|
|
@Resource
|
|
|
|
|
private TokenService tokenService;
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private BmsEnterpriseBasicInfoMapper bmsEnterpriseBasicInfoMapper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
private SysRegisterService sysRegisterService;
|
|
|
|
|
private ISysUserService userService;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 统一登陆
|
|
|
|
|
*
|
|
|
|
@ -83,13 +97,12 @@ public class SingleLoginController {
|
|
|
|
|
@ApiOperation("统一登陆")
|
|
|
|
|
@GetMapping("/login")
|
|
|
|
|
public void login(HttpServletResponse response) throws IOException {
|
|
|
|
|
String clientUrl = "http://192.168.0.111:80/system/singlelogin/getInfo";
|
|
|
|
|
//变成参数模式,appid 用于校验提交的来源地址,生产环境会校验,试用环境暂不校验
|
|
|
|
|
String scUrl = "clientUrl=" + URLEncoder.encode(clientUrl, "UTF-8") + "&appid=b40b40e3-f188-4e00-b67a6ec5701ce02b";
|
|
|
|
|
String scUrl = "clientUrl=" + URLEncoder.encode(returnUrl, "UTF-8") + "?appid=b40b40e3-f188-4e00-b67a6ec5701ce02b";
|
|
|
|
|
//加密后的客户端地址
|
|
|
|
|
scUrl = Base64.getEncoder().encodeToString(scUrl.getBytes());
|
|
|
|
|
//最终跳转地址
|
|
|
|
|
String jumpurl = url + "&scUrl=" + URLEncoder.encode(scUrl, "UTF-8");
|
|
|
|
|
String jumpurl = url + "?scUrl=" + URLEncoder.encode(scUrl, "UTF-8");
|
|
|
|
|
// 添加请求头
|
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
|
response.setHeader("X-Requested-With", "XMLHttpRequest");
|
|
|
|
@ -116,54 +129,104 @@ public class SingleLoginController {
|
|
|
|
|
responseBody = response.body();
|
|
|
|
|
JSONObject jsonObj = JSONUtil.parseObj(responseBody);
|
|
|
|
|
JSONObject dataObj = jsonObj.getJSONObject("data");
|
|
|
|
|
JSONObject mainobj = dataObj.getJSONObject("mainbody");
|
|
|
|
|
UserResponse res = JSONUtil.toBean(dataObj, UserResponse.class);
|
|
|
|
|
UserMainResponse req = JSONUtil.toBean(mainobj, UserMainResponse.class);
|
|
|
|
|
// 将用户信息存储到Redis中
|
|
|
|
|
String key = "user:" + clientToken;
|
|
|
|
|
// 设置过期时间(可选)
|
|
|
|
|
int expirationSeconds = 24 * 60;
|
|
|
|
|
// 设置过期时间为8小时
|
|
|
|
|
redisCache.setCacheObject(key, res, expirationSeconds, TimeUnit.MINUTES);
|
|
|
|
|
String token = singleLogin(res.getUserid());
|
|
|
|
|
res.setToken(token);
|
|
|
|
|
res.setMain(req);
|
|
|
|
|
return AjaxResult.success(res);
|
|
|
|
|
// 将用户信息存储到Redis中
|
|
|
|
|
String key = "user:" + clientToken;
|
|
|
|
|
//判断是法人用户
|
|
|
|
|
if("ep".equals(dataObj.get("usertype"))){
|
|
|
|
|
JSONObject mainobj = dataObj.getJSONObject("mainbody");
|
|
|
|
|
UserResponse res = JSONUtil.toBean(dataObj, UserResponse.class);
|
|
|
|
|
UserMainResponse req = JSONUtil.toBean(mainobj, UserMainResponse.class);
|
|
|
|
|
//存储到企业信息库
|
|
|
|
|
QueryWrapper<BmsEnterpriseBasicInfo> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
queryWrapper.eq("credit_code", req.getUscc());
|
|
|
|
|
BmsEnterpriseBasicInfo bmsEnterpriseBasicInfo = bmsEnterpriseBasicInfoMapper.selectOne(queryWrapper);
|
|
|
|
|
BmsEnterpriseBasicInfo bms = new BmsEnterpriseBasicInfo();
|
|
|
|
|
Long id;
|
|
|
|
|
if (bmsEnterpriseBasicInfo == null) {
|
|
|
|
|
bms.setCreditCode(req.getUscc());
|
|
|
|
|
bms.setEnterpriseName(req.getEpname());
|
|
|
|
|
bms.setIsDeleted(0L);
|
|
|
|
|
bmsEnterpriseBasicInfoMapper.insert(bms);
|
|
|
|
|
id = bms.getId();
|
|
|
|
|
}else {
|
|
|
|
|
id = bmsEnterpriseBasicInfo.getId();
|
|
|
|
|
}
|
|
|
|
|
// 设置过期时间为8小时
|
|
|
|
|
String token = singleLogin(req.getUscc(),req.getEpname(),id);
|
|
|
|
|
res.setToken(token);
|
|
|
|
|
res.setMain(req);
|
|
|
|
|
redisCache.setCacheObject(key, res, expirationSeconds, TimeUnit.MINUTES);
|
|
|
|
|
|
|
|
|
|
return AjaxResult.success(res);
|
|
|
|
|
}else {
|
|
|
|
|
log.error(response.body());
|
|
|
|
|
throw new ServiceException("未知异常请联系管理员");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.error(response.body());
|
|
|
|
|
throw new ServiceException("未知异常请联系管理员");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@ApiOperation(value = "退出登录")
|
|
|
|
|
@GetMapping("/enterpriseLogout")
|
|
|
|
|
public AjaxResult enterpriseLogout(@RequestParam("clienttoken") String clienttoken) {
|
|
|
|
|
String url = logoutUrl + "?clienttoken=" + clienttoken;
|
|
|
|
|
HttpResponse execute = HttpUtil.createGet(url).execute();
|
|
|
|
|
String str;
|
|
|
|
|
// 获取响应状态码
|
|
|
|
|
int statusCode = execute.getStatus();
|
|
|
|
|
if(statusCode==200){
|
|
|
|
|
str = "退出成功";
|
|
|
|
|
}else {
|
|
|
|
|
str = "退出失败";
|
|
|
|
|
}
|
|
|
|
|
return AjaxResult.success(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String singleLogin(String userId) {
|
|
|
|
|
private String singleLogin(String userName,String nickName,Long id) {
|
|
|
|
|
// 用户验证
|
|
|
|
|
Authentication authentication;
|
|
|
|
|
try {
|
|
|
|
|
RegisterBody registerBody=new RegisterBody();
|
|
|
|
|
registerBody.setValue(String.valueOf(1));
|
|
|
|
|
registerBody.setUsername(userId);
|
|
|
|
|
registerBody.setPassword("admin123");
|
|
|
|
|
sysRegisterService.register(registerBody);
|
|
|
|
|
SysUser user = new SysUser();
|
|
|
|
|
user.setUserName(userName);
|
|
|
|
|
user.setNickName(nickName);
|
|
|
|
|
user.setPassword("admin123");
|
|
|
|
|
Long[] roles = new Long[1];
|
|
|
|
|
roles[0]=101L;
|
|
|
|
|
user.setRoleIds(roles);
|
|
|
|
|
user.setStatus("0");
|
|
|
|
|
user.setUserType("01");
|
|
|
|
|
user.setEnterpriseId(id);
|
|
|
|
|
//如果用户名也就是信用代码没有重复的就新增用户
|
|
|
|
|
if (userService.checkUserNameUnique(user))
|
|
|
|
|
{
|
|
|
|
|
user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
|
|
|
|
|
userService.insertUser(user);
|
|
|
|
|
}
|
|
|
|
|
//新增user账号密码
|
|
|
|
|
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userId, "admin123");
|
|
|
|
|
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userName, "admin123");
|
|
|
|
|
AuthenticationContextHolder.setContext(authenticationToken);
|
|
|
|
|
// 该方法会去调用UserDetailsServiceImpl.loadUserByUsername
|
|
|
|
|
authentication = authenticationManager.authenticate(authenticationToken);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
if (e instanceof BadCredentialsException) {
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(userId, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
|
|
|
|
|
throw new UserPasswordNotMatchException();
|
|
|
|
|
} else {
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(userId, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGIN_FAIL, e.getMessage()));
|
|
|
|
|
throw new ServiceException(e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
AuthenticationContextHolder.clearContext();
|
|
|
|
|
}
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(userId, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
|
|
AsyncManager.me().execute(AsyncFactory.recordLogininfor(userName, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
|
|
|
|
|
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
|
|
// 生成token
|
|
|
|
|
return tokenService.createToken(loginUser);
|
|
|
|
|
Map<String, Object> token = tokenService.createToken(loginUser);
|
|
|
|
|
return String.valueOf(token.get("access_token"));
|
|
|
|
|
}
|
|
|
|
|
}
|