拦截器代码
package com.centaline.employpay.interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class WebAppConfig extends WebMvcConfigurationSupport {
@Bean
public HandlerInterceptor getPcInterceptor() {
return new PCLoginInterceptor();
}
@Bean
public HandlerInterceptor getMyInterceptor() {
return new WeChatLoginInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(getPcInterceptor()).addPathPatterns("/api/**").excludePathPatterns("/static/**");
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/lan-jie/**").excludePathPatterns("/static/**").excludePathPatterns("/not-lan-jie");
super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/", "classpath:/");
}
}
页面上静态资源访问需要加上项目名
<link href="/employpay/static/css/style.css" rel="stylesheet">
<link href="/employpay/static/css/c-style.css" rel="stylesheet">
<link href="/employpay/static/css/style-responsive.css" rel="stylesheet">
<link href="/employpay/static/css/new_style.css" rel="stylesheet">
``
在本地运行时,在配置文件中加项目名,
发布时注解掉.
server.servlet.context-path=/employpay
该博客介绍了如何在Spring MVC中配置拦截器,包括两个拦截器PCLoginInterceptor和WeChatLoginInterceptor,分别处理/api/**和/lan-jie/**路径,并排除静态资源。在本地开发时,通过配置server.servlet.context-path=/employpay来添加项目名,但在发布时需注解掉。此外,还展示了如何处理静态资源,将所有路径映射到应用根目录和类路径下的资源。
2781

被折叠的 条评论
为什么被折叠?



