静态资源配置
- yml方式 ,简易快捷配置
# spring boot版本 > 2.5.0
spring:
web:
resources:
# 路径值为: file:img/ ↓
# 开发时,指向src同级目录 /img
# 部署时,在jar包同级目录
# file:../img/ ,代表上一级目录
# 资源匹配路径都为 /** , eg: localhost:8080/1.jpg
static-locations: file:img/,file:upload/
#配置整体的资源映射路径
#mvc:
#static-path-pattern: /**
# 过往版本 spring boot
spring:
resources:
static-locations:
配置类方式:
- 可以灵活配置静态资源映射;
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/upload/**")
.addResourceLocations("file:upload/");
registry.addResourceHandler("/img/**")
.addResourceLocations("file:img/");
}
}
拦截器
- 好比进停车场是拦住车,是可以控制用户的访问的;如用户的权限访问控制
过滤器
- 好比 水过滤器一样,去除掉部分不需要的东西
请求 -> 过滤器 -> 拦截器
拦截器实现:
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class MyInterCeptor implements HandlerInterceptor {
private String id;
//return true 放行
//拦截
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("-------拦截器:处理前-------");
return true;
//return HandlerInterceptor.super.preHandle(request, response, handler);
}
//拦截日志
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("拦截器:处理后: ");
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("-------拦截器:资源清理-------");
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
@Resource
private MyInterCeptor interCeptor;
/**
* 注册自定义拦截器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//可以多个拦截器
InterceptorRegistration interceptorRegistration = registry.addInterceptor(interCeptor);
//可以设置多个路径拦截
interceptorRegistration.addPathPatterns("/**"); //拦截全部
}
}
过滤器实现:
import org.springframework.core.annotation.Order;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@Order(1)
@WebFilter(filterName = "myFilter",urlPatterns = {"/*"})
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("过滤器:进入目标资源之前先干点啥");
filterChain.doFilter(servletRequest, servletResponse);
}
}
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FilterConfig {
@Bean
public FilterRegistrationBean registFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new MyFilter());
registrationBean.addUrlPatterns("/*");
registrationBean.setName("Filter1");
registrationBean.setOrder(1);
return registrationBean;
}
}