|
@@ -0,0 +1,465 @@
|
|
|
+package org.jeecg.modules.ssoClient.controller;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import io.swagger.annotations.Api;
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.shiro.SecurityUtils;
|
|
|
+import org.jeecg.common.api.vo.Result;
|
|
|
+import org.jeecg.common.constant.CacheConstant;
|
|
|
+import org.jeecg.common.constant.CommonConstant;
|
|
|
+import org.jeecg.common.constant.SymbolConstant;
|
|
|
+import org.jeecg.common.system.util.JwtUtil;
|
|
|
+import org.jeecg.common.system.vo.LoginUser;
|
|
|
+import org.jeecg.common.util.PasswordUtil;
|
|
|
+import org.jeecg.common.util.RedisUtil;
|
|
|
+import org.jeecg.common.util.oConvertUtils;
|
|
|
+import org.jeecg.modules.base.service.BaseCommonService;
|
|
|
+import org.jeecg.modules.iotedgeConfig.service.IIotedgeConfigService;
|
|
|
+import org.jeecg.modules.iotedgeConfig.util.ConfigInfo;
|
|
|
+import org.jeecg.modules.ssoClient.constants.SSOConstants;
|
|
|
+import org.jeecg.modules.system.entity.SysDepart;
|
|
|
+import org.jeecg.modules.system.entity.SysTenant;
|
|
|
+import org.jeecg.modules.system.entity.SysUser;
|
|
|
+import org.jeecg.modules.system.model.SysLoginModel;
|
|
|
+import org.jeecg.modules.system.service.ISysDepartService;
|
|
|
+import org.jeecg.modules.system.service.ISysDictService;
|
|
|
+import org.jeecg.modules.system.service.ISysTenantService;
|
|
|
+import org.jeecg.modules.system.service.ISysUserService;
|
|
|
+import org.jeecg.modules.system.service.impl.SysBaseApiImpl;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.*;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import javax.servlet.http.Cookie;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 调用SSO接口登录登出
|
|
|
+ *
|
|
|
+ * @author: nn
|
|
|
+ * @date: 2024年07月11日 14:33
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/sso/dy")
|
|
|
+@Api(tags=".....................调用SSO接口登录登出")
|
|
|
+@Slf4j
|
|
|
+public class SSOLoginLogoutController {
|
|
|
+ @Autowired
|
|
|
+ private ISysUserService sysUserService;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtil redisUtil;
|
|
|
+ @Autowired
|
|
|
+ private ISysDepartService sysDepartService;
|
|
|
+ @Autowired
|
|
|
+ private ISysTenantService sysTenantService;
|
|
|
+ @Autowired
|
|
|
+ private ISysDictService sysDictService;
|
|
|
+ @Resource
|
|
|
+ private BaseCommonService baseCommonService;
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ private IIotedgeConfigService configService;
|
|
|
+ @Autowired
|
|
|
+ private SysBaseApiImpl sysBaseApi;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * sso校验token
|
|
|
+ * */
|
|
|
+ @GetMapping("/tokenvalidation")
|
|
|
+ @ApiOperation("sso校验token")
|
|
|
+ public Boolean ssoJYToken(String token){
|
|
|
+ Result<JSONObject> result = ssoTokenValidation(token);
|
|
|
+ if(result.isSuccess()){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<JSONObject> ssoTokenValidation(String token) {
|
|
|
+ // String url = "http://192.168.2.248:8188/v4.0/tokenvalidation;
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_TOKENVALIDATION);
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("token",token);
|
|
|
+
|
|
|
+ // 设置请求头部
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+
|
|
|
+ // 创建RestTemplate实例
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ // 创建HttpEntity封装请求体和头部信息
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(), headers);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发送POST请求并获取响应
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
+ url,
|
|
|
+ HttpMethod.POST,
|
|
|
+ entity,
|
|
|
+ String.class);
|
|
|
+
|
|
|
+ // 处理响应
|
|
|
+ if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
+// return Result.ok();
|
|
|
+ String responseBody = response.getBody();
|
|
|
+ if (responseBody != null && !responseBody.equals("")) {
|
|
|
+ // 假设body中包含了token信息,可以在这里进行处理
|
|
|
+ if (responseBody.contains("Token") || responseBody.contains("Token")) {
|
|
|
+ String accessToken = extractAccessTokenFromResponseBody(responseBody,"accessToken");
|
|
|
+ String refreshToken = extractAccessTokenFromResponseBody(responseBody,"refreshToken");
|
|
|
+ System.out.println("成功获取到token:" + token);
|
|
|
+ return Result.ok(accessToken);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("未找到body信息");
|
|
|
+ return Result.ok("未找到body信息");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return Result.error("校验失败");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @ApiOperation("登录接口")
|
|
|
+ @RequestMapping(value = "/login", method = RequestMethod.POST)
|
|
|
+ public Result<JSONObject> login(HttpServletRequest request, HttpServletResponse response, @RequestBody SysLoginModel sysLoginModel){
|
|
|
+
|
|
|
+ Result<JSONObject> result = new Result<JSONObject>();
|
|
|
+ String username = sysLoginModel.getUsername();
|
|
|
+ String password = sysLoginModel.getPassword();
|
|
|
+
|
|
|
+ String eiToken = ssodlcs(username, password);
|
|
|
+ if(eiToken==null || "".equals(eiToken)){
|
|
|
+ result.error("登录请求失败");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ //1. 校验用户是否有效
|
|
|
+ SysUser sysUser = sysUserService.getUserAll(username);
|
|
|
+ result = sysUserService.checkUserIsEffective(sysUser);
|
|
|
+ if(!result.isSuccess()) {
|
|
|
+ // TODO 已经单点登录成功了的用户如果在本系统不存在,新增用户?
|
|
|
+ if(result.getMessage().equals(SSOConstants.BCZ) ){
|
|
|
+ log.info("用户在本系统不存在,新增该用户");
|
|
|
+ JSONObject addJSONObject = toAddJSONObject(username, password);
|
|
|
+ addUser(addJSONObject);
|
|
|
+ }else if(result.getMessage().equals(SSOConstants.YZX) || result.getMessage().equals(SSOConstants.YDj)){
|
|
|
+ // TODO 已注销或冻结的用户怎么处理?——首先不应该允许在本系统注销或者冻结用户?——如果真的有恢复正常状态?
|
|
|
+ editUserStatusOrDel(username);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //用户登录信息 TODO-token
|
|
|
+ userInfo1(sysUser, result, eiToken);
|
|
|
+ LoginUser loginUser = new LoginUser();
|
|
|
+ BeanUtils.copyProperties(sysUser, loginUser);
|
|
|
+ baseCommonService.addLog("用户名: " + username + ",登录成功!", CommonConstant.LOG_TYPE_1, null,loginUser);
|
|
|
+ //update-end--Author:wangshuai Date:20200714 for:登录日志没有记录人员
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject toAddJSONObject(String username, String password){
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("workNo", "");
|
|
|
+ jsonObject.put("password", password);
|
|
|
+ jsonObject.put("confirmpassword", password);
|
|
|
+ jsonObject.put("phone", "18888888888");
|
|
|
+ jsonObject.put("selecteddeparts", "");
|
|
|
+ jsonObject.put("selectedroles", SSOConstants.ADMIN_ROLE_ID);
|
|
|
+ jsonObject.put("activitiSync", "1");
|
|
|
+ jsonObject.put("departIds", "");
|
|
|
+ jsonObject.put("userIdentity", "1");
|
|
|
+ jsonObject.put("username", username);
|
|
|
+ jsonObject.put("realname", username);
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void addUser(JSONObject jsonObject){
|
|
|
+ String selectedRoles = jsonObject.getString("selectedroles");
|
|
|
+ String selectedDeparts = jsonObject.getString("selecteddeparts");
|
|
|
+ try {
|
|
|
+ SysUser user = JSON.parseObject(jsonObject.toJSONString(), SysUser.class);
|
|
|
+ user.setCreateTime(new Date());//设置创建时间
|
|
|
+ String salt = oConvertUtils.randomGen(8);
|
|
|
+ user.setSalt(salt);
|
|
|
+ String passwordEncode = PasswordUtil.encrypt(user.getUsername(), user.getPassword(), salt);
|
|
|
+ user.setPassword(passwordEncode);
|
|
|
+ user.setStatus(1);
|
|
|
+ user.setDelFlag(CommonConstant.DEL_FLAG_0);
|
|
|
+ //用户表字段org_code不能在这里设置他的值
|
|
|
+ user.setOrgCode(null);
|
|
|
+ // 保存用户走一个service 保证事务
|
|
|
+ sysUserService.saveUser(user, selectedRoles, selectedDeparts);
|
|
|
+ baseCommonService.addLog("添加用户,username: " +user.getUsername() ,CommonConstant.LOG_TYPE_2, 2);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void editUserStatusOrDel(String username){
|
|
|
+ try {
|
|
|
+ baseCommonService.addLog("还原被逻辑删除、被冻结的用户,username: " +username ,CommonConstant.LOG_TYPE_2, 2);
|
|
|
+ sysUserService.editUserStatusOrDel(username);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * sso登录,获取token
|
|
|
+ */
|
|
|
+ public String ssodlcs(String username, String password){
|
|
|
+ // String url = "http://192.168.2.248:8188/v4.0/auth/native";
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_LOGIN_URL);
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ jsonObject.put("username",username);
|
|
|
+ jsonObject.put("password",password);
|
|
|
+
|
|
|
+ // 设置请求头部
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+
|
|
|
+ // 创建RestTemplate实例
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ // 创建HttpEntity封装请求体和头部信息
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(jsonObject.toString(), headers);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发送POST请求并获取响应
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
+ url,
|
|
|
+ HttpMethod.POST,
|
|
|
+ entity,
|
|
|
+ String.class);
|
|
|
+
|
|
|
+ // 处理响应
|
|
|
+ if (response.getStatusCode() == HttpStatus.OK) {
|
|
|
+ String responseBody = response.getBody();
|
|
|
+ if (responseBody != null && !responseBody.equals("")) {
|
|
|
+ // 假设body中包含了token信息,可以在这里进行处理
|
|
|
+ if (responseBody.contains("Token") || responseBody.contains("Token")) {
|
|
|
+ String token = extractTokenFromResponseBody(responseBody);
|
|
|
+ System.out.println("成功获取到token:" + token);
|
|
|
+ // 可以将token保存到本地或者设置到后续请求的Header中
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("未找到body信息");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("登录失败,HTTP状态码:" + response.getStatusCode());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("登录请求失败:" + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String extractTokenFromResponseBody(String responseBody) {
|
|
|
+ // 假设body格式为 {"accessToken":"token","expiresIn":1720663523,"refreshToken":"a6ab6460-3f21-11ef-beaa-e454e833f52c","tokenType":"Bearer"}
|
|
|
+ try {
|
|
|
+ // 使用ObjectMapper解析JSON
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ JsonNode jsonNode = mapper.readTree(responseBody);
|
|
|
+
|
|
|
+ String accessToken = jsonNode.get("accessToken").asText();
|
|
|
+ System.out.println("Access Token: " + accessToken);
|
|
|
+
|
|
|
+ return accessToken;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String extractAccessTokenFromResponseBody(String responseBody, String tokenType) {
|
|
|
+ // 假设body格式为 {"accessToken":"token","expiresIn":1720663523,"refreshToken":"a6ab6460-3f21-11ef-beaa-e454e833f52c","tokenType":"Bearer"}
|
|
|
+ try {
|
|
|
+ // 使用ObjectMapper解析JSON
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ JsonNode jsonNode = mapper.readTree(responseBody);
|
|
|
+
|
|
|
+ String token = jsonNode.get(tokenType).asText();
|
|
|
+ System.out.println(tokenType+ ": " + token);
|
|
|
+
|
|
|
+ return token;
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 用户信息
|
|
|
+ *
|
|
|
+ * @param sysUser
|
|
|
+ * @param result
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Result<JSONObject> userInfo1(SysUser sysUser, Result<JSONObject> result, String eiToken) {
|
|
|
+ String username = sysUser.getUsername();
|
|
|
+ String syspassword = sysUser.getPassword();
|
|
|
+ // 获取用户部门信息
|
|
|
+ JSONObject obj = new JSONObject(new LinkedHashMap<>());
|
|
|
+
|
|
|
+ // 生成token TODO 用本系统生成的token还是sso的token
|
|
|
+// String token = JwtUtil.sign(username, syspassword);
|
|
|
+ String token =eiToken;
|
|
|
+ // 设置token缓存有效时间
|
|
|
+ redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
|
|
|
+ redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME * 2 / 1000);
|
|
|
+ obj.put("token", token);
|
|
|
+
|
|
|
+ // update-begin--Author:sunjianlei Date:20210802 for:获取用户租户信息
|
|
|
+ String tenantIds = sysUser.getRelTenantIds();
|
|
|
+ if (oConvertUtils.isNotEmpty(tenantIds)) {
|
|
|
+ List<Integer> tenantIdList = new ArrayList<>();
|
|
|
+ for(String id: tenantIds.split(SymbolConstant.COMMA)){
|
|
|
+ tenantIdList.add(Integer.valueOf(id));
|
|
|
+ }
|
|
|
+ // 该方法仅查询有效的租户,如果返回0个就说明所有的租户均无效。
|
|
|
+ List<SysTenant> tenantList = sysTenantService.queryEffectiveTenant(tenantIdList);
|
|
|
+ if (tenantList.size() == 0) {
|
|
|
+ result.error500("与该用户关联的租户均已被冻结,无法登录!");
|
|
|
+ return result;
|
|
|
+ } else {
|
|
|
+ obj.put("tenantList", tenantList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // update-end--Author:sunjianlei Date:20210802 for:获取用户租户信息
|
|
|
+
|
|
|
+ obj.put("userInfo", sysUser);
|
|
|
+
|
|
|
+ List<SysDepart> departs = sysDepartService.queryUserDeparts(sysUser.getId());
|
|
|
+ obj.put("departs", departs);
|
|
|
+ if (departs == null || departs.size() == 0) {
|
|
|
+ obj.put("multi_depart", 0);
|
|
|
+ } else if (departs.size() == 1) {
|
|
|
+ sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
|
|
|
+ obj.put("multi_depart", 1);
|
|
|
+ } else {
|
|
|
+ //查询当前是否有登录部门
|
|
|
+ // update-begin--Author:wangshuai Date:20200805 for:如果用戶为选择部门,数据库为存在上一次登录部门,则取一条存进去
|
|
|
+ SysUser sysUserById = sysUserService.getById(sysUser.getId());
|
|
|
+ if(oConvertUtils.isEmpty(sysUserById.getOrgCode())){
|
|
|
+ sysUserService.updateUserDepart(username, departs.get(0).getOrgCode());
|
|
|
+ }
|
|
|
+ // update-end--Author:wangshuai Date:20200805 for:如果用戶为选择部门,数据库为存在上一次登录部门,则取一条存进去
|
|
|
+ obj.put("multi_depart", 2);
|
|
|
+ }
|
|
|
+ obj.put("sysAllDictItems", sysDictService.queryAllDictItems());
|
|
|
+ result.setResult(obj);
|
|
|
+ result.success("登录成功");
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 退出登录
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @RequestMapping(value = "/logout")
|
|
|
+ public Result<Object> logout(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+ log.info("开始退出登录----------------------------------");
|
|
|
+
|
|
|
+ //SSO登出
|
|
|
+ String msg = IoTedgessolgout(response);
|
|
|
+ if(!SSOConstants.LOGOUT_SUCCESS.equals(msg)) return Result.error(msg);
|
|
|
+
|
|
|
+ // 创建一个同名的 Cookie 对象
|
|
|
+ Cookie cookie = new Cookie("EIToken", "");
|
|
|
+ // 设置 Cookie 的路径和最大生存时间(单位:秒)
|
|
|
+ cookie.setPath("/"); // 设置路径为根路径,确保能在整个应用中删除该 Cookie
|
|
|
+ cookie.setMaxAge(0); // 将 Cookie 的最大生存时间设置为 0,使其立即过期
|
|
|
+ // 将 Cookie 添加到 HttpServletResponse 中
|
|
|
+ response.addCookie(cookie);
|
|
|
+
|
|
|
+ //用户退出逻辑
|
|
|
+ String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
|
|
|
+ if(oConvertUtils.isEmpty(token)) {
|
|
|
+ return Result.error("退出登录失败!");
|
|
|
+ }
|
|
|
+ String username = JwtUtil.getUsername(token);
|
|
|
+ LoginUser sysUser = sysBaseApi.getUserByName(username);
|
|
|
+ if(sysUser!=null) {
|
|
|
+ //update-begin--Author:wangshuai Date:20200714 for:登出日志没有记录人员
|
|
|
+ baseCommonService.addLog("用户名: "+sysUser.getRealname()+",退出成功!", CommonConstant.LOG_TYPE_1, null,sysUser);
|
|
|
+ //update-end--Author:wangshuai Date:20200714 for:登出日志没有记录人员
|
|
|
+ log.info(" 用户名: "+sysUser.getRealname()+",退出成功! ");
|
|
|
+ //清空用户登录Token缓存
|
|
|
+ redisUtil.del(CommonConstant.PREFIX_USER_TOKEN + token);
|
|
|
+ //清空用户登录Shiro权限缓存
|
|
|
+ redisUtil.del(CommonConstant.PREFIX_USER_SHIRO_CACHE + sysUser.getId());
|
|
|
+ //清空用户的缓存信息(包括部门信息),例如sys:cache:user::<username>
|
|
|
+ redisUtil.del(String.format("%s::%s", CacheConstant.SYS_USERS_CACHE, sysUser.getUsername()));
|
|
|
+ //调用shiro的logout
|
|
|
+ SecurityUtils.getSubject().logout();
|
|
|
+ return Result.ok("退出登录成功!");
|
|
|
+ }else {
|
|
|
+ return Result.error("Token无效!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**IoTedge sso登出*/
|
|
|
+ public String IoTedgessolgout(HttpServletResponse response1){
|
|
|
+ //String url = "http://192.168.2.248:8082/v1/sso/auth";
|
|
|
+ String url = configService.getConfigValue(ConfigInfo.SSO_LOGOUT_URL);
|
|
|
+
|
|
|
+ // 设置请求头部
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+
|
|
|
+ // 创建RestTemplate实例
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+
|
|
|
+ // 创建HttpEntity封装请求体和头部信息
|
|
|
+ HttpEntity<String> entity = new HttpEntity<>(headers);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 发送POST请求并获取响应
|
|
|
+ ResponseEntity<String> response = restTemplate.exchange(
|
|
|
+ url,
|
|
|
+ HttpMethod.DELETE,
|
|
|
+ entity,
|
|
|
+ String.class);
|
|
|
+
|
|
|
+ // 处理响应
|
|
|
+ if (response.getStatusCode() == HttpStatus.NO_CONTENT) {
|
|
|
+ return SSOConstants.LOGOUT_SUCCESS;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ System.err.println("登出请求失败:" + e.getMessage());
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+}
|