Edd*_*die 8 java node.js docker spring-boot docker-compose
我想以这样一种方式配置我的spring拦截器,每次请求都应该调用它.
我在Interceptor中面临一个问题.我想以这样的方式配置它:每次请求都应该调用它preHandle()
并执行我在其中编写的操作.
我注意到一个问题,我想在这里提一下.
如果我正在调用的服务已停止(未运行),Interceptor正在正常工作并给我一个响应,如somename-service not found.如果此时正在运行相同的服务,则不执行Interceptor.
这是我的代码片段
@EnableEurekaClient
@SpringBootApplication
@EnableZuulProxy
@Configuration
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Autowired
private TokenValidateInterceptor tokenValidateInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(tokenValidateInterceptor).addPathPatterns("/**");
}
Run Code Online (Sandbox Code Playgroud)
拦截器
@Component
public class TokenValidateInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
LOG.info("#### Starting TokenValidateInterceptor.preHandle ####");
String apiKey = null;
try {
apiKey = request.getHeader("apikey");
LOG.info("The request come with apikey ======" + apiKey);
LOG.info("Actual apikey ======" + azureApikey);
}
Run Code Online (Sandbox Code Playgroud)
您必须将此拦截器添加到您的调度程序 xml 文件中:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" p:interceptors-ref="tokenInterceptor" />
<bean id="tokenInterceptor" class="yourpackage.TokenValidateInterceptor" />
</beans>
Run Code Online (Sandbox Code Playgroud)
这里有一些不错的示例:
归档时间: |
|
查看次数: |
1649 次 |
最近记录: |