Commit 23ff62c5 by 胡懿

修复地址问题

parent c4d8de4b
......@@ -43,6 +43,21 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
......
package com.yzkj.scrtsdk.common;
import ch.qos.logback.core.util.StringUtil;
import jakarta.servlet.http.HttpServletResponse;
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.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import jakarta.servlet.http.HttpServletRequest; // 注意包名变化
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.thymeleaf.util.StringUtils;
@Aspect
@Component
public class TokenAndUrlAspect {
@Around("@within(WithTokenAndUrl)")
public Object captureControllerAndMethodPath(ProceedingJoinPoint joinPoint) throws Throwable {
// 获取当前请求对象
HttpServletRequest request = getCurrentRequest();
// 1. 提取 Controller 类上的路径
Class<?> controllerClass = joinPoint.getTarget().getClass();
String controllerPath = getControllerPath(controllerClass);
// 2. 提取接口方法上的路径
Method method = getCurrentMethod(joinPoint);
String methodPath = getMethodPath(method);
// 3. 组合完整路径(例如:/api/v1/user/login)
String fullPath = combinePaths(controllerPath, methodPath);
if (!fullPath.equals("/loginScrt/jumpLogin")) {
// 4. 获取 Token(示例:从请求头获取)
String token = request.getHeader("Authorization");
System.out.println("Token: " + token);
if (StringUtils.isEmpty(token)) {
return "scrtLogin"; // 终止后续执行,不再调用原 Controller 方法
}
}
// 5. 打印或处理路径信息(此处仅为演示)
System.out.println("Controller 路径: " + controllerPath);
System.out.println("接口方法路径: " + methodPath);
System.out.println("完整路径: " + fullPath);
// 继续执行原方法
return joinPoint.proceed();
}
// 获取当前请求对象
private HttpServletRequest getCurrentRequest() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
}
private HttpServletResponse getCurrentResponse() {
return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
}
// 获取当前执行的方法
private Method getCurrentMethod(ProceedingJoinPoint joinPoint) throws NoSuchMethodException {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
return joinPoint.getTarget().getClass().getMethod(
methodSignature.getName(),
methodSignature.getParameterTypes()
);
}
// 提取 Controller 类的路径
private String getControllerPath(Class<?> controllerClass) {
RequestMapping classMapping = AnnotationUtils.findAnnotation(controllerClass, RequestMapping.class);
if (classMapping != null && classMapping.value().length > 0) {
return normalizePath(classMapping.value()[0]);
}
return "";
}
// 提取接口方法的路径
private String getMethodPath(Method method) {
// 优先级:具体方法注解 > @RequestMapping
RequestMapping reqMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class);
String path = "";
// 检查所有可能的注解
for (Class<? extends Annotation> annotationType : Arrays.asList(
GetMapping.class,
PostMapping.class,
PutMapping.class,
DeleteMapping.class,
PatchMapping.class,
RequestMapping.class
)) {
Annotation annotation = AnnotationUtils.findAnnotation(method, annotationType);
if (annotation != null) {
String[] values = (String[]) AnnotationUtils.getValue(annotation, "value");
if (values != null && values.length > 0 && !values[0].isEmpty()) {
path = values[0];
break;
}
}
}
return normalizePath(path);
}
// 规范化路径(确保以 / 开头且不重复)
private String normalizePath(String path) {
if (path == null || path.isEmpty()) return "";
if (!path.startsWith("/")) {
path = "/" + path;
}
return path.replaceAll("/{2,}", "/");
}
// 组合 Controller 和方法的路径
private String combinePaths(String controllerPath, String methodPath) {
return normalizePath(controllerPath + methodPath);
}
}
package com.yzkj.scrtsdk.common;
import java.lang.annotation.*;
@Target(ElementType.TYPE) // 标注在类上
@Retention(RetentionPolicy.RUNTIME) // 运行时生效
@Documented
public @interface WithTokenAndUrl {
}
package com.yzkj.scrtsdk.controller;
import com.yzkj.scrtsdk.common.WithTokenAndUrl;
import com.yzkj.scrtsdk.utils.CallbackUrlUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
......@@ -9,10 +10,12 @@ import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
@WithTokenAndUrl
@Controller
@RequestMapping("/loginScrt")
public class LoginScrtController {
@GetMapping("/jumpLogin")
public String jumpLogin(Model model) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment