pref_mail@163.com
9 months ago
14 changed files with 288 additions and 11 deletions
@ -0,0 +1,22 @@
|
||||
package org.springblade.common.annotations; |
||||
|
||||
import java.lang.annotation.ElementType; |
||||
import java.lang.annotation.Retention; |
||||
import java.lang.annotation.RetentionPolicy; |
||||
import java.lang.annotation.Target; |
||||
|
||||
/** |
||||
* 特俗标记 |
||||
*/ |
||||
@Retention(RetentionPolicy.RUNTIME) |
||||
@Target({ ElementType.METHOD}) |
||||
public @interface LocalServerUser { |
||||
/**‘ |
||||
* 匹配值 |
||||
* factory:工厂系统 |
||||
* 6278683: 租户 |
||||
* factory-data: 租户 |
||||
* @return |
||||
*/ |
||||
String ds() default ""; |
||||
} |
@ -0,0 +1,59 @@
|
||||
//package com.logpm.factory.aspect;
|
||||
//
|
||||
//import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
||||
//import org.aspectj.lang.ProceedingJoinPoint;
|
||||
//import org.aspectj.lang.annotation.Around;
|
||||
//import org.aspectj.lang.annotation.Aspect;
|
||||
//import org.aspectj.lang.reflect.MethodSignature;
|
||||
//import org.springblade.common.annotations.LocalServerUser;
|
||||
//import org.springblade.core.secure.BladeUser;
|
||||
//import org.springblade.core.secure.utils.AuthUtil;
|
||||
//import org.springblade.core.tool.utils.StringUtil;
|
||||
//import org.springblade.core.tool.utils.WebUtil;
|
||||
//import org.springblade.system.cache.UserCache;
|
||||
//import org.springblade.system.entity.User;
|
||||
//import org.springframework.core.annotation.Order;
|
||||
//import org.springframework.scheduling.annotation.Async;
|
||||
//import org.springframework.stereotype.Component;
|
||||
//
|
||||
//import javax.servlet.http.HttpServletRequest;
|
||||
//import java.lang.reflect.Method;
|
||||
//
|
||||
//@Aspect
|
||||
//@Component
|
||||
//public class LocalServerTenantIdAnnotationAspect {
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 定义一个切点,匹配所有带有@Async("asyncExecutor")注解的方法。
|
||||
// * 注意:实际上Spring Framework自带对@Async("asyncExecutor")的处理,直接这样配置可能会导致预期之外的行为。
|
||||
// */
|
||||
//
|
||||
// // 定义一个
|
||||
// @Around("@annotation(org.springblade.common.annotations.LocalServerUser)")
|
||||
// public Object logAroundAsyncMethods(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
//
|
||||
// MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
// Method method = signature.getMethod();
|
||||
//
|
||||
// LocalServerUser myAsync = method.getAnnotation(LocalServerUser.class);
|
||||
// String annotationValue = myAsync.ds();
|
||||
//
|
||||
// if(StringUtil.isNotBlank(annotationValue) ){
|
||||
// // 在方法执行前的操作
|
||||
// DynamicDataSourceContextHolder.push(annotationValue);
|
||||
//
|
||||
// // 执行原方法
|
||||
// Object result = joinPoint.proceed();
|
||||
//
|
||||
// // 在方法执行后的操作
|
||||
// DynamicDataSourceContextHolder.poll();
|
||||
//
|
||||
// return result;
|
||||
// }else{
|
||||
// return joinPoint.proceed();
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
//}
|
@ -0,0 +1,100 @@
|
||||
package com.logpm.factory.interceptor; |
||||
|
||||
import cn.hutool.core.map.MapUtil; |
||||
import cn.hutool.http.HttpRequest; |
||||
import cn.hutool.http.HttpResponse; |
||||
import cn.hutool.http.HttpUtil; |
||||
import com.alibaba.fastjson.JSONObject; |
||||
import com.logpm.factory.comfac.service.IFactoryTokenService; |
||||
import com.logpm.factory.config.RequestWrapper; |
||||
import com.logpm.factory.wrapper.CustomHttpServletRequestWrapper; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.extern.log4j.Log4j2; |
||||
import org.springblade.common.cache.CacheNames; |
||||
import org.springblade.common.constant.LauncherConstant; |
||||
import org.springblade.common.exception.CustomerException; |
||||
import org.springblade.core.launch.constant.AppConstant; |
||||
import org.springblade.core.redis.cache.BladeRedis; |
||||
import org.springblade.core.secure.utils.AuthUtil; |
||||
import org.springblade.core.tool.api.R; |
||||
import org.springblade.core.tool.utils.StringUtil; |
||||
import org.springframework.web.context.request.RequestContextHolder; |
||||
import org.springframework.web.context.request.ServletRequestAttributes; |
||||
import org.springframework.web.servlet.HandlerInterceptor; |
||||
import org.springframework.web.servlet.ModelAndView; |
||||
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.HashMap; |
||||
import java.util.Map; |
||||
import java.util.Objects; |
||||
|
||||
@Log4j2 |
||||
@AllArgsConstructor |
||||
public class LocalServerLoginAccountsInterceptor extends HandlerInterceptorAdapter { |
||||
|
||||
private final BladeRedis bladeRedis; |
||||
|
||||
@Override |
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws CustomerException { |
||||
|
||||
|
||||
try { |
||||
JSONObject data =bladeRedis.get(CacheNames.LOCAL_SERVER_USER+"admin"); |
||||
CustomHttpServletRequestWrapper wrappedRequest = new CustomHttpServletRequestWrapper(request); |
||||
if(Objects.isNull(data)){ |
||||
String url = "http://"+ LauncherConstant.loginAddr(AppConstant.DEV_CODE)+"/blade-auth/oauth/token"; |
||||
cn.hutool.http.HttpRequest urlRequest = HttpRequest.post(url); |
||||
urlRequest.header("Authorization", "Basic c2FiZXI6c2FiZXJfc2VjcmV0"); |
||||
urlRequest.header("Tenant-Id", "627683"); |
||||
|
||||
Map<String, Object> params = new HashMap<>(); |
||||
params.put("grant_type", "local_server"); |
||||
params.put("scope", "all"); |
||||
params.put("username", "admin"); |
||||
params.put("tenantId", "627683"); |
||||
HttpResponse execute = urlRequest.form(params).execute(); |
||||
String body = execute.body(); |
||||
data = JSONObject.parseObject(body); |
||||
bladeRedis.setEx(CacheNames.LOCAL_SERVER_USER+"admin",data,3200L); |
||||
} |
||||
|
||||
// 修改或添加header
|
||||
|
||||
// 发送登录请求
|
||||
log.info("##########preHandle: request={}", request); |
||||
|
||||
wrappedRequest.addHeader("Blade-Auth", "bearer "+data.getString("access_token")); |
||||
|
||||
// 用包装后的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(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,36 @@
|
||||
package com.logpm.factory.wrapper; |
||||
|
||||
import org.springframework.http.HttpHeaders; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import javax.servlet.http.HttpServletRequestWrapper; |
||||
import java.util.*; |
||||
|
||||
public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper { |
||||
|
||||
private final HttpHeaders headers; |
||||
|
||||
public CustomHttpServletRequestWrapper(HttpServletRequest request) { |
||||
super(request); |
||||
headers = new HttpHeaders(); |
||||
} |
||||
|
||||
@Override |
||||
public String getHeader(String name) { |
||||
String headerValue = headers.getFirst(name); |
||||
return headerValue != null ? headerValue : super.getHeader(name); |
||||
} |
||||
|
||||
@Override |
||||
public Enumeration<String> getHeaderNames() { |
||||
List<String> names = Collections.list(super.getHeaderNames()); |
||||
headers.forEach((key, value) -> names.add(key)); |
||||
return Collections.enumeration(names); |
||||
} |
||||
|
||||
// 其他需要覆盖的方法...
|
||||
|
||||
public void addHeader(String name, String value) { |
||||
headers.add(name, value); |
||||
} |
||||
} |
Loading…
Reference in new issue