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的更多相关文章

  1. @EnableWebMvc WebMvcConfigurer

    Spring注解@EnableWebMvc使用坑点解析 https://blog.csdn.net/zxc123e/article/details/84636521 @EnableWebMvc,Web ...

  2. SpringBoot笔记(5)

    一.Web原生组件注入 1.使用Servlet API @ServletComponentScan(basePackages = "com.atguigu.admin") :指定原 ...

  3. @EnableWebMvc,WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter区别

    @EnableWebMvc是什么 直接看源码,@EnableWebMvc实际上引入一个DelegatingWebMvcConfiguration. @Retention(RetentionPolicy ...

  4. Spring boot 梳理 -@SpringBootApplication、@EnableAutoConfiguration与(@EnableWebMVC、WebMvcConfigurationSupport,WebMvcConfigurer和WebMvcConfigurationAdapter)

    @EnableWebMvc=继承DelegatingWebMvcConfiguration=继承WebMvcConfigurationSupport 直接看源码,@EnableWebMvc实际上引入一 ...

  5. Spring Boot @EnableWebMvc 与 mvc 配置

    注意: 1.小心使用  @EnableWebMvc 注解 根据官方文档,尽量不要使用 @EnableWebMvc 注解,因为它会关闭默认配置. ① 你希望关闭默认配置,自己完全重新实现一个 @Enab ...

  6. Spring EnableWebMvc vs WebMvcConfigurationSupport

    EnableWebMvc vs WebMvcConfigurationSupport spring doc解释 WebMvcConfigurationSupport: This is the main ...

  7. @EnableWebMvc

    1.启用MVC Java config 或 MVC XML namespace 想要启用MVC Java config,只需要将@EnableWebMvc添加到你的一个@Configuration c ...

  8. 详解@EnableWebMvc

    最近看了<Spring in Action>的开头,就被Spring注解开发(完全不写web.xml)惊叹了,也第一次知道了@EnableWebMvc是SpringMVC的注解 @Enab ...

  9. WebMvcConfigurationSupport与WebMvcConfigurer的关系

    大家从网上及源码注释上查到的解释是,在spring中配置WebMvc时有两种方法,一种是继承WebMvcConfigurationSupport,重写里面相应的方法,还有一种是继承WebMvcConf ...

随机推荐

  1. CocoaPods为多个target添加依赖库/Podfile的配置

    Podfile的相关配置,请看官方文档http://guides.cocoapods.org/syntax/podfile.html 1)多个target公用相同库,还可以添加额外的不同第三方库 编辑 ...

  2. D语言-变量、输入、输出、注释

    Part 1:变量 D语言的变量有很多类型,这里只讨论几个基本类型 §1.1变量的定义方法 在D语言中,变量的定义方法是这样子的: [typename] [var1,var2,var3...] 其中, ...

  3. LeetCode——79. 单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  4. 视频课程 | 云原生下的Serverless浅谈

    京东云开发者社区在3月底于北京举行了以"Cloud Native时代的应用之路与开源创新"为主题的技术沙龙,现场多位技术大咖与开发者们面对面就Cloud Native进行了深入交流 ...

  5. 吴裕雄--天生自然 JAVASCRIPT开发学习:函数定义

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. tensorflow 读取训练集文件 from Hadoop

    1.代码配置 filename_queue = tf.train.string_input_producer([ "hdfs://namenode:8020/path/to/file1.cs ...

  7. ZJNU 2353 - UNO

    大模拟,但是题目好像有些地方表述不清 根据UNO在初中曾被别人虐了很久很久的经历 猜测出了原本的题意 本题中的+2虽然有颜色,但是也可以当作原UNO游戏中的+4黑牌 即在某人出了+2后,可以出不同颜色 ...

  8. Linux 杀掉所有Java进程

      ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9 管道符"|"用来隔开两个命令,管道符左 ...

  9. cppcheck下载及使用

    一.参考文档 1.Ubuntu下安装Cppcheck源码操作步骤 2.cppcheck std.cfg not found error when std.cfg file is available 3 ...

  10. UML-领域模型-添加关联和属性

    1.何谓关联? 关联(association):一个类的全局变量引用了另一个类,就表示关联了这个类 2.何时使用关联? 长时间(需要记住)留存的需要关联:短时间的不需要.比如: 需要关联:老师教那些课 ...