Browse Source

1.模拟登录,添加分布式锁

dist.1.3.0
zhenghaoyu 11 months ago
parent
commit
ae5dd42e3a
  1. 45
      blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/JobAnnotationAspect.java
  2. 41
      blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/JobAnnotationAspect.java
  3. 41
      blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/JobAnnotationAspect.java
  4. 41
      blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/JobAnnotationAspect.java
  5. 6
      blade-service/logpm-factory/src/main/java/com/logpm/factory/config/InterceptorAdapterConfig.java
  6. 42
      blade-service/logpm-factory/src/main/java/com/logpm/factory/interceptor/LocalServerLoginAccountsInterceptor.java
  7. 41
      blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/JobAnnotationAspect.java
  8. 5
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/InterceptorAdapterConfig.java
  9. 49
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/RedissonConfig.java
  10. 39
      blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/interceptor/LocalServerLoginAccountsInterceptor.java

45
blade-service/logpm-aftersales/src/main/java/com/logpm/aftersales/aspect/JobAnnotationAspect.java

@ -8,12 +8,11 @@ import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springblade.common.annotations.LogpmAsync;
import org.springblade.common.cache.CacheNames; import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.LauncherConstant; import org.springblade.common.constant.LauncherConstant;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.secure.utils.AuthUtil; import org.springblade.core.redis.lock.LockType;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springblade.core.tool.utils.ThreadLocalUtil; import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -22,10 +21,10 @@ import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
import java.lang.reflect.Method;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Aspect @Aspect
@Component @Component
@ -33,6 +32,7 @@ import java.util.Objects;
public class JobAnnotationAspect { public class JobAnnotationAspect {
private final BladeRedis bladeRedis; private final BladeRedis bladeRedis;
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable {
@ -59,25 +59,32 @@ public class JobAnnotationAspect {
} }
private JSONObject mockLogin(){ private JSONObject mockLogin() throws InterruptedException {
String account ="shujutongbu"; String account ="shujutongbu";
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
if(Objects.isNull(data)){ if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS);
HttpRequest urlRequest = HttpRequest.post(url); if(flag){
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
urlRequest.header("Tenant-Id", "627683"); if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token";
Map<String, Object> params = new HashMap<>(); HttpRequest urlRequest = HttpRequest.post(url);
params.put("grant_type", "local_server"); urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
params.put("scope", "all"); urlRequest.header("Tenant-Id", "627683");
params.put("username", account);
params.put("tenantId", "627683"); Map<String, Object> params = new HashMap<>();
HttpResponse execute = urlRequest.form(params).execute(); params.put("grant_type", "local_server");
String body = execute.body(); params.put("scope", "all");
data = JSONObject.parseObject(body); params.put("username", account);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body();
data = JSONObject.parseObject(body);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,20L);
redisLockClient.unLock("local_server_user", LockType.FAIR);
}
}
} }
return data; return data;
} }

41
blade-service/logpm-basicdata/src/main/java/com/logpm/basicdata/aspect/JobAnnotationAspect.java

