|
@@ -4,6 +4,8 @@ package org.bfkj.services;
|
|
|
import com.fasterxml.jackson.annotation.JsonInclude;
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
|
|
|
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
|
|
import org.bfkj.apos.Log;
|
|
|
import org.bfkj.domain.*;
|
|
|
import org.bfkj.domain.log.Applog;
|
|
@@ -11,30 +13,35 @@ import org.bfkj.domain.log.LogEntity;
|
|
|
import org.bfkj.domain.log.Userlog;
|
|
|
import org.bfkj.dtos.R;
|
|
|
import org.bfkj.dtos.ServiceDto;
|
|
|
+import org.bfkj.envs.HttpEnv;
|
|
|
import org.bfkj.services.cache.CodeCacheService;
|
|
|
import org.bfkj.utils.CommonUtil;
|
|
|
import org.bfkj.utils.RandomGraphic;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.security.NoSuchAlgorithmException;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
|
|
|
|
+import static org.bfkj.envs.HttpEnv.*;
|
|
|
+
|
|
|
@Service
|
|
|
public class SecurityService {
|
|
|
|
|
|
|
|
|
- private final static Map<String, List<String>> alias = new HashMap<>();
|
|
|
+ private static final Map<String, List<String>> alias = new HashMap<>();
|
|
|
|
|
|
static {
|
|
|
- alias.put("appid", List.of("appid", "app_id", "appId", "APPID"));
|
|
|
- alias.put("appsecret", List.of("appSecret", "app_secret", "APP_SECRET", "appsecret", "APPSECRET"));
|
|
|
- alias.put("sessionid", List.of("sessionId", "sessionid", "SESSIONID", "SESSION_ID", "session_id"));
|
|
|
- alias.put("requestip", List.of("requestIp", "requestip", "request_ip", "REQUEST_IP", "request_ip", "REQUESTIP", "ip"));
|
|
|
+ alias.put(APP_ID, List.of(APP_ID, "app_id", "appId", "APPID"));
|
|
|
+ alias.put(APP_SECRET, List.of(APP_SECRET, "appSecret", "app_secret", "APP_SECRET", "appsecret", "APPSECRET"));
|
|
|
+ alias.put(SESSION_ID, List.of(SESSION_ID, "sessionId", "SESSIONID", "SESSION_ID", "session_id"));
|
|
|
+ alias.put(REQUEST_IP, List.of(REQUEST_IP, "requestIp", "request_ip", "REQUEST_IP", "request_ip", "REQUESTIP", "ip"));
|
|
|
alias.put("username", List.of("username", "userName", "user_name", "USER_NAME", "USERNAME"));
|
|
|
alias.put("password", List.of("password", "pwd", "PWD", "PASSWORD"));
|
|
|
alias.put("version", List.of("version", "Version", "VERSION"));
|
|
|
- alias.put("verifycode", List.of("verifycode", "verifyCode", "code"));
|
|
|
+ alias.put(VERIFY_CODE, List.of(VERIFY_CODE, "verifyCode", "code"));
|
|
|
}
|
|
|
|
|
|
private final ApplicationService applicationService;
|
|
@@ -43,10 +50,8 @@ public class SecurityService {
|
|
|
private final PermissionsService permissionsService;
|
|
|
private final UserinfoService userinfoService;
|
|
|
private final CodeCacheService codeCacheService;
|
|
|
- private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
- private ObjectMapper objectMapper = new ObjectMapper() {{
|
|
|
- setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
|
- }};
|
|
|
+ private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
+ private final ObjectMapper objectMapper;
|
|
|
|
|
|
public SecurityService(ApplicationService applicationService, ApplicationconnectlogService applicationconnectlogService, UserloginlogService userloginlogService, PermissionsService permissionsService, UserinfoService userinfoService, CodeCacheService codeCacheService) {
|
|
|
this.applicationService = applicationService;
|
|
@@ -55,65 +60,65 @@ public class SecurityService {
|
|
|
this.permissionsService = permissionsService;
|
|
|
this.userinfoService = userinfoService;
|
|
|
this.codeCacheService = codeCacheService;
|
|
|
+ this.objectMapper = new ObjectMapper();
|
|
|
+ this.objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
|
|
+ this.objectMapper.registerModule(new Jdk8Module());
|
|
|
+ this.objectMapper.registerModule(new JavaTimeModule());
|
|
|
}
|
|
|
|
|
|
//安全类服务
|
|
|
//连接认证--获取连接令牌
|
|
|
@Log(Log.LogType.APP)
|
|
|
- public ServiceDto<Map<String, Object>, LogEntity> getToken(Map<String, Object> requestData) throws JsonProcessingException {
|
|
|
- Optional<String> appid = getValue("appid", requestData);
|
|
|
- Optional<String> appSecret = getValue("appsecret", requestData);
|
|
|
- Optional<String> requestIp = getValue("requestip", requestData);
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
+ public ServiceDto<Map<String, Object>, LogEntity> getToken(Map<String, Object> requestData) throws JsonProcessingException, NoSuchAlgorithmException {
|
|
|
+ String appid = getValue(APP_ID, requestData);
|
|
|
+ String appSecret = getValue(APP_SECRET, requestData);
|
|
|
+ String requestIp = getValue(REQUEST_IP, requestData);
|
|
|
+ String sessionId = getValue(SESSION_ID, requestData);
|
|
|
ServiceDto<Map<String, Object>, LogEntity> serviceDto = new ServiceDto<>();
|
|
|
- Application application = null;
|
|
|
- if (appid.isPresent() && appSecret.isPresent()) {
|
|
|
-// 无条件删除过期的数据
|
|
|
- applicationconnectlogService.removeExpiresData();
|
|
|
+ Application application;
|
|
|
|
|
|
- application = applicationService.findByAppId(appid.get());
|
|
|
- if (appSecret.get().equals(application.getAppsecret())) {
|
|
|
+ applicationconnectlogService.removeExpiresData();
|
|
|
+ application = applicationService.findByAppIdAndAppSecret(appid, appSecret);
|
|
|
+ if (Objects.nonNull(application)) {
|
|
|
+
|
|
|
+ Appconnectlog applicationconnectlog = applicationconnectlogService.findByAppidAndRequestIp(appid, requestIp);
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ if (Objects.isNull(applicationconnectlog)) {
|
|
|
+ applicationconnectlog = new Appconnectlog();
|
|
|
// 令牌
|
|
|
- String md5Token = CommonUtil.toMD5("%s:%s".formatted(LocalDateTime.now(), sessionId));
|
|
|
-// 有效期时长
|
|
|
- Long apptokeneffective = application.getApptokeneffective();
|
|
|
-// 过期时间
|
|
|
- LocalDateTime expiresTime = LocalDateTime.now().plusSeconds(apptokeneffective);
|
|
|
-// 新增记录
|
|
|
- Appconnectlog applicationconnectlog = new Appconnectlog();
|
|
|
-
|
|
|
- applicationconnectlog.setAppid(appid.get());
|
|
|
- applicationconnectlog.setExpiretime(expiresTime);
|
|
|
+ String md5Token = CommonUtil.toMD5("%s:%s".formatted(now, sessionId));
|
|
|
applicationconnectlog.setApptoken(md5Token);
|
|
|
- applicationconnectlog.setRequestip(requestIp.get());
|
|
|
- applicationconnectlog.setRequesttime(LocalDateTime.now());
|
|
|
- applicationconnectlog.setLasttime(LocalDateTime.now());
|
|
|
- applicationconnectlogService.save(applicationconnectlog);
|
|
|
- Map<String, Object> data = new HashMap<>();
|
|
|
- data.put("token", md5Token);
|
|
|
- data.put("expirestime", expiresTime.format(dateTimeFormatter));
|
|
|
- data.put("appname", application.getAppname());
|
|
|
- data.put("appenname", application.getAppengname());
|
|
|
- data.put("logo", application.getApplogo());
|
|
|
- data.put("smalllogo", application.getSmalllogo());
|
|
|
- data.put("background", application.getBackgroundimage());
|
|
|
- data.put("securitycoderule", application.getSecuritycoderule());
|
|
|
- serviceDto.setReturnData(R.success("0", data));
|
|
|
- serviceDto.setSuccess(true);
|
|
|
-
|
|
|
- } else {
|
|
|
- serviceDto.setSuccess(false);
|
|
|
- serviceDto.setReturnData(R.fail("-1", "用户或密码错误"));
|
|
|
+ applicationconnectlog.setAppid(appid);
|
|
|
+ applicationconnectlog.setRequestip(requestIp);
|
|
|
}
|
|
|
+// 有效期时长(分钟)
|
|
|
+ Long apptokeneffective = application.getApptokeneffective();
|
|
|
+// 过期时间分钟
|
|
|
+ LocalDateTime expiresTime = now.plusMinutes(apptokeneffective);
|
|
|
+ applicationconnectlog.setExpiretime(expiresTime);
|
|
|
+ applicationconnectlog.setRequesttime(now);
|
|
|
+ applicationconnectlog.setLasttime(now);
|
|
|
+ applicationconnectlogService.saveOrUpdate(applicationconnectlog);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("apptoken", applicationconnectlog.getApptoken());
|
|
|
+ data.put("appeffective", expiresTime.format(dateTimeFormatter));
|
|
|
+ data.put("appname", application.getAppname());
|
|
|
+ data.put("appengname", application.getAppengname());
|
|
|
+ data.put("applogo", application.getApplogo());
|
|
|
+ data.put("appsmalllogo", application.getSmalllogo());
|
|
|
+ data.put("background", application.getBackgroundimage());
|
|
|
+ data.put("appcoderule", application.getSecuritycoderule());
|
|
|
+ serviceDto.setReturnData(R.success("0", data));
|
|
|
+ serviceDto.setSuccess(true);
|
|
|
} else {
|
|
|
serviceDto.setSuccess(false);
|
|
|
- serviceDto.setReturnData(R.fail("-1", "appid 或者 appSecret 错误"));
|
|
|
+ serviceDto.setReturnData(R.fail("-1", "appid或者appsecret错误"));
|
|
|
}
|
|
|
Applog logData = new Applog();
|
|
|
- logData.setAppid(appid.orElse(null));
|
|
|
+ logData.setAppid(appid);
|
|
|
logData.setApiname((Objects.nonNull(application) ? application.getAppname() : null));
|
|
|
- logData.setRequestip(requestIp.get());
|
|
|
- logData.setSessionid(sessionId.get());
|
|
|
+ logData.setRequestip(requestIp);
|
|
|
+ logData.setSessionid(sessionId);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(serviceDto.getReturnData()));
|
|
|
serviceDto.setLogData(logData);
|
|
@@ -123,46 +128,35 @@ public class SecurityService {
|
|
|
//校验连接令牌
|
|
|
@Log(Log.LogType.APP)
|
|
|
public ServiceDto<Map<String, Object>, LogEntity> verifyToken(Map<String, Object> requestData) throws JsonProcessingException {
|
|
|
- Optional<String> token = getValue("token", requestData);
|
|
|
+ String token = getValue(APP_TOKEN, requestData);
|
|
|
|
|
|
- Optional<String> requestIp = getValue("requestip", requestData);
|
|
|
+ String requestIp = getValue(REQUEST_IP, requestData);
|
|
|
|
|
|
ServiceDto<Map<String, Object>, LogEntity> serviceDto = new ServiceDto<>();
|
|
|
String appid = null;
|
|
|
- if (token.isEmpty()) {
|
|
|
- serviceDto.setSuccess(false);
|
|
|
- serviceDto.setReturnData(R.fail("-1", "token错误"));
|
|
|
+ Application application = null;
|
|
|
+ applicationconnectlogService.removeExpiresData();
|
|
|
+ Appconnectlog applicationLog = applicationconnectlogService.findByTokenAndRequestIp(token, requestIp);
|
|
|
+ if (Objects.nonNull(applicationLog)) {
|
|
|
+ appid = applicationLog.getAppid();
|
|
|
+ application = applicationService.findByAppId(appid);
|
|
|
+ serviceDto.setSuccess(true);
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("validstatus", true);
|
|
|
+ data.put("appid", applicationLog.getAppid());
|
|
|
+ data.put("application", application);
|
|
|
+ serviceDto.setReturnData(R.success("0", "token校验通过", data));
|
|
|
} else {
|
|
|
- Appconnectlog applicationLog = applicationconnectlogService.findByTokenAndRequestIp(token.get(), requestIp.get());
|
|
|
-
|
|
|
- if (Objects.nonNull(applicationLog)) {
|
|
|
- appid = applicationLog.getAppid();
|
|
|
- if (LocalDateTime.now().isAfter(applicationLog.getExpiretime())) {
|
|
|
- serviceDto.setSuccess(false);
|
|
|
- serviceDto.setReturnData(R.fail("-1", "token已过期"));
|
|
|
- } else {
|
|
|
- serviceDto.setSuccess(true);
|
|
|
-
|
|
|
- Map<String, Object> data = new HashMap<>();
|
|
|
- data.put("validstatus", true);
|
|
|
- data.put("appid", applicationLog.getAppid());
|
|
|
- serviceDto.setReturnData(R.success("0", "token校验通过", data));
|
|
|
- }
|
|
|
- } else {
|
|
|
- Map<String, Object> data = new HashMap<>();
|
|
|
- data.put("validstatus", false);
|
|
|
- serviceDto.setReturnData(R.success("-1", "token校验通过", data));
|
|
|
- }
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("validstatus", false);
|
|
|
+ serviceDto.setReturnData(R.success("-1", "token无效", data));
|
|
|
}
|
|
|
Applog logData = new Applog();
|
|
|
logData.setAppid(appid);
|
|
|
- Application application = applicationService.findByAppId(appid);
|
|
|
-
|
|
|
logData.setApiname((Objects.nonNull(application) ? application.getAppname() : null));
|
|
|
-
|
|
|
- logData.setRequestip(requestIp.get());
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- logData.setSessionid(sessionId.get());
|
|
|
+ logData.setRequestip(requestIp);
|
|
|
+ String sessionId = getValue(SESSION_ID, requestData);
|
|
|
+ logData.setSessionid(sessionId);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(serviceDto.getReturnData()));
|
|
|
serviceDto.setLogData(logData);
|
|
@@ -172,37 +166,38 @@ public class SecurityService {
|
|
|
//刷新连接令牌
|
|
|
@Log(Log.LogType.APP)
|
|
|
public ServiceDto<Map<String, Object>, LogEntity> refreshToken(Map<String, Object> requestData) throws JsonProcessingException {
|
|
|
- ServiceDto<Map<String, Object>, LogEntity> verified = verifyToken(requestData);
|
|
|
|
|
|
- if (!verified.isSuccess()) {
|
|
|
- return verified;
|
|
|
- }
|
|
|
- ServiceDto<Map<String, Object>, LogEntity> resultData = new ServiceDto<>();
|
|
|
- Optional<String> requestIp = getValue("requestIp", requestData);
|
|
|
- Optional<String> token = getValue("token", requestData);
|
|
|
- String appid = (String) verified.getReturnData().getReturnData().get("appid");
|
|
|
- Application application = applicationService.findByAppId(appid);
|
|
|
- LocalDateTime expiresTime = LocalDateTime.now().plusSeconds(application.getApptokeneffective());
|
|
|
- if (applicationconnectlogService.updateApplicationLogTokenExpiresTime(appid, token.get(), requestIp.get(), expiresTime)) {
|
|
|
- Map<String, Object> data = new HashMap<>();
|
|
|
- data.put("expirestime", expiresTime.format(dateTimeFormatter));
|
|
|
- data.put("token", token.get());
|
|
|
- resultData.setSuccess(true);
|
|
|
- resultData.setReturnData(R.success("0", data));
|
|
|
+ ServiceDto<Map<String, Object>, LogEntity> resultData = verifyToken(requestData);
|
|
|
+ Application application = null;
|
|
|
|
|
|
- } else {
|
|
|
- resultData.setReturnData(R.fail("-1", "刷新令牌失败"));
|
|
|
- resultData.setSuccess(false);
|
|
|
+ String appid = null;
|
|
|
+ String sessionId = getValue(SESSION_ID, requestData);
|
|
|
+
|
|
|
+ String requestIp = getValue(REQUEST_IP, requestData);
|
|
|
+ if (resultData.isSuccess()) {
|
|
|
+ String token = getValue(APP_TOKEN, requestData);
|
|
|
+ application = (Application) resultData.getReturnData().getReturnData().get("application");
|
|
|
+ appid = application.getAppid();
|
|
|
+
|
|
|
+ LocalDateTime expiresTime = LocalDateTime.now().plusSeconds(application.getApptokeneffective());
|
|
|
+ String tokenStr = token;
|
|
|
+ if (applicationconnectlogService.updateApplicationLogTokenExpiresTime(appid, tokenStr, requestIp, expiresTime)) {
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("expirestime", expiresTime.format(dateTimeFormatter));
|
|
|
+ data.put("token", tokenStr);
|
|
|
+ resultData.setSuccess(true);
|
|
|
+ resultData.setReturnData(R.success("0", data));
|
|
|
+ } else {
|
|
|
+ resultData.setReturnData(R.fail("-1", "刷新令牌失败"));
|
|
|
+ resultData.setSuccess(false);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
Applog logData = new Applog();
|
|
|
logData.setAppid(appid);
|
|
|
-
|
|
|
logData.setApiname((Objects.nonNull(application) ? application.getAppname() : null));
|
|
|
-
|
|
|
- logData.setRequestip(requestIp.get());
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- logData.setSessionid(sessionId.get());
|
|
|
+ logData.setRequestip(requestIp);
|
|
|
+ logData.setSessionid(sessionId);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(resultData.getReturnData()));
|
|
|
resultData.setLogData(logData);
|
|
@@ -214,40 +209,43 @@ public class SecurityService {
|
|
|
public ServiceDto<Map<String, Object>, LogEntity> verifyCode(Map<String, Object> requestData) throws JsonProcessingException {
|
|
|
|
|
|
ServiceDto<Map<String, Object>, LogEntity> resultData = verifyToken(requestData);
|
|
|
- Optional<String> appidOpt = Optional.empty();
|
|
|
- Optional<String> appnameOpt = Optional.empty();
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- Optional<String> requestIp = getValue("requestip", requestData);
|
|
|
+ String sessionId = getValue(SESSION_ID, requestData);
|
|
|
+ String requestIp = getValue(REQUEST_IP, requestData);
|
|
|
+ Application application = null;
|
|
|
if (resultData.isSuccess()) {
|
|
|
- String appid = (String) resultData.getReturnData().getReturnData().get("appid");
|
|
|
- appidOpt.of(appid);
|
|
|
- Application application = applicationService.findByAppId(appid);
|
|
|
- if (Objects.nonNull(application)) {
|
|
|
- appnameOpt.of(application.getAppname());
|
|
|
- String securitycoderule = application.getSecuritycoderule();
|
|
|
- Long securitycodeeffective = application.getSecuritycodeeffective();
|
|
|
- Integer securitycoderulelength = application.getSecuritycoderulelength();
|
|
|
- Map<String, Object> codeMap = RandomGraphic.generateVerifyCode(securitycoderulelength, securitycoderule);
|
|
|
-
|
|
|
- String code = codeMap.get("verifyCode").toString();
|
|
|
- String verifyCodeImage = codeMap.get("verifyCodeImage").toString();
|
|
|
- codeCacheService.addCode(code, sessionId.get(), appid, securitycodeeffective, requestIp.get());
|
|
|
-
|
|
|
-// 编译后的验证码
|
|
|
+ application = (Application) resultData.getReturnData().getReturnData().get("application");
|
|
|
+ String securitycoderule = application.getSecuritycoderule();
|
|
|
+ Rule rule = parserRule(securitycoderule);
|
|
|
+ if (Objects.isNull(rule.range)) {
|
|
|
+ resultData.setSuccess(true);
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
- data.put("verifyCodeImage", verifyCodeImage);
|
|
|
+ data.put("verifyCodeImage", null);
|
|
|
resultData.setReturnData(R.success("0", data));
|
|
|
- resultData.setSuccess(true);
|
|
|
-
|
|
|
+ } else {
|
|
|
+ try {
|
|
|
+ RandomGraphic.CodeResult codeMap = RandomGraphic.generateVerifyCode(rule.max, rule.range);
|
|
|
+ String code = codeMap.verifyCode();
|
|
|
+ String verifyCodeImage = codeMap.verifyCodeImage();
|
|
|
+ codeCacheService.addCode(code, sessionId, application.getAppid(), application.getSecuritycodeeffective(), requestIp);
|
|
|
+// 编译后的验证码
|
|
|
+ Map<String, Object> data = new HashMap<>();
|
|
|
+ data.put("verifyCodeImage", verifyCodeImage);
|
|
|
+ resultData.setReturnData(R.success("0", data));
|
|
|
+ resultData.setSuccess(true);
|
|
|
+ } catch (IOException e) {
|
|
|
+ resultData.setReturnData(R.fail("-1", "生成验证码失败: " + e.getMessage()));
|
|
|
+ resultData.setSuccess(false);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
Applog logData = new Applog();
|
|
|
- logData.setAppid(appidOpt.orElse(null));
|
|
|
+ logData.setAppid(Objects.nonNull(application) ? application.getAppid() : null);
|
|
|
+ logData.setApiname(Objects.nonNull(application) ? application.getAppname() : null);
|
|
|
|
|
|
- logData.setApiname(appnameOpt.orElse(null));
|
|
|
-
|
|
|
- logData.setRequestip(requestIp.get());
|
|
|
- logData.setSessionid(sessionId.get());
|
|
|
+ logData.setRequestip(requestIp);
|
|
|
+ logData.setSessionid(sessionId);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(resultData.getReturnData()));
|
|
|
resultData.setLogData(logData);
|
|
@@ -260,151 +258,106 @@ public class SecurityService {
|
|
|
@Log(Log.LogType.USER)
|
|
|
public ServiceDto<Map<String, Object>, LogEntity> login(Map<String, Object> requestData) throws JsonProcessingException {
|
|
|
// 首先,通过verifyToken方法验证app的令牌(token)是否有效(返回是否有效、appid)。
|
|
|
- Optional<String> requestip = getValue("requestip", requestData);
|
|
|
- Optional<String> sessionid = getValue("sessionid", requestData);
|
|
|
- Optional<String> username = getValue("username", requestData);
|
|
|
- Optional<String> password = getValue("password", requestData);
|
|
|
- Optional<String> verifycode = getValue("verifycode", requestData);
|
|
|
+ String requestip = getValue(REQUEST_IP, requestData);
|
|
|
+ String sessionid = getValue(SESSION_ID, requestData);
|
|
|
+ String username = getValue("username", requestData);
|
|
|
+ String password = getValue("password", requestData);
|
|
|
+ String verifycode = getValue(VERIFY_CODE, requestData);
|
|
|
ServiceDto<Map<String, Object>, LogEntity> resultData = verifyToken(requestData);
|
|
|
- Optional<Long> userid = Optional.empty();
|
|
|
+ Long userid = 0L;
|
|
|
if (resultData.isSuccess()) {
|
|
|
|
|
|
- String appid = resultData.getReturnData().getReturnData().get("appid").toString();
|
|
|
+ Application application = (Application) resultData.getReturnData().getReturnData().get("application");
|
|
|
|
|
|
- Application application = applicationService.findByAppId(appid);
|
|
|
String securitycoderule = application.getSecuritycoderule();
|
|
|
-// if (Objects.nonNull(securitycoderule) && !codeCacheService.check(verifycode.get(), sessionid.get(), appid, requestip.get())) {
|
|
|
-// resultData.setSuccess(false);
|
|
|
-// resultData.setReturnData(R.fail("-1", "验证码错误"));
|
|
|
-// return resultData;
|
|
|
-// }
|
|
|
+ if (Objects.nonNull(securitycoderule) && !codeCacheService.check(verifycode, sessionid, application.getAppid(), requestip)) {
|
|
|
+ resultData.setSuccess(false);
|
|
|
+ resultData.setReturnData(R.fail("-1", "验证码错误"));
|
|
|
+ return resultData;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
- Userinfo userinfo = userinfoService.findByUsernameAndPassword(username.get(), password.get());
|
|
|
- userid.ofNullable(userinfo.getUserid());
|
|
|
+ String token = getValue("token", requestData);
|
|
|
+ Userinfo userinfo = userinfoService.findByUsernameAndPassword(username, password);
|
|
|
if (Objects.isNull(userinfo)) {
|
|
|
resultData.setSuccess(false);
|
|
|
resultData.setReturnData(R.fail("-1", "用户名或密码错误"));
|
|
|
} else {
|
|
|
+ userid = userinfo.getUserid();
|
|
|
|
|
|
- Userloginlog userloginlog = userloginlogService.findByUserIdAndSessionId(userinfo.getUserid(), sessionid.get());
|
|
|
+ List<Userloginlog> userloginlogs = userloginlogService.findByUserId(userinfo.getUserid());
|
|
|
|
|
|
Map<String, Object> data = new HashMap<>();
|
|
|
- if (Objects.nonNull(userloginlog)) {
|
|
|
-
|
|
|
- data.put("userstatus", "2");
|
|
|
+ Integer multilogin = application.getMultilogin();
|
|
|
+// 没有登录 或者允许多机登录
|
|
|
+ String requestIp = requestip;
|
|
|
+ String sessionId = sessionid;
|
|
|
+ String appToken = token;
|
|
|
+ if (userloginlogs.isEmpty() || 1 == multilogin) {
|
|
|
+ data.put("userstatus", "0");
|
|
|
resultData.setSuccess(true);
|
|
|
-
|
|
|
+ userloginlogService.insertUserLoginLog(requestIp, sessionId, userinfo.getUserid(), null, appToken, application.getAppid());
|
|
|
resultData.setReturnData(R.success("0", data));
|
|
|
} else {
|
|
|
- Integer multilogin = application.getMultilogin();
|
|
|
- Optional<String> token = getValue("token", requestData);
|
|
|
- if (1 == multilogin) {
|
|
|
- userloginlogService.insertUserLoginLog(requestip.get(), sessionid.get(), userinfo.getUserid(), null, token.get(), appid);
|
|
|
-
|
|
|
- data.put("userstatus", "0");
|
|
|
- resultData.setSuccess(true);
|
|
|
- resultData.setReturnData(R.success("0", data));
|
|
|
- } else {
|
|
|
-
|
|
|
-
|
|
|
- List<Userloginlog> userloginlogs = userloginlogService.findByUserId(userinfo.getUserid());
|
|
|
-// 没有登录
|
|
|
- if (userloginlogs.isEmpty()) {
|
|
|
- data.put("userstatus", "0");
|
|
|
- resultData.setSuccess(true);
|
|
|
- resultData.setReturnData(R.fail("0", "data"));
|
|
|
- } else {
|
|
|
- data.put("userstatus", "1");
|
|
|
- resultData.setSuccess(true);
|
|
|
- resultData.setReturnData(R.fail("0", "data"));
|
|
|
- }
|
|
|
- Appconnectlog appconnectlog = new Appconnectlog();
|
|
|
- appconnectlog.setApptoken(appconnectlog.getApptoken());
|
|
|
- appconnectlog.setAppid(appid);
|
|
|
- appconnectlog.setLasttime(LocalDateTime.now());
|
|
|
- appconnectlog.setRequesttime(LocalDateTime.now());
|
|
|
- appconnectlog.setRequestip(requestip.get());
|
|
|
- appconnectlog.setExpiretime(LocalDateTime.now().plusSeconds(application.getApptokeneffective()));
|
|
|
- applicationconnectlogService.save(appconnectlog);
|
|
|
-
|
|
|
- codeCacheService.remove(verifycode.get(), sessionid.get(), appid, requestip.get());
|
|
|
+// 有登录
|
|
|
+ data.put("userstatus", "0");
|
|
|
+ resultData.setSuccess(true);
|
|
|
+ resultData.setReturnData(R.fail("1", "data"));
|
|
|
+ if (userloginlogs.stream().filter(it -> it.getSessionid().equals(sessionId) && Objects.nonNull(it.getUsertoken())).findFirst().isEmpty()) {
|
|
|
+ userloginlogService.insertUserLoginLog(requestIp, sessionId, userinfo.getUserid(), null, appToken, application.getAppid());
|
|
|
}
|
|
|
+ Appconnectlog appconnectlog = new Appconnectlog();
|
|
|
+ appconnectlog.setApptoken(appconnectlog.getApptoken());
|
|
|
+ appconnectlog.setAppid(application.getAppid());
|
|
|
+ appconnectlog.setLasttime(LocalDateTime.now());
|
|
|
+ appconnectlog.setRequesttime(LocalDateTime.now());
|
|
|
+ appconnectlog.setRequestip(requestIp);
|
|
|
+ appconnectlog.setExpiretime(LocalDateTime.now().plusSeconds(application.getApptokeneffective()));
|
|
|
+ applicationconnectlogService.saveOrUpdate(appconnectlog);
|
|
|
+
|
|
|
+ codeCacheService.remove(sessionId, application.getAppid(), requestIp);
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
Userlog logData = new Userlog();
|
|
|
- logData.setUserid(userid.orElse(null));
|
|
|
+ logData.setUserid(userid);
|
|
|
|
|
|
- logData.setUsername(username.orElse(null));
|
|
|
+ logData.setUsername(username);
|
|
|
|
|
|
- logData.setRequestip(requestip.get());
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- logData.setSessionid(sessionId.orElse(null));
|
|
|
+ logData.setRequestip(requestip);
|
|
|
+ logData.setSessionid(sessionid);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(resultData.getReturnData()));
|
|
|
resultData.setLogData(logData);
|
|
|
return resultData;
|
|
|
-
|
|
|
-
|
|
|
-//
|
|
|
-// 如果令牌有效,继续执行以下步骤:
|
|
|
-// 从requestData中获取sessionid、requestip、用户名、密码、验证码(md5)。
|
|
|
-// 通过appid字段查找对应的application表记录
|
|
|
-// 判断记录中验证码规则
|
|
|
-// 如需验证验证码
|
|
|
-// 根据appid,ip,sessionid,和验证码查询数据库
|
|
|
-// 如不通过 返回错误
|
|
|
-// 验证用户名密码(根据用户名、密码到数据库查询即可,同时还可获取到userid)
|
|
|
-// 如不通过 返回错误
|
|
|
-// 通过userid、sessionid在userloginlog中查找
|
|
|
-// 如找到 用户状态设置为2
|
|
|
-// 如未找到
|
|
|
-// 获取application记录中的multilogin字段。
|
|
|
-// 如果 multilogin 等于1 则
|
|
|
-// 在userloginlog表中插入一条新的登录记录(userid、sessionid、ip、apptoken)。
|
|
|
-// 将用户状态设置为0
|
|
|
-// 如果 multilogin 不等于1 则
|
|
|
-// 通过用户ID在userloginlog表中查找对应的登录记录。
|
|
|
-// 如果登录记录不为空,
|
|
|
-// 将用户状态设置为1。
|
|
|
-// 如果登录记录为空,
|
|
|
-// 将用户状态设置为0。
|
|
|
-// 在userloginlog表中插入一条新的登录记录。
|
|
|
-// 将用户状态添加到result中。
|
|
|
-// 返回result作为用户令牌的结果。
|
|
|
-// 否则
|
|
|
-// 返回错误
|
|
|
}
|
|
|
|
|
|
|
|
|
//强制登录
|
|
|
@Log(Log.LogType.USER)
|
|
|
- public ServiceDto<Map<String, Object>, LogEntity> forceLogin(Map<String, Object> requestData) throws JsonProcessingException {
|
|
|
- Optional<String> requestip = getValue("requestip", requestData);
|
|
|
- Optional<String> sessionid = getValue("sessionid", requestData);
|
|
|
+ public ServiceDto<Map<String, Object>, LogEntity> forceLogin(Map<String, Object> requestData) throws JsonProcessingException, NoSuchAlgorithmException {
|
|
|
+ String requestip = getValue(REQUEST_IP, requestData);
|
|
|
+ String sessionid = getValue(SESSION_ID, requestData);
|
|
|
ServiceDto<Map<String, Object>, LogEntity> result = verifyToken(requestData);
|
|
|
- Optional<Long> userid = Optional.empty();
|
|
|
- Optional<String> username = Optional.empty();
|
|
|
+ Userinfo userinfo = null;
|
|
|
+ Application application = null;
|
|
|
if (result.isSuccess()) {
|
|
|
- Optional<String> apptoken = getValue("token", requestData);
|
|
|
- String appid = result.getReturnData().getReturnData().get("appid").toString();
|
|
|
- Application application = applicationService.findByAppId(appid);
|
|
|
+ String apptoken = getValue(APP_TOKEN, requestData);
|
|
|
+ application = (Application) result.getReturnData().getReturnData().get("application");
|
|
|
result.setSuccess(false);
|
|
|
if (Objects.isNull(application)) {
|
|
|
result.setReturnData(R.fail("-1", "没有找到应用配置"));
|
|
|
} else {
|
|
|
- Userloginlog userloginlog = userloginlogService.findByAppTokenAndSessionIdAndRequestIp(apptoken.get(), sessionid.get(), requestip.get());
|
|
|
+ Userloginlog userloginlog = userloginlogService.findByAppTokenAndSessionIdAndRequestIpAndAppId(apptoken, sessionid, requestip, application.getAppid());
|
|
|
if (Objects.isNull(userloginlog)) {
|
|
|
result.setReturnData(R.fail("-1", "登录失败"));
|
|
|
} else {
|
|
|
- userid.of(userloginlog.getUserid());
|
|
|
- Userinfo userinfo = userinfoService.findByUserId(userid.get());
|
|
|
- username.of(userinfo.getUsername());
|
|
|
+ userinfo = userinfoService.findByUserId(userloginlog.getUserid());
|
|
|
Long securitycodeeffective = application.getSecuritycodeeffective();
|
|
|
LocalDateTime expiresTime = LocalDateTime.now().plusSeconds(securitycodeeffective);
|
|
|
- String userToken = CommonUtil.toMD5("%s:%s".formatted(LocalDateTime.now(), sessionid.get()));
|
|
|
+ String userToken = CommonUtil.toMD5("%s:%s".formatted(LocalDateTime.now(), sessionid));
|
|
|
userloginlogService.updateUserToken(userloginlog.getLoginid(), userToken);
|
|
|
result.setSuccess(true);
|
|
|
HashMap<String, Object> data = new HashMap<>();
|
|
@@ -413,23 +366,22 @@ public class SecurityService {
|
|
|
result.setReturnData(R.success("0", data));
|
|
|
Integer multilogin = application.getMultilogin();
|
|
|
if (multilogin == 1) {
|
|
|
- userloginlogService.removeByLogIdAndUserIdAndAppId(userloginlog.getLoginid(), userloginlog.getUserid(), userloginlog.getAppid());
|
|
|
- } else {
|
|
|
- userloginlogService.removeExpires();
|
|
|
+ userloginlogService.removeByLogIdAndUserIdAndAppId(userloginlog.getLoginid());
|
|
|
}
|
|
|
+ userloginlogService.removeExpires();
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
+ codeCacheService.remove(sessionid, Objects.nonNull(application) ? application.getAppid() : "", requestip);
|
|
|
Userlog logData = new Userlog();
|
|
|
- logData.setUserid(userid.orElse(null));
|
|
|
+ logData.setUserid(Objects.nonNull(userinfo) ? userinfo.getUserid() : null);
|
|
|
|
|
|
- logData.setUsername(username.orElse(null));
|
|
|
+ logData.setUsername(Objects.nonNull(userinfo) ? userinfo.getUsername() : null);
|
|
|
|
|
|
|
|
|
- logData.setRequestip(requestip.get());
|
|
|
- logData.setSessionid(sessionid.orElse(null));
|
|
|
+ logData.setRequestip(requestip);
|
|
|
+ logData.setSessionid(sessionid);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(result.getReturnData()));
|
|
|
result.setLogData(logData);
|
|
@@ -456,9 +408,9 @@ public class SecurityService {
|
|
|
}
|
|
|
|
|
|
private ServiceDto<Map<String, Object>, LogEntity> checkUserToken(Map<String, Object> requestData) {
|
|
|
- Optional<String> userToken = getValue("usertoken", requestData);
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- Userloginlog userloginlog = userloginlogService.findByUserToken(userToken.get(), sessionId.get());
|
|
|
+ String userToken = getValue(USER_TOKEN, requestData);
|
|
|
+ String sessionId = getValue(SESSION_ID, requestData);
|
|
|
+ Userloginlog userloginlog = userloginlogService.findByUserToken(userToken, sessionId);
|
|
|
|
|
|
String appid = userloginlog.getAppid();
|
|
|
Application application = applicationService.findByAppId(appid);
|
|
@@ -469,6 +421,10 @@ public class SecurityService {
|
|
|
return result;
|
|
|
} else {
|
|
|
result.setSuccess(true);
|
|
|
+ Userinfo userinfo = userinfoService.findByUserId(userloginlog.getUserid());
|
|
|
+ HashMap<String, Object> data = new HashMap<>();
|
|
|
+ data.put("userInfo", userinfo);
|
|
|
+ result.setReturnData(R.success("0", data));
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -478,26 +434,21 @@ public class SecurityService {
|
|
|
@Log(Log.LogType.USER)
|
|
|
public ServiceDto<Map<String, Object>, LogEntity> logOut(Map<String, Object> requestData) throws JsonProcessingException {
|
|
|
ServiceDto<Map<String, Object>, LogEntity> resultData = checkUserToken(requestData);
|
|
|
- Optional<Long> userid = Optional.empty();
|
|
|
- Optional<String> username = Optional.empty();
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- Optional<String> requestIp = getValue("requestip", requestData);
|
|
|
+ String sessionId = getValue(SESSION_ID, requestData);
|
|
|
+ String requestIp = getValue(REQUEST_IP, requestData);
|
|
|
+ Userinfo userInfo = null;
|
|
|
if (resultData.isSuccess()) {
|
|
|
- Optional<String> userToken = getValue("usertoken", requestData);
|
|
|
- Userloginlog userloginlog = userloginlogService.findByUserToken(userToken.get(), sessionId.get());
|
|
|
- userid.of(userloginlog.getUserid());
|
|
|
- Userinfo userinfo = userinfoService.findByUserId(userloginlog.getUserid());
|
|
|
- username.of(userinfo.getUsername());
|
|
|
- userloginlogService.removeUserLoginLogByUserId(userloginlog.getUserid());
|
|
|
- permissionsService.removePermissions(userloginlog.getUserid());
|
|
|
+ userInfo = (Userinfo) resultData.getReturnData().getReturnData().get("userInfo");
|
|
|
+ userloginlogService.removeUserLoginLogByUserId(userInfo.getUserid());
|
|
|
+ permissionsService.removePermissions(userInfo.getUserid());
|
|
|
resultData.setReturnData(R.success("0", "成功", null));
|
|
|
}
|
|
|
|
|
|
Userlog logData = new Userlog();
|
|
|
- logData.setUserid(userid.orElse(null));
|
|
|
- logData.setUsername(username.orElse(null));
|
|
|
- logData.setRequestip(requestIp.get());
|
|
|
- logData.setSessionid(sessionId.get());
|
|
|
+ logData.setUserid(Objects.nonNull(userInfo) ? userInfo.getUserid() : null);
|
|
|
+ logData.setUsername(Objects.nonNull(userInfo) ? userInfo.getUsername() : null);
|
|
|
+ logData.setRequestip(requestIp);
|
|
|
+ logData.setSessionid(sessionId);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(resultData.getReturnData()));
|
|
|
resultData.setLogData(logData);
|
|
@@ -511,29 +462,22 @@ public class SecurityService {
|
|
|
|
|
|
ServiceDto<Map<String, Object>, LogEntity> checked = checkUserToken(requestData);
|
|
|
ServiceDto<List<Permissions>, LogEntity> resultData = new ServiceDto<>();
|
|
|
- Optional<Long> userid = Optional.empty();
|
|
|
- Optional<String> username = Optional.empty();
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- Optional<String> requestIp = getValue("requestip", requestData);
|
|
|
+ String sessionId = getValue(SESSION_ID, requestData);
|
|
|
+ String requestIp = getValue(REQUEST_IP, requestData);
|
|
|
+ Userinfo userInfo = null;
|
|
|
if (!checked.isSuccess()) {
|
|
|
resultData.setSuccess(false);
|
|
|
resultData.setReturnData(R.fail("-1", checked.getReturnData().getMessage()));
|
|
|
} else {
|
|
|
- Optional<String> userToken = getValue("usertoken", requestData);
|
|
|
- Userloginlog userloginlog = userloginlogService.findByUserToken(userToken.get(), sessionId.get());
|
|
|
- userid.of(userloginlog.getUserid());
|
|
|
- Userinfo userinfo = userinfoService.findByUserId(userloginlog.getUserid());
|
|
|
- username.of(userinfo.getUsername());
|
|
|
- List<Permissions> ps = permissionsService.getPermissions(userloginlog.getUserid().toString());
|
|
|
+ userInfo = (Userinfo) checked.getReturnData().getReturnData().get("userInfo");
|
|
|
+ List<Permissions> ps = permissionsService.getPermissions(userInfo.getUserid().toString());
|
|
|
resultData.setReturnData(R.success("0", ps));
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
Userlog logData = new Userlog();
|
|
|
- logData.setUserid(userid.orElse(null));
|
|
|
- logData.setUsername(username.orElse(null));
|
|
|
- logData.setRequestip(requestIp.orElse(null));
|
|
|
- logData.setSessionid(sessionId.orElse(null));
|
|
|
+ logData.setUserid(Objects.nonNull(userInfo) ? userInfo.getUserid() : null);
|
|
|
+ logData.setUsername(Objects.nonNull(userInfo) ? userInfo.getUsername() : null);
|
|
|
+ logData.setRequestip(requestIp);
|
|
|
+ logData.setSessionid(sessionId);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(resultData.getReturnData()));
|
|
|
resultData.setLogData(logData);
|
|
@@ -542,46 +486,41 @@ public class SecurityService {
|
|
|
|
|
|
//应用API及数据权限
|
|
|
public ServiceDto<Map<String, Object>, LogEntity> changePassword(Map<String, Object> requestData) throws JsonProcessingException {
|
|
|
- Optional<Long> userid = Optional.empty();
|
|
|
- Optional<String> username = Optional.empty();
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- Optional<String> requestIp = getValue("requestip", requestData);
|
|
|
+ String username = "";
|
|
|
+ String sessionId = getValue(SESSION_ID, requestData);
|
|
|
+ String requestIp = getValue(REQUEST_IP, requestData);
|
|
|
ServiceDto<Map<String, Object>, LogEntity> resultData = checkUserToken(requestData);
|
|
|
+ Userinfo userInfo = null;
|
|
|
if (resultData.isSuccess()) {
|
|
|
- Optional<String> oldPassword = getValue("oldpassword", requestData);
|
|
|
- Optional<String> password = getValue("password", requestData);
|
|
|
-
|
|
|
- Optional<String> userToken = getValue("usertoken", requestData);
|
|
|
- Userloginlog userloginlog = userloginlogService.findByUserToken(userToken.get(), sessionId.get());
|
|
|
- Long userId = userloginlog.getUserid();
|
|
|
- userid.of(userId);
|
|
|
-
|
|
|
- Userinfo userinfo = userinfoService.findByUserId(userId);
|
|
|
- if (Objects.nonNull(userinfo)) {
|
|
|
- username.of(userinfo.getUsername());
|
|
|
- String userpassword = userinfo.getUserpassword();
|
|
|
- if (!userpassword.equals(oldPassword.get())) {
|
|
|
- resultData.setSuccess(false);
|
|
|
- resultData.setReturnData(R.fail("-1", "密码错误"));
|
|
|
- } else {
|
|
|
- userinfoService.updateUserPassword(userId, password.get());
|
|
|
- }
|
|
|
- resultData.setSuccess(true);
|
|
|
- resultData.setReturnData(R.success("-1", "成功", null));
|
|
|
- } else {
|
|
|
+ String oldPassword = getValue("oldpassword", requestData);
|
|
|
+ String password = getValue("password", requestData);
|
|
|
+
|
|
|
|
|
|
+ userInfo = (Userinfo) resultData.getReturnData().getReturnData().get("userInfo");
|
|
|
+ String userpassword = userInfo.getUserpassword();
|
|
|
+ if (!userpassword.equals(oldPassword)) {
|
|
|
resultData.setSuccess(false);
|
|
|
- resultData.setReturnData(R.fail("-1", "用户没有找到"));
|
|
|
- }
|
|
|
+ resultData.setReturnData(R.fail("-1", "密码错误"));
|
|
|
+ } else {
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ boolean isTrue = userinfoService.updateUserPassword(password, now, userInfo.getUserid(), oldPassword);
|
|
|
+
|
|
|
+ resultData.setSuccess(isTrue);
|
|
|
+ if (isTrue) {
|
|
|
+ resultData.setReturnData(R.success("0", "成功", null));
|
|
|
+ } else {
|
|
|
|
|
|
+ resultData.setReturnData(R.fail("-1", "成功"));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
|
|
|
Userlog logData = new Userlog();
|
|
|
- logData.setUserid(userid.orElse(null));
|
|
|
- logData.setUsername(username.orElse(null));
|
|
|
- logData.setRequestip(requestIp.get());
|
|
|
- logData.setSessionid(sessionId.get());
|
|
|
+ logData.setUserid(Objects.nonNull(userInfo) ? userInfo.getUserid() : null);
|
|
|
+ logData.setUsername(username);
|
|
|
+ logData.setRequestip(requestIp);
|
|
|
+ logData.setSessionid(sessionId);
|
|
|
logData.setInputdata(objectMapper.writeValueAsString(requestData));
|
|
|
logData.setOutputdata(objectMapper.writeValueAsString(resultData.getReturnData()));
|
|
|
resultData.setLogData(logData);
|
|
@@ -589,44 +528,44 @@ public class SecurityService {
|
|
|
}
|
|
|
|
|
|
//用户心跳
|
|
|
- public ServiceDto<List<Permissions>, LogEntity> userHeartbeat(Map<String, Object> requestData) {
|
|
|
- ServiceDto<Map<String, Object>, LogEntity> checked = checkUserToken(requestData);
|
|
|
- ServiceDto<List<Permissions>, LogEntity> resultData = new ServiceDto<>();
|
|
|
- if (!checked.isSuccess()) {
|
|
|
- resultData.setSuccess(false);
|
|
|
- resultData.setReturnData(R.fail("-1", checked.getReturnData().getMessage()));
|
|
|
- return resultData;
|
|
|
- } else {
|
|
|
- Optional<String> userToken = getValue("usertoken", requestData);
|
|
|
- Optional<String> sessionId = getValue("sessionid", requestData);
|
|
|
- Userloginlog userloginlog1 = userloginlogService.findByUserToken(userToken.get(), sessionId.get());
|
|
|
- if (Objects.nonNull(userloginlog1)) {
|
|
|
- List<Userloginlog> userloginlogs = userloginlogService.findByUserId(userloginlog1.getUserid());
|
|
|
+ public ServiceDto<Map<String, Object>, LogEntity> userHeartbeat(Map<String, Object> requestData) {
|
|
|
+ ServiceDto<Map<String, Object>, LogEntity> resultData = checkUserToken(requestData);
|
|
|
+ if (resultData.isSuccess()) {
|
|
|
+ resultData.setReturnData(R.success("0", null));
|
|
|
+ }
|
|
|
+ return resultData;
|
|
|
+ }
|
|
|
|
|
|
- if (Objects.nonNull(userloginlogs) && !userloginlogs.isEmpty()) {
|
|
|
- for (Userloginlog userloginlog : userloginlogs) {
|
|
|
|
|
|
- userloginlogService.updateLoginLogUserLastTimeById(userloginlog.getLoginid(), sessionId.get());
|
|
|
- resultData.setReturnData(R.success("0", "用户在线", null));
|
|
|
- resultData.setSuccess(true);
|
|
|
+ private String getValue(String key, Map<String, Object> data) {
|
|
|
+ return alias.getOrDefault(key, Collections.singletonList(key)).stream().map(data::get).filter(Objects::nonNull).map(Object::toString).findAny().orElse(null);
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
- if (!resultData.isSuccess()) {
|
|
|
- resultData.setReturnData(R.success("-1", "查询失败", null));
|
|
|
- resultData.setSuccess(false);
|
|
|
- }
|
|
|
+ private Rule parserRule(String rule) {
|
|
|
+ if (Objects.isNull(rule)) {
|
|
|
+ return new Rule(null, 0, 0);
|
|
|
+ }
|
|
|
+ String range = "";
|
|
|
+ if (rule.contains("N")) {
|
|
|
+ range += "0123456789";
|
|
|
+ }
|
|
|
+ if (rule.contains("U")) {
|
|
|
+ range += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
+ }
|
|
|
|
|
|
- } else {
|
|
|
- resultData.setReturnData(R.success("-1", "查询失败", null));
|
|
|
- resultData.setSuccess(false);
|
|
|
- }
|
|
|
- }
|
|
|
+ if (rule.contains("L") || (!rule.contains("U") && !rule.contains("L"))) {
|
|
|
+ range += "abcdefghijklmnopqrstuvwxyz";
|
|
|
}
|
|
|
- return resultData;
|
|
|
+
|
|
|
+ if (rule.contains("W")) {
|
|
|
+ range += "~`!@#$%^&*(),./<>?;:|";
|
|
|
+ }
|
|
|
+ String minStr = rule.substring(rule.length() - 4, rule.length() - 2);
|
|
|
+ String maxStr = rule.substring(rule.length() - 2);
|
|
|
+ return new Rule(range, Integer.parseInt(minStr), Integer.parseInt(maxStr));
|
|
|
}
|
|
|
|
|
|
+ private record Rule(String range, int min, int max) {
|
|
|
|
|
|
- private Optional<String> getValue(String key, Map<String, Object> data) {
|
|
|
- return alias.getOrDefault(key, Collections.singletonList(key)).stream().map(data::get).filter(Objects::nonNull).map(Object::toString).findAny();
|
|
|
}
|
|
|
}
|