pref_mail@163.com
4 days ago
16 changed files with 343 additions and 37 deletions
@ -0,0 +1,79 @@
|
||||
package com.logpm.report.aspect; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder; |
||||
import lombok.AllArgsConstructor; |
||||
import org.aspectj.lang.ProceedingJoinPoint; |
||||
import org.aspectj.lang.annotation.Around; |
||||
import org.aspectj.lang.annotation.Aspect; |
||||
import org.springblade.common.cache.CacheNames; |
||||
import org.springblade.common.component.MockLoginService; |
||||
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.springframework.core.env.Environment; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.mock.web.MockHttpServletRequest; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.web.context.request.RequestContextHolder; |
||||
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
|
||||
import java.util.Objects; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
@Aspect |
||||
@Component |
||||
@AllArgsConstructor |
||||
public class JobAnnotationAspect { |
||||
private final BladeRedis bladeRedis; |
||||
private final Environment environment; |
||||
private final RedisLockClient redisLockClient; |
||||
private final MockLoginService mockLoginService; |
||||
@Around("@annotation(com.xxl.job.core.handler.annotation.XxlJob)") |
||||
public Object xxlJobAnnotationMethods(ProceedingJoinPoint joinPoint) throws Throwable { |
||||
|
||||
|
||||
JSONObject jsonObject = mockLogin(); |
||||
MockHttpServletRequest mockRequest = new MockHttpServletRequest(); |
||||
mockRequest.addHeader("Blade-Auth", "bearer "+jsonObject.get("access_token")); |
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(mockRequest)); |
||||
|
||||
HttpHeaders httpHeaders = new HttpHeaders(); |
||||
httpHeaders.add("Blade-Auth","bearer "+jsonObject.get("access_token") ); |
||||
httpHeaders.add( "Authorization", "Basic bG9jYWw6bG9jYWxfc2VjcmV0"); |
||||
ThreadLocalUtil.put("bladeContext", httpHeaders); |
||||
|
||||
DynamicDataSourceContextHolder.push(jsonObject.getString("tenant_id")); |
||||
// 执行原方法
|
||||
Object result = joinPoint.proceed(); |
||||
// 在方法执行后,从数据源上下文中移除租户ID
|
||||
DynamicDataSourceContextHolder.poll(); |
||||
|
||||
return result; |
||||
|
||||
|
||||
} |
||||
|
||||
private JSONObject mockLogin() throws InterruptedException { |
||||
String account ="shujutongbu"; |
||||
String tenantId ="627683"; |
||||
String key =CacheNames.LOCAL_SERVER_USER+tenantId+":"+account; |
||||
String lockKey =key+":lock"; |
||||
JSONObject data =bladeRedis.get(key); |
||||
if(Objects.isNull(data)){ |
||||
boolean flag = redisLockClient.tryLock(lockKey, LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS); |
||||
if(flag){ |
||||
data =bladeRedis.get(key); |
||||
if(Objects.isNull(data)){ |
||||
data = mockLoginService.mockToken(tenantId,account); |
||||
bladeRedis.setEx(key,data,2591990L); |
||||
redisLockClient.unLock(lockKey, LockType.FAIR); |
||||
} |
||||
} |
||||
} |
||||
return data; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,33 @@
|
||||
package com.logpm.report.config; |
||||
|
||||
|
||||
import com.logpm.report.interceptor.LocalServerLoginAccountsInterceptor; |
||||
import lombok.AllArgsConstructor; |
||||
import org.springblade.common.component.MockLoginService; |
||||
import org.springblade.core.redis.cache.BladeRedis; |
||||
import org.springblade.core.redis.lock.RedisLockClient; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; |
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
||||
|
||||
/** |
||||
* 为所有的的 |
||||
*/ |
||||
@Configuration |
||||
@AllArgsConstructor |
||||
public class InterceptorAdapterConfig implements WebMvcConfigurer { |
||||
|
||||
|
||||
private final BladeRedis redis; |
||||
|
||||
private final RedisLockClient redisLockClient; |
||||
private final MockLoginService mockLoginService; |
||||
|
||||
|
||||
@Override |
||||
public void addInterceptors(InterceptorRegistry interceptorRegistry) { |
||||
|
||||
interceptorRegistry.addInterceptor(new LocalServerLoginAccountsInterceptor(redis,redisLockClient,mockLoginService)).addPathPatterns("/largeScreen/**").order(1); |
||||
|
||||
} |
||||
} |
@ -0,0 +1,74 @@
|
||||
package com.logpm.report.config; |
||||
|
||||
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
|
||||
/** |
||||
* xxl-job config |
||||
* |
||||
* @author xuxueli 2017-04-28 |
||||
*/ |
||||
@Configuration(proxyBeanMethods = false) |
||||
public class XxlJobConfig { |
||||
private final Logger logger = LoggerFactory.getLogger(XxlJobConfig.class); |
||||
|
||||
@Value("${xxl.job.admin.addresses}") |
||||
private String adminAddresses; |
||||
|
||||
@Value("${xxl.job.executor.appname}") |
||||
private String appName; |
||||
|
||||
@Value("${xxl.job.executor.ip}") |
||||
private String ip; |
||||
|
||||
@Value("${xxl.job.executor.port}") |
||||
private int port; |
||||
|
||||
@Value("${xxl.job.accessToken}") |
||||
private String accessToken; |
||||
|
||||
@Value("${xxl.job.executor.logpath}") |
||||
private String logPath; |
||||
|
||||
@Value("${xxl.job.executor.logretentiondays}") |
||||
private int logRetentionDays; |
||||
|
||||
|
||||
@Bean |
||||
public XxlJobSpringExecutor xxlJobExecutor() { |
||||
logger.info(">>>>>>>>>>> xxl-job config init."); |
||||
XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); |
||||
xxlJobSpringExecutor.setAdminAddresses(adminAddresses); |
||||
xxlJobSpringExecutor.setAppName(appName); |
||||
xxlJobSpringExecutor.setIp(ip); |
||||
xxlJobSpringExecutor.setPort(port); |
||||
xxlJobSpringExecutor.setAccessToken(accessToken); |
||||
xxlJobSpringExecutor.setLogPath(logPath); |
||||
xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); |
||||
|
||||
return xxlJobSpringExecutor; |
||||
} |
||||
|
||||
/** |
||||
* 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP; |
||||
* |
||||
* 1、引入依赖: |
||||
* <dependency> |
||||
* <groupId>org.springframework.cloud</groupId> |
||||
* <artifactId>spring-cloud-commons</artifactId> |
||||
* <version>${version}</version> |
||||
* </dependency> |
||||
* |
||||
* 2、配置文件,或者容器启动变量 |
||||
* spring.cloud.inetutils.preferred-networks: 'xxx.xxx.xxx.' |
||||
* |
||||
* 3、获取IP |
||||
* String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); |
||||
*/ |
||||
|
||||
|
||||
} |
@ -0,0 +1,100 @@
|
||||
package com.logpm.report.interceptor; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.log4j.Log4j2; |
||||
import org.springblade.common.cache.CacheNames; |
||||
import org.springblade.common.component.MockLoginService; |
||||
import org.springblade.common.exception.CustomerException; |
||||
import org.springblade.common.wrapper.CustomHttpServletRequestWrapper; |
||||
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.utils.StringUtil; |
||||
import org.springblade.core.tool.utils.ThreadLocalUtil; |
||||
import org.springframework.http.HttpHeaders; |
||||
import org.springframework.web.context.request.RequestContextHolder; |
||||
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletResponse; |
||||
import java.io.IOException; |
||||
import java.io.PrintWriter; |
||||
import java.util.Objects; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
@Log4j2 |
||||
@AllArgsConstructor |
||||
public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapter { |
||||
|
||||
private final BladeRedis bladeRedis; |
||||
private final RedisLockClient redisLockClient; |
||||
private final MockLoginService mockLoginService; |
||||
|
||||
@Override |
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws CustomerException { |
||||
|
||||
|
||||
try { |
||||
CustomHttpServletRequestWrapper wrappedRequest = new CustomHttpServletRequestWrapper(request); |
||||
String account ="shujutongbu"; |
||||
String tenantId = request.getHeader("tenantId"); |
||||
tenantId = StringUtil.isBlank(tenantId)?"627683":tenantId; |
||||
String key =CacheNames.LOCAL_SERVER_USER+tenantId+":"+account; |
||||
String lockKey =key+":lock"; |
||||
JSONObject data =bladeRedis.get(key); |
||||
if(Objects.isNull(data)){ |
||||
boolean flag = redisLockClient.tryLock(lockKey, LockType.FAIR, 5000, 10000, TimeUnit.MILLISECONDS); |
||||
if(flag){ |
||||
data =bladeRedis.get(key); |
||||
if(Objects.isNull(data)){ |
||||
data = mockLoginService.mockToken(tenantId,account); |
||||
bladeRedis.setEx(key,data,2591990L); |
||||
redisLockClient.unLock(lockKey, LockType.FAIR); |
||||
} |
||||
} |
||||
} |
||||
|
||||
// 修改或添加header
|
||||
|
||||
// 发送登录请求
|
||||
log.info("##########preHandle: request={}", request); |
||||
|
||||
wrappedRequest.addHeader("Blade-Auth", "bearer "+data.getString("access_token")); |
||||
HttpHeaders httpHeaders = new HttpHeaders(); |
||||
httpHeaders.add("Blade-Auth","bearer "+data.get("access_token") ); |
||||
httpHeaders.add( "Authorization", "Basic bG9jYWw6bG9jYWxfc2VjcmV0"); |
||||
ThreadLocalUtil.put("bladeContext", httpHeaders); |
||||
|
||||
|
||||
// 用包装后的request替换原始request
|
||||
request = wrappedRequest; |
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request)); |
||||
return true; |
||||
} catch (Exception e) { |
||||
returnJson(response, JSONObject.toJSONString(R.fail(500, "服务异常,请联系管理员"))); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
private void returnJson(HttpServletResponse response, String json) { |
||||
PrintWriter writer = null; |
||||
response.setCharacterEncoding("UTF-8"); |
||||
response.setContentType("application/json"); |
||||
try { |
||||
writer = response.getWriter(); |
||||
writer.print(json); |
||||
|
||||
} catch (IOException e) { |
||||
System.out.println(e.getMessage()); |
||||
} finally { |
||||
if (writer != null) { |
||||
writer.close(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
Loading…
Reference in new issue