@ -11,6 +11,8 @@ import org.aspectj.lang.annotation.Aspect;
import org.springblade.common.cache.CacheNames; import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.LauncherConstant; import org.springblade.common.constant.LauncherConstant;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.redis.lock.LockType;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springblade.core.tool.utils.ThreadLocalUtil; import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -22,6 +24,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Aspect @Aspect
@Component @Component
@ -29,6 +32,7 @@ import java.util.Objects;
public class JobAnnotationAspect { public class JobAnnotationAspect {
private final BladeRedis bladeRedis; private final BladeRedis bladeRedis;
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable {
@ -55,25 +59,32 @@ public class JobAnnotationAspect {
} }
private JSONObject mockLogin(){ private JSONObject mockLogin() throws InterruptedException {
String account ="shujutongbu"; String account ="shujutongbu";
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
if(Objects.isNull(data)){ if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS);
HttpRequest urlRequest = HttpRequest.post(url); if(flag){
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
urlRequest.header("Tenant-Id", "627683"); if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token";
Map<String, Object> params = new HashMap<>(); HttpRequest urlRequest = HttpRequest.post(url);
params.put("grant_type", "local_server"); urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
params.put("scope", "all"); urlRequest.header("Tenant-Id", "627683");
params.put("username", account);
params.put("tenantId", "627683"); Map<String, Object> params = new HashMap<>();
HttpResponse execute = urlRequest.form(params).execute(); params.put("grant_type", "local_server");
String body = execute.body(); params.put("scope", "all");
data = JSONObject.parseObject(body); params.put("username", account);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body();
data = JSONObject.parseObject(body);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,20L);
redisLockClient.unLock("local_server_user", LockType.FAIR);
}
}
} }
return data; return data;
} }

41
blade-service/logpm-factory-data/logpm-factory-data-jinpai/src/main/java/com/logpm/factorydata/jinpai/aspect/JobAnnotationAspect.java

@ -11,6 +11,8 @@ import org.aspectj.lang.annotation.Aspect;
import org.springblade.common.cache.CacheNames; import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.LauncherConstant; import org.springblade.common.constant.LauncherConstant;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.redis.lock.LockType;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springblade.core.tool.utils.ThreadLocalUtil; import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -22,6 +24,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Aspect @Aspect
@Component @Component
@ -29,6 +32,7 @@ import java.util.Objects;
public class JobAnnotationAspect { public class JobAnnotationAspect {
private final BladeRedis bladeRedis; private final BladeRedis bladeRedis;
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable {
@ -55,25 +59,32 @@ public class JobAnnotationAspect {
} }
private JSONObject mockLogin(){ private JSONObject mockLogin() throws InterruptedException {
String account ="shujutongbu"; String account ="shujutongbu";
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
if(Objects.isNull(data)){ if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS);
HttpRequest urlRequest = HttpRequest.post(url); if(flag){
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
urlRequest.header("Tenant-Id", "627683"); if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token";
Map<String, Object> params = new HashMap<>(); HttpRequest urlRequest = HttpRequest.post(url);
params.put("grant_type", "local_server"); urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
params.put("scope", "all"); urlRequest.header("Tenant-Id", "627683");
params.put("username", account);
params.put("tenantId", "627683"); Map<String, Object> params = new HashMap<>();
HttpResponse execute = urlRequest.form(params).execute(); params.put("grant_type", "local_server");
String body = execute.body(); params.put("scope", "all");
data = JSONObject.parseObject(body); params.put("username", account);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body();
data = JSONObject.parseObject(body);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,20L);
redisLockClient.unLock("local_server_user", LockType.FAIR);
}
}
} }
return data; return data;
} }

41
blade-service/logpm-factory/src/main/java/com/logpm/factory/aspect/JobAnnotationAspect.java

@ -11,6 +11,8 @@ import org.aspectj.lang.annotation.Aspect;
import org.springblade.common.cache.CacheNames; import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.LauncherConstant; import org.springblade.common.constant.LauncherConstant;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.redis.lock.LockType;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springblade.core.tool.utils.ThreadLocalUtil; import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -22,6 +24,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Aspect @Aspect
@Component @Component
@ -29,6 +32,7 @@ import java.util.Objects;
public class JobAnnotationAspect { public class JobAnnotationAspect {
private final BladeRedis bladeRedis; private final BladeRedis bladeRedis;
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable {
@ -55,25 +59,32 @@ public class JobAnnotationAspect {
} }
private JSONObject mockLogin(){ private JSONObject mockLogin() throws InterruptedException {
String account ="shujutongbu"; String account ="shujutongbu";
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
if(Objects.isNull(data)){ if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS);
HttpRequest urlRequest = HttpRequest.post(url); if(flag){
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
urlRequest.header("Tenant-Id", "627683"); if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token";
Map<String, Object> params = new HashMap<>(); HttpRequest urlRequest = HttpRequest.post(url);
params.put("grant_type", "local_server"); urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
params.put("scope", "all"); urlRequest.header("Tenant-Id", "627683");
params.put("username", account);
params.put("tenantId", "627683"); Map<String, Object> params = new HashMap<>();
HttpResponse execute = urlRequest.form(params).execute(); params.put("grant_type", "local_server");
String body = execute.body(); params.put("scope", "all");
data = JSONObject.parseObject(body); params.put("username", account);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body();
data = JSONObject.parseObject(body);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,20L);
redisLockClient.unLock("local_server_user", LockType.FAIR);
}
}
} }
return data; return data;
} }

