parent
ffed318958
commit
d9c5c8b63b
@ -0,0 +1,184 @@
|
||||
package com.ruoyi.gysl.login.controller;
|
||||
|
||||
/**
|
||||
* @author dong
|
||||
* @since 2024/5/22 15:50
|
||||
*/
|
||||
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.gysl.login.dto.response.UserMainResponse;
|
||||
import com.ruoyi.gysl.login.dto.response.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.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.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Base64;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 单点登陆
|
||||
*/
|
||||
@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;
|
||||
|
||||
@Value("${logoutUrl}")
|
||||
private String logoutUrl;
|
||||
|
||||
@Value("${returnUrl}")
|
||||
private String returnUrl;
|
||||
|
||||
@Resource
|
||||
private RedisCache redisCache;
|
||||
|
||||
|
||||
@Value("${enterpriseAppId}")
|
||||
private String enterpriseAppId;
|
||||
|
||||
@Value("${enterpriseAppsecret}")
|
||||
private String enterpriseAppsecret;
|
||||
|
||||
/**
|
||||
* 统一登陆
|
||||
*/
|
||||
@ApiOperation("统一登陆")
|
||||
@GetMapping("/login")
|
||||
public void login(HttpServletResponse response) throws IOException {
|
||||
//变成参数模式,appid 用于校验提交的来源地址,生产环境会校验,试用环境暂不校验
|
||||
String scUrl = "clientUrl=" + URLEncoder.encode(returnUrl, "UTF-8") + "?appid=" + enterpriseAppId;
|
||||
//加密后的客户端地址
|
||||
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(String clientToken) throws Exception {
|
||||
HttpResponse response = HttpUtil.createGet(infoUrl)
|
||||
.form("clienttoken", clientToken)
|
||||
// .addHeaders(generationHeader())
|
||||
.execute();
|
||||
String responseBody;
|
||||
// 获取响应状态码
|
||||
int statusCode = response.getStatus();
|
||||
if (statusCode == 200) {
|
||||
// 获取响应内容
|
||||
responseBody = response.body();
|
||||
JSONObject jsonObj = JSONUtil.parseObj(responseBody);
|
||||
JSONObject dataObj = jsonObj.getJSONObject("data");
|
||||
// 设置过期时间(可选)
|
||||
int expirationSeconds = 24 * 60;
|
||||
// 将用户信息存储到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);
|
||||
|
||||
//如果在企业库里面找不到该企业就返回错误
|
||||
// BmsEnterpriseBasicInfo bmsEnterpriseBasicInfo =
|
||||
// bmsEnterpriseBasicInfoMapper.findByQytyxydm(req.getUscc());
|
||||
Long id;
|
||||
// if (bmsEnterpriseBasicInfo == null) {
|
||||
// throw new ServiceException("该企业不在库!请稍后再试!");
|
||||
// } else {
|
||||
// id = bmsEnterpriseBasicInfo.getId();
|
||||
// }
|
||||
// 设置过期时间为8小时
|
||||
// String token = singleLoginService.singleLogin(req.getUscc(), req.getEpname(), id, "01", null,null);
|
||||
// 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("未知异常请联系管理员");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*
|
||||
* @param clienttoken clienttoken
|
||||
* @return 响应类
|
||||
*/
|
||||
@ApiOperation(value = "退出登录")
|
||||
@GetMapping("/enterpriseLogout")
|
||||
public AjaxResult enterpriseLogout(@RequestParam("clienttoken") String clienttoken) {
|
||||
String url = logoutUrl + "?clienttoken=" + clienttoken;
|
||||
HttpResponse execute = HttpUtil.createGet(url)
|
||||
// .addHeaders(generationHeader())
|
||||
.execute();
|
||||
String str;
|
||||
// 获取响应状态码
|
||||
int statusCode = execute.getStatus();
|
||||
if (statusCode == 200) {
|
||||
str = "退出成功";
|
||||
} else {
|
||||
str = "退出失败";
|
||||
}
|
||||
return AjaxResult.success(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* 正式环境需要的请求头
|
||||
*
|
||||
* @return 请求头
|
||||
*/
|
||||
private Map<String, String> generationHeader() {
|
||||
// 定义时间格式化器
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
|
||||
String format = LocalDateTime.now().format(formatter);
|
||||
//正式端需要请求头
|
||||
HashMap<String, String> headers = new HashMap<>();//存放请求头,可以存放多个请求头
|
||||
headers.put("appid", enterpriseAppId);
|
||||
headers.put("time", format);
|
||||
headers.put("sign", DigestUtil.md5Hex(enterpriseAppId + enterpriseAppsecret + format));
|
||||
return headers;
|
||||
}
|
||||
}
|
@ -1,55 +0,0 @@
|
||||
package com.ruoyi.gysl.utils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
public class ImgBase64Extractor {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
String imgUrl ="http://39.101.188.84:7071/profile/upload/2025/03/24/abc_20250324154129A006.jpg";
|
||||
|
||||
URL url = new URL(imgUrl);
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setDoInput(true);
|
||||
// 添加请求头
|
||||
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
|
||||
connection.setRequestProperty("Accept", "image/webp,image/apng,image/*,*/*;q=0.8");
|
||||
connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
|
||||
|
||||
// 连接并获取响应
|
||||
connection.connect();
|
||||
|
||||
// 检查响应码
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
// 连接成功,获取输入流
|
||||
InputStream inputStream = connection.getInputStream();
|
||||
|
||||
// 读取输入流
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
byte[] buffer = new byte[1024];
|
||||
int bytesRead;
|
||||
|
||||
while ((bytesRead = inputStream.read(buffer)) != -1) {
|
||||
outputStream.write(buffer, 0, bytesRead);
|
||||
}
|
||||
|
||||
inputStream.close();
|
||||
byte[] imageBytes = outputStream.toByteArray();
|
||||
System.out.println(imageBytes);
|
||||
} else {
|
||||
System.out.println("Error: Server returned HTTP response code: " + responseCode);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue