Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
scrtsdk
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
胡懿
scrtsdk
Commits
23ff62c5
Commit
23ff62c5
authored
Apr 14, 2025
by
胡懿
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复地址问题
parent
c4d8de4b
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
154 additions
and
0 deletions
+154
-0
pom.xml
pom.xml
+15
-0
TokenAndUrlAspect.java
src/main/java/com/yzkj/scrtsdk/common/TokenAndUrlAspect.java
+127
-0
WithTokenAndUrl.java
src/main/java/com/yzkj/scrtsdk/common/WithTokenAndUrl.java
+9
-0
LoginScrtController.java
...java/com/yzkj/scrtsdk/controller/LoginScrtController.java
+3
-0
No files found.
pom.xml
View file @
23ff62c5
...
...
@@ -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>
...
...
src/main/java/com/yzkj/scrtsdk/common/TokenAndUrlAspect.java
0 → 100644
View file @
23ff62c5
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
);
}
}
src/main/java/com/yzkj/scrtsdk/common/WithTokenAndUrl.java
0 → 100644
View file @
23ff62c5
package
com
.
yzkj
.
scrtsdk
.
common
;
import
java.lang.annotation.*
;
@Target
(
ElementType
.
TYPE
)
// 标注在类上
@Retention
(
RetentionPolicy
.
RUNTIME
)
// 运行时生效
@Documented
public
@interface
WithTokenAndUrl
{
}
src/main/java/com/yzkj/scrtsdk/controller/LoginScrtController.java
View file @
23ff62c5
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
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment