@EnableWebMvc WebMvcConfigurer CorsConfig
package me.zhengjie.core.config; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /**
* 跨域请求
*
* @author jie
* @date 2018-11-30
*/
@Configuration
@EnableWebMvc
public class CorsConfig implements WebMvcConfigurer { @Override
public void addCorsMappings(CorsRegistry registry) {
//设置允许跨域的路径
registry.addMapping("/**")
//设置允许跨域请求的域名
.allowedOrigins("*")
//是否允许证书 不再默认开启
.allowCredentials(true)
//设置允许的方法
.allowedMethods("*")
//跨域允许时间
.maxAge(3600);
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/").setCachePeriod(0);
}
}
精通SpringBoot——第三篇:详解WebMvcConfigurer接口
https://yq.aliyun.com/articles/617307
使用WebMvcConfigurer配置SpringMVC
https://www.jianshu.com/p/52f39b799fbb
详解@EnableWebMvc
https://www.cnblogs.com/lvbinbin2yujie/p/10624584.html
package org.linlinjava.litemall.wx.config; import org.linlinjava.litemall.wx.annotation.support.LoginUserHandlerMethodArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import java.util.List; @Configuration
public class WxWebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new LoginUserHandlerMethodArgumentResolver());
}
}
package org.linlinjava.litemall.wx.annotation.support; import org.linlinjava.litemall.wx.annotation.LoginUser;
import org.linlinjava.litemall.wx.service.UserTokenManager;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer; public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
public static final String LOGIN_TOKEN_KEY = "X-Litemall-Token"; @Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterType().isAssignableFrom(Integer.class) && parameter.hasParameterAnnotation(LoginUser.class);
} @Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container,
NativeWebRequest request, WebDataBinderFactory factory) throws Exception { // return new Integer(1);
String token = request.getHeader(LOGIN_TOKEN_KEY);
if (token == null || token.isEmpty()) {
return null;
} return UserTokenManager.getUserId(token);
}
}
@EnableWebMvc WebMvcConfigurer CorsConfig的更多相关文章
- @EnableWebMvc WebMvcConfigurer
Spring注解@EnableWebMvc使用坑点解析 https://blog.csdn.net/zxc123e/article/details/84636521 @EnableWebMvc,Web ...
- SpringBoot笔记(5)
一.Web原生组件注入 1.使用Servlet API @ServletComponentScan(basePackages = "com.atguigu.admin") :指定原 ...
- @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别
@EnableWebMvc是什么 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration. @Retention(RetentionPolicy ...
- Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)
@EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...
- Spring Boot @EnableWebMvc 与 mvc 配置
注意: 1.小心使用 @EnableWebMvc 注解 根据官方文档,尽量不要使用 @EnableWebMvc 注解,因为它会关闭默认配置. ① 你希望关闭默认配置,自己完全重新实现一个 @Enab ...
- Spring EnableWebMvc vs WebMvcConfigurationSupport
EnableWebMvc vs WebMvcConfigurationSupport spring doc解释 WebMvcConfigurationSupport: This is the main ...
- @EnableWebMvc
1.启用MVC Java config 或 MVC XML namespace 想要启用MVC Java config,只需要将@EnableWebMvc添加到你的一个@Configuration c ...
- 详解@EnableWebMvc
最近看了<Spring in Action>的开头,就被Spring注解开发(完全不写web.xml)惊叹了,也第一次知道了@EnableWebMvc是SpringMVC的注解 @Enab ...
- WebMvcConfigurationSupport与WebMvcConfigurer的关系
大家从网上及源码注释上查到的解释是,在spring中配置WebMvc时有两种方法,一种是继承WebMvcConfigurationSupport,重写里面相应的方法,还有一种是继承WebMvcConf ...
随机推荐
- 落地即王道,锁死企业智变CP——云+AI
国庆前夜,"70年,我是主角"的微电影引发网络热议,这是人民日报新媒体和电影频道联手打造.京东云和京东AI提供技术支持.这是中国首部全民定制国庆献礼片,网友只要上传正脸照片,就能通 ...
- 吴裕雄--天生自然 JAVASCRIPT开发学习:HTML DOM 节点列表
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- ubuntu---【NVIDIA驱动 + CUDA 安装】不成功时的卸载方式
NVIDIA驱动 与 CUDA 安装不成功时,可以卸载,检查相关问题(配置.兼容性等),重新安装.这里记录一下,卸载方式.
- 18 11 12 网络通信 tcp
tec 客户端的接受代码 import socket def main(): # 1. 创建tcp的套接字 tcp_socket = socket.socket(socket.AF_INET, soc ...
- MQL4编程—值传递和引用传递
定义 简单的说就是给一个变量或者对象取一个别名(引用和被引用的共享存储单元,要用修饰符&). 引用的用法 在MQL4中通过调试只接受引用作为函数参数的用法,在函数参数传递过程中,有两种传递方式 ...
- html属性,上传图片选择时只显示图片文件
这个实现比较简单,就是用到accept属性: 注意这里我们对这个file元素进行了隐藏,因为它默认呈现是下面这个样子的,并不好看. <div style="display:none;& ...
- Aras Innovator客户端批量下载关联文件
<button onclick="btnDownload();" id="downfilebtn">批量下载关联文件</button> ...
- 吴裕雄--天生自然 JAVA开发学习:日期时间
import java.util.Date; public class DateDemo { public static void main(String args[]) { // 初始化 Date ...
- python格式化输出的三种形式
法一: list_a = [1, 2, 3] str_b = 'aaa' string = "There are two contents:%s, %s" % (list_a, s ...
- flask框架-下
Local与偏函数 threasing.local 多个线程修改同一个数据,复制多份变量给每个线程用,为每个线程开辟一块空间进行数据存储. 不使用therading.local # 不用local f ...