6
blade-service/logpm-factory/src/main/java/com/logpm/factory/config/InterceptorAdapterConfig.java

@ -7,9 +7,9 @@ import com.logpm.factory.interceptor.ZbFactoryAccountsInterceptor;
import com.logpm.factory.props.ZbFactoryProperties; import com.logpm.factory.props.ZbFactoryProperties;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@ -25,6 +25,8 @@ public class InterceptorAdapterConfig implements WebMvcConfigurer {
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Override @Override
public void addInterceptors(InterceptorRegistry interceptorRegistry) { public void addInterceptors(InterceptorRegistry interceptorRegistry) {
@ -36,7 +38,7 @@ public class InterceptorAdapterConfig implements WebMvcConfigurer {
.excludePathPatterns("/factory/common/**").order(2); .excludePathPatterns("/factory/common/**").order(2);
interceptorRegistry.addInterceptor(new ZbFactoryAccountsInterceptor(zbFactoryProperties)) interceptorRegistry.addInterceptor(new ZbFactoryAccountsInterceptor(zbFactoryProperties))
.addPathPatterns("/factory/zb/**").order(3); .addPathPatterns("/factory/zb/**").order(3);
interceptorRegistry.addInterceptor(new LocalServerLoginAccountsInterceptor(redis,environment)).addPathPatterns("/**").order(1); interceptorRegistry.addInterceptor(new LocalServerLoginAccountsInterceptor(redis,environment,redisLockClient)).addPathPatterns("/**").order(1);
} }
} }

42
blade-service/logpm-factory/src/main/java/com/logpm/factory/interceptor/LocalServerLoginAccountsInterceptor.java

@ -3,13 +3,15 @@ package com.logpm.factory.interceptor;
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse; import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import org.springblade.common.wrapper.CustomHttpServletRequestWrapper;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springblade.common.cache.CacheNames; import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.LauncherConstant; import org.springblade.common.constant.LauncherConstant;
import org.springblade.common.exception.CustomerException; import org.springblade.common.exception.CustomerException;
import org.springblade.common.wrapper.CustomHttpServletRequestWrapper;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.redis.lock.LockType;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.ThreadLocalUtil; import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -25,6 +27,7 @@ import java.io.PrintWriter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Log4j2 @Log4j2
@AllArgsConstructor @AllArgsConstructor
@ -32,6 +35,8 @@ public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapt
private final BladeRedis bladeRedis; private final BladeRedis bladeRedis;
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws CustomerException { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws CustomerException {
@ -41,20 +46,27 @@ public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapt
String account ="shujutongbu"; String account ="shujutongbu";
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
if(Objects.isNull(data)){ if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS);
cn.hutool.http.HttpRequest urlRequest = HttpRequest.post(url); if(flag){
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
urlRequest.header("Tenant-Id", "627683"); if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token";
Map<String, Object> params = new HashMap<>(); HttpRequest urlRequest = HttpRequest.post(url);
params.put("grant_type", "local_server"); urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
params.put("scope", "all"); urlRequest.header("Tenant-Id", "627683");
params.put("username", account);
params.put("tenantId", "627683"); Map<String, Object> params = new HashMap<>();
HttpResponse execute = urlRequest.form(params).execute(); params.put("grant_type", "local_server");
String body = execute.body(); params.put("scope", "all");
data = JSONObject.parseObject(body); params.put("username", account);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body();
data = JSONObject.parseObject(body);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,20L);
redisLockClient.unLock("local_server_user", LockType.FAIR);
}
}
} }
// 修改或添加header // 修改或添加header

