配置静态资源地址映射
package top.xinzhang0618.demo.config;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** 静态资源地址映射** @author gavin* @version 2020/6/10 0010*/@Configurationpublic class StaticResourceConfig implements WebMvcConfigurer {/*** 访问 http://localhost:8080/images/11.png ==> D:/images/11.png* @param registry*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/images/**").addResourceLocations("file:D://images/");}}
参考:
/*** FileName: StaticResourceConfig* Description: 静态文件转换* Author: snow* Date: 2019/1/23 20:32*/@Configurationpublic class StaticResourceConfig implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");}@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/api/**").allowedOrigins("*").allowedMethods("PUT", "GET", "POST", "DELETE", "PATCH").allowCredentials(false).maxAge(3600);}}
全局跨域配置
参考: https://www.cnblogs.com/yuansc/p/9076604.html
跨域知识点补充:
1.同源策略[same origin policy]是浏览器的一个安全功能,不同源的客户端脚本在没有明确授权的情况下,不能读写对方资源。 同源策略是浏览器安全的基石。
源[origin]就是协议、域名和端口号。例如:http://www.baidu.com:80这个URL。若地址里面的协议、域名和端口号均相同则属于同源。
2.跨域,非同源的网站之前的请求是跨域请求
3.如何跨域,降域,JSONP跨域,CORS跨域
4.CORS跨域
为了解决浏览器同源问题,W3C 提出了跨源资源共享,即 CORS(Cross-Origin Resource Sharing)。
CORS 做到了如下两点:
- 不破坏即有规则
- 服务器实现了 CORS 接口,就可以跨源通信
CORS分为简单请求和非简单请求
4.1 对于简单请求,CORS的策略是请求时在请求头中增加一个Origin字段,服务器收到请求后,根据该字段判断是否允许该请求访问。
- 如果允许,则在 HTTP 头信息中添加
Access-Control-Allow-Origin字段,并返回正确的结果 ; - 如果不 允许,则不在 HTTP 头信息中添加
Access-Control-Allow-Origin字段 。
4.2 对于非简单请求的跨源请求,浏览器会在真实请求发出前,增加一次OPTION请求,称为预检请求(preflight request)。预检请求将真实请求的信息,包括请求方法、自定义头字段、源信息添加到 HTTP 头信息字段中,询问服务器是否允许这样的操作。
5.SpringBoot配置CORS
5.1 使用@CrossOrigin注解(可用于类和接口上)
5.2 WebMvcConfigurer配置addCorsMappings
/*** @author gavin* @version 2020/6/10 0010*/@Configurationpublic class MvcConfig implements WebMvcConfigurer {/*** 设置全局跨域*/@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH")// 预检请求的有效期,单位为秒。有效期内,不会重复发送预检请求.maxAge(3600)// 是否允许用户发送、处理 cooki.allowCredentials(true);}}
5.3 添加Filter
/*** CorsConfig** @author gavin* @version 2020/6/10 0010*/@Configurationpublic class CorsConfig {@Beanpublic FilterRegistrationBean corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();CorsConfiguration config = new CorsConfiguration();config.setAllowCredentials(true);config.addAllowedOrigin("*");config.addAllowedHeader("*");config.addAllowedMethod("*");// CORS 配置对所有接口都有效source.registerCorsConfiguration("/**", config);FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));bean.setOrder(0);return bean;}}
配置拦截器
HandlerInterceptor拦截器的使用
参考: https://blog.csdn.net/zhibo_lv/article/details/81699360
配置转化器
字符串转LocalDate
/*** 字符串转LocalDate** @author gavin* @date 2020-06-12*/public class String2LocalDateConverter implements Converter<String, LocalDate> {@Overridepublic LocalDate convert(String source) {if (StringUtils.isBlank(source)) {return null;}return DateUtils.parserLocalDate(source);}}
字符串装LocalDateTime
public class String2LocalDateTimeConverter implements Converter<String, LocalDateTime> {@Overridepublic LocalDateTime convert(String source) {if (StringUtils.isBlank(source)) {return null;}return DateUtils.parserLocalDateTime(source);}}
配置
/*** @author gavin* @date 2020-06-12*/@Configurationpublic class WebConfiguration implements WebMvcConfigurer {@Overridepublic void addFormatters(FormatterRegistry registry) {registry.addConverter(String.class, LocalDateTime.class, new String2LocalDateTimeConverter());registry.addConverter(String.class, LocalDate.class, new String2LocalDateConverter());}}
