修改单点登录

This commit is contained in:
chy
2026-03-11 00:08:12 +08:00
parent d7aa0de3be
commit 94a7c9e000
8 changed files with 114 additions and 17 deletions

View File

@@ -178,7 +178,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/login/**","/logout", "/health", "/user/loginCode/**").permitAll()
.antMatchers("/login/**","/logout","/outlogcas", "/health", "/user/loginCode/**").permitAll()
.mvcMatchers(HttpMethod.GET,"/dict/item/**").permitAll()
.anyRequest().authenticated()
.and()

View File

@@ -106,6 +106,14 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
</dependencies>
<build>

View File

@@ -1,6 +1,12 @@
package top.lidee.taie.http;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import top.lidee.taie.http.ssl.SslSocketClient;
import okhttp3.*;
@@ -135,4 +141,40 @@ public class HttpClientUtils {
Request request = new Request.Builder().url(url).headers(headers).delete().build();
httpClient.newCall(request).enqueue(callback);
}
public static String getPost(String url,String Token)
{
try {
// 创建HttpClient实例
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建POST请求
HttpPost httpPost = new HttpPost(url);
// 设置请求体
StringEntity params = new StringEntity("refreshToken="+Token);
httpPost.setEntity(params);
httpPost.setHeader("Authorization",Token);
// 发送请求并获取响应
CloseableHttpResponse response = httpClient.execute(httpPost);
try {
// 获取响应状态码
System.out.println(response.getStatusLine().getStatusCode());
// 获取响应体内容
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
return result;
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
}

View File

@@ -8,7 +8,7 @@
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.14</version>
<!--<version>2.3.5.RELEASE</version>-->
<relativePath/> <!-- lookup parent from repository -->
<relativePath/>
</parent>
<groupId>top.lidee.taie</groupId>
<artifactId>lidee-common</artifactId>

View File

@@ -21,6 +21,7 @@ import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.io.IOException;
import java.util.Map;
/**
@@ -99,9 +100,19 @@ public class AccessUserController extends LideeBaseController<AccessUserParam, A
}
@PostMapping({"/logincas"})
public ResponseBean logincas(@RequestBody @Validated LideeUserDto dto) {
public ResponseBean logincas(@RequestBody @Validated LideeUserDto dto) throws IOException {
return responseSuccessWithData(accessUserService.logincas(dto));
}
@PostMapping({"/outlogcas"})
public ResponseBean outlogcas(@RequestBody @Validated LideeUserDto dto){
Boolean data =accessUserService.outlogincas(dto);
return responseSuccessWithData(data);
}
/**
* 修改自己的密码

View File

@@ -8,6 +8,7 @@ import top.lidee.taie.business.modules.accessuser.dao.entity.AccessUser;
import top.lidee.taie.business.modules.accessuser.controller.param.AccessUserParam;
import top.lidee.taie.curd.service.LideeBaseService;
import java.io.IOException;
import java.util.Map;
/**
@@ -43,7 +44,10 @@ public interface AccessUserService extends LideeBaseService<AccessUserParam, Acc
*/
LideeUserDto login(LideeUserDto lideeUserDto);
LideeUserDto logincas(LideeUserDto lideeUserDto);
LideeUserDto logincas(LideeUserDto lideeUserDto) throws IOException;
Boolean outlogincas(LideeUserDto lideeUserDto);
/**
* 修改密码

View File

@@ -10,6 +10,7 @@ import top.lidee.taie.exception.BusinessException;
import top.lidee.taie.exception.BusinessExceptionBuilder;
import top.lidee.taie.curd.mapper.LideeBaseMapper;
import top.lidee.taie.holder.UserContentHolder;
import top.lidee.taie.http.HttpClientUtils;
import top.lidee.taie.utils.LideeUtils;
import top.lidee.taie.utils.JwtBean;
import top.lidee.taie.business.code.ResponseCode;
@@ -34,6 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
@@ -71,6 +73,14 @@ public class AccessUserServiceImpl implements AccessUserService {
@Autowired
private CacheHelper cacheHelper;
@Value("${cas.loginurl:}")
private String casLoginUrl;
@Value("${cas.outlogouturl:}")
private String casoutlogouturl;
@Override
public Map getRoleTree(String loginName, String operator) {
Map<String, Object> result = new HashMap<String, Object>();
@@ -146,7 +156,7 @@ public class AccessUserServiceImpl implements AccessUserService {
throw BusinessExceptionBuilder.build("用户不存在");
}
//默认密码
accessUser.setPassword(MD5Util.encrypt(MD5Util.encrypt(defaultPassword.concat("lidee@123"))));
accessUser.setPassword(MD5Util.encrypt(MD5Util.encrypt(defaultPassword.concat("Lidee@654!"))));
accessUserMapper.updateById(accessUser);
return true;
}
@@ -220,20 +230,19 @@ public class AccessUserServiceImpl implements AccessUserService {
@Override
public LideeUserDto logincas(LideeUserDto lideeUserDto) {
String loginName="";
String url = casLoginUrl+"?refreshToken="+ lideeUserDto.getLoginName();
//根据ticker获取用户信息
String response = HttpClientUtils.getPost(url, lideeUserDto.getLoginName());
String loginName = lideeUserDto.getLoginName();
JSONObject jsonObject = null;
jsonObject= JSONObject.parseObject(response);
if(Integer.parseInt(jsonObject.getString("code"))==0)
{
loginName=jsonObject.getJSONObject("data").getString("username");
}
loginName="admin";
// String password = lideeUserDto.getPassword();
String password ="Lidee@654!";
//将ticket保存到 redis 方便退出使用 key为username_ticket
// 1.判断用户是否存在
LambdaQueryWrapper<AccessUser> wrapper = Wrappers.lambdaQuery();
wrapper.eq(AccessUser::getLoginName, loginName);
@@ -290,10 +299,25 @@ public class AccessUserServiceImpl implements AccessUserService {
String ldUserStr = JSONObject.toJSONString(lduser);
cacheHelper.stringSetExpire(userKey, ldUserStr, 3600);
cacheHelper.stringSetExpire(loginName+"_cas", lideeUserDto.getLoginName(), 360000);
// String ss= cacheHelper.stringGet(loginName+"_cas");
return lduser;
}
@Override
public Boolean outlogincas(LideeUserDto lideeUserDto) {
String token= cacheHelper.stringGet(lideeUserDto.getLoginName()+"_cas");
String url = casoutlogouturl+"?refreshToken="+ token;
String response = HttpClientUtils.getPost(url, token);
return true;
}
/**
* 修改密码
@@ -341,7 +365,7 @@ public class AccessUserServiceImpl implements AccessUserService {
switch (operationEnum) {
case INSERT:
//lidee是为了和前端加密保持一致
entity.setPassword(MD5Util.encrypt(MD5Util.encrypt(defaultPassword.concat("lidee"))));
entity.setPassword(MD5Util.encrypt(MD5Util.encrypt(defaultPassword.concat("Lidee@654!"))));
break;
case UPDATE:
//更新用户不允许修改密码
@@ -353,4 +377,7 @@ public class AccessUserServiceImpl implements AccessUserService {
}
}
}

View File

@@ -145,3 +145,8 @@ customer:
##新增用户默认密码
default:
password: Lidee@654!
cas:
# loginurl: http://127.0.0.1:48080/admin-api/system/auth/user-info
# outlogouturl: http://127.0.0.1:48080/admin-api/system/auth/logout-client
loginurl: http://192.168.1.241/admin-api/system/auth/user-info
outlogouturl: http://192.168.1.241/admin-api/system/auth/logout-client