41
blade-service/logpm-patch/src/main/java/com/logpm/patch/aspect/JobAnnotationAspect.java

@ -11,6 +11,8 @@ import org.aspectj.lang.annotation.Aspect;
import org.springblade.common.cache.CacheNames; import org.springblade.common.cache.CacheNames;
import org.springblade.common.constant.LauncherConstant; import org.springblade.common.constant.LauncherConstant;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.redis.lock.LockType;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springblade.core.tool.utils.ThreadLocalUtil; import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
@ -22,6 +24,7 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Aspect @Aspect
@Component @Component
@ -29,6 +32,7 @@ import java.util.Objects;
public class JobAnnotationAspect { public class JobAnnotationAspect {
private final BladeRedis bladeRedis; private final BladeRedis bladeRedis;
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") @Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)")
public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable {
@ -55,25 +59,32 @@ public class JobAnnotationAspect {
} }
private JSONObject mockLogin(){ private JSONObject mockLogin() throws InterruptedException {
String account ="shujutongbu"; String account ="shujutongbu";
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
if(Objects.isNull(data)){ if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS);
HttpRequest urlRequest = HttpRequest.post(url); if(flag){
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
urlRequest.header("Tenant-Id", "627683"); if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token";
Map<String, Object> params = new HashMap<>(); HttpRequest urlRequest = HttpRequest.post(url);
params.put("grant_type", "local_server"); urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
params.put("scope", "all"); urlRequest.header("Tenant-Id", "627683");
params.put("username", account);
params.put("tenantId", "627683"); Map<String, Object> params = new HashMap<>();
HttpResponse execute = urlRequest.form(params).execute(); params.put("grant_type", "local_server");
String body = execute.body(); params.put("scope", "all");
data = JSONObject.parseObject(body); params.put("username", account);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body();
data = JSONObject.parseObject(body);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,20L);
redisLockClient.unLock("local_server_user", LockType.FAIR);
}
}
} }
return data; return data;
} }

5
blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/InterceptorAdapterConfig.java

@ -3,6 +3,7 @@ package com.logpm.warehouse.config;
import com.logpm.warehouse.config.interceptor.LocalServerLoginAccountsInterceptor; import com.logpm.warehouse.config.interceptor.LocalServerLoginAccountsInterceptor;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@ -16,9 +17,11 @@ public class InterceptorAdapterConfig implements WebMvcConfigurer {
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Override @Override
public void addInterceptors(InterceptorRegistry interceptorRegistry) { public void addInterceptors(InterceptorRegistry interceptorRegistry) {
interceptorRegistry.addInterceptor(new LocalServerLoginAccountsInterceptor(redis,environment)).addPathPatterns("/oldpush/**").order(1); interceptorRegistry.addInterceptor(new LocalServerLoginAccountsInterceptor(redis,environment,redisLockClient)).addPathPatterns("/oldpush/**").order(1);
} }
} }

49
blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/RedissonConfig.java

