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. 从结构和数字看OO——面向对象设计与构造第一章总结

    不知不觉中,我已经接触OO五周了,顺利地完成了第一章节的学习,回顾三次编程作业,惊喜于自身在设计思路和编程习惯已有了一定的改变,下面我将从度量分析.自身Bug.互测和设计模式四个方向对自己第一章的学习 ...

  2. 2018.11.16javascript课上随笔(DOM)

    <li> <a href = "“#”>-</a> </li> <li>子节点:文本节点(回车),元素节点,文本节点. 不同节点树 ...

  3. 双向链表的双向冒泡排序 c++

    #include<iostream> using namespace std; #define swap(a,b) {int t;t = a;a = b;b = t;} //节点类型的定义 ...

  4. 64位win7+PCL1.6.0+VS2010,64位win10+PCL1.6.0+VS2010

    https://blog.csdn.net/liukunrs/article/details/80216329 大体转载自:https://blog.csdn.net/sinat_24206709/a ...

  5. 手把手教你入门Yii2框架-1

    前言概述: 我是一名PHP开发工程师,最拿手的是版本2.0的Yii框架,在培训班里老师没教我Yii框架,只是由于我弟弟(同行)擅长Yii框架,所以我用得最多的就是Yii2.0,后台我学了ThinkPH ...

  6. oi笔记——抽象的深度优先搜索

    oi笔记--抽象的深度优先搜索 例题: \(N个数中选K个数,选出的和要为sum\) 例题分析: 对于每个点,我们可以按"选"和"不选"进行搜索,如图: 或者0 ...

  7. FPGA的基本组成单元LUT,以及三种核的概念

    .查找表 LUT就是查找表,对于4输入的LUT而言,实际上就是4位地址位,一位数据位的存储器,能够存储16位数据,所以我们在FPGA设计中可以用LUT组建分布式的RAM. 这样也可以解释我们在设计中为 ...

  8. [Algo] 118. Array Deduplication IV

    Given an unsorted integer array, remove adjacent duplicate elements repeatedly, from left to right. ...

  9. ZJNU 2204 - dzj的数学作业

    我猜这个数列可以直接从大到小凑…… 推出帕多瓦数列每一项,从大到小循环 遇到小于等于x的项就减掉这一项 全部循环完毕后判断x是否为0即可 #include<stdio.h> typedef ...

  10. 国产手机早就异军突起,为何还是有很多人“迷恋”iPhone?

    近几年,国产手机不论是从外观上还是设计上,以及销量上都比前有了长足的进步,手机的品质和售后也在不断提升.这也让中国手机厂商不断的推出了拥有自己品牌特色的产品.特别是2018年不少国产手机品牌的旗舰级产 ...