parent
916c55fc65
commit
1e76abb50d
@ -0,0 +1,169 @@
|
||||
package com.ruoyi.jjh.declaration.single.controller;
|
||||
|
||||
/**
|
||||
* @author dong
|
||||
* @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.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
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.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.single.dto.reqponse.UserMainResponse;
|
||||
import com.ruoyi.jjh.declaration.single.dto.reqponse.UserResponse;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.security.authentication.AuthenticationManager;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.util.Base64;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 单点登陆
|
||||
*/
|
||||
@Api(tags = "单点登陆")
|
||||
@RestController
|
||||
@RequestMapping("/system/singlelogin")
|
||||
public class SingleLoginController {
|
||||
private static final Logger log = LoggerFactory.getLogger(SingleLoginController.class);
|
||||
@Value("${url}")
|
||||
private String url;
|
||||
|
||||
|
||||
@Value("${infoUrl}")
|
||||
private String infoUrl;
|
||||
|
||||
@Resource
|
||||
private RedisCache redisCache;
|
||||
|
||||
|
||||
@Resource
|
||||
private AuthenticationManager authenticationManager;
|
||||
|
||||
@Resource
|
||||
private TokenService tokenService;
|
||||
|
||||
|
||||
@Resource
|
||||
private SysRegisterService sysRegisterService;
|
||||
/**
|
||||
* 统一登陆
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@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";
|
||||
//加密后的客户端地址
|
||||
scUrl = Base64.getEncoder().encodeToString(scUrl.getBytes());
|
||||
//最终跳转地址
|
||||
String jumpurl = url + "&scUrl=" + URLEncoder.encode(scUrl, "UTF-8");
|
||||
// 添加请求头
|
||||
response.setHeader("Content-Type", "application/json");
|
||||
response.setHeader("X-Requested-With", "XMLHttpRequest");
|
||||
|
||||
response.sendRedirect(jumpurl);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "获取用户信息",response = UserResponse.class)
|
||||
@GetMapping("/getInfo")
|
||||
public AjaxResult getInfo(@RequestParam("clientToken") String clientToken) throws Exception {
|
||||
String url = infoUrl + "?clienttoken=" + clientToken;
|
||||
HttpResponse response = HttpUtil.createGet(url).execute();
|
||||
String responseBody = null;
|
||||
// 获取响应状态码
|
||||
int statusCode = response.getStatus();
|
||||
if (statusCode == 200) {
|
||||
// 获取响应内容
|
||||
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);
|
||||
} else {
|
||||
log.error(response.body());
|
||||
throw new ServiceException("未知异常请联系管理员");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String singleLogin(String userId) {
|
||||
// 用户验证
|
||||
Authentication authentication;
|
||||
try {
|
||||
RegisterBody registerBody=new RegisterBody();
|
||||
registerBody.setValue(String.valueOf(1));
|
||||
registerBody.setUsername(userId);
|
||||
registerBody.setPassword("admin123");
|
||||
sysRegisterService.register(registerBody);
|
||||
//新增user账号密码
|
||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(userId, "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")));
|
||||
throw new UserPasswordNotMatchException();
|
||||
} else {
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(userId, 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")));
|
||||
LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
||||
// 生成token
|
||||
return tokenService.createToken(loginUser);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.ruoyi.jjh.declaration.single.dto.reqponse;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author dong
|
||||
* @since 2024/5/27 14:37
|
||||
*/
|
||||
@Data
|
||||
@ApiModel("企业机构代码")
|
||||
public class UserMainResponse {
|
||||
|
||||
/**
|
||||
* 对应企业的组织机构代码
|
||||
*/
|
||||
@ApiModelProperty(value = "对应企业的组织机构代码")
|
||||
private String organcode;
|
||||
|
||||
|
||||
/**
|
||||
* 对应企业的统一社会信用代码
|
||||
*/
|
||||
@ApiModelProperty(value = "对应企业的统一社会信用代码")
|
||||
private String uscc;
|
||||
|
||||
|
||||
/**
|
||||
* 对应企业名称
|
||||
*/
|
||||
@ApiModelProperty(value = "对应企业名称")
|
||||
private String epname;
|
||||
|
||||
}
|
Loading…
Reference in new issue