@ -1,35 +1,26 @@
package com.logpm.warehouse.config; package com.logpm.warehouse.config;
import org.redisson.Redisson; //@Configuration
import org.redisson.config.Config;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.tool.utils.SpringUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
@Configuration
public class RedissonConfig { public class RedissonConfig {
@Bean // @Bean
public Redisson redisson() { // public Redisson redisson() {
// 单机模式 // // 单机模式
Config config = new Config(); // Config config = new Config();
//
BladeRedis bean = SpringUtil.getBean(BladeRedis.class); // BladeRedis bean = SpringUtil.getBean(BladeRedis.class);
//
RedisConnectionFactory connectionFactory = bean.getRedisTemplate().getConnectionFactory(); // RedisConnectionFactory connectionFactory = bean.getRedisTemplate().getConnectionFactory();
//
LettuceConnectionFactory factory = (LettuceConnectionFactory) connectionFactory; // LettuceConnectionFactory factory = (LettuceConnectionFactory) connectionFactory;
//
String hostName = factory.getHostName(); // String hostName = factory.getHostName();
int port = factory.getPort(); // int port = factory.getPort();
String password = factory.getPassword(); // String password = factory.getPassword();
//
config.useSingleServer().setAddress("redis://"+hostName+":"+port).setDatabase(2); // config.useSingleServer().setAddress("redis://"+hostName+":"+port).setDatabase(2);
config.useSingleServer().setPassword(password); // config.useSingleServer().setPassword(password);
return (Redisson) Redisson.create(config); // return (Redisson) Redisson.create(config);
} // }
} }

39
blade-service/logpm-warehouse/src/main/java/com/logpm/warehouse/config/interceptor/LocalServerLoginAccountsInterceptor.java

@ -10,6 +10,8 @@ import org.springblade.common.constant.LauncherConstant;
import org.springblade.common.exception.CustomerException; import org.springblade.common.exception.CustomerException;
import org.springblade.common.wrapper.CustomHttpServletRequestWrapper; import org.springblade.common.wrapper.CustomHttpServletRequestWrapper;
import org.springblade.core.redis.cache.BladeRedis; import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.core.redis.lock.LockType;
import org.springblade.core.redis.lock.RedisLockClient;
import org.springblade.core.tool.api.R; import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.ThreadLocalUtil; import org.springblade.core.tool.utils.ThreadLocalUtil;
import org.springframework.core.env.Environment; import org.springframework.core.env.Environment;
@ -25,6 +27,7 @@ import java.io.PrintWriter;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit;
@Log4j2 @Log4j2
@AllArgsConstructor @AllArgsConstructor
@ -32,6 +35,7 @@ public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapt
private final BladeRedis bladeRedis; private final BladeRedis bladeRedis;
private final Environment environment; private final Environment environment;
private final RedisLockClient redisLockClient;
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws CustomerException { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws CustomerException {
@ -41,20 +45,27 @@ public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapt
String account ="shujutongbu"; String account ="shujutongbu";
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account); JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
if(Objects.isNull(data)){ if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token"; boolean flag = redisLockClient.tryLock("local_server_user", LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS);
HttpRequest urlRequest = HttpRequest.post(url); if(flag){
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+account);
urlRequest.header("Tenant-Id", "627683"); if(Objects.isNull(data)){
String url = "http://"+ LauncherConstant.loginAddr(Objects.requireNonNull(environment.getActiveProfiles()[0]))+"/blade-auth/oauth/token";
Map<String, Object> params = new HashMap<>(); HttpRequest urlRequest = HttpRequest.post(url);
params.put("grant_type", "local_server"); urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0");
params.put("scope", "all"); urlRequest.header("Tenant-Id", "627683");
params.put("username", account);
params.put("tenantId", "627683"); Map<String, Object> params = new HashMap<>();
HttpResponse execute = urlRequest.form(params).execute(); params.put("grant_type", "local_server");
String body = execute.body(); params.put("scope", "all");
data = JSONObject.parseObject(body); params.put("username", account);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,3200L); params.put("tenantId", "627683");
HttpResponse execute = urlRequest.form(params).execute();
String body = execute.body();
data = JSONObject.parseObject(body);
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+account,data,20L);
redisLockClient.unLock("local_server_user", LockType.FAIR);
}
}
} }
// 修改或添加header // 修改或添加header

Loading…
Cancel
Save