当前位置: 首页 > news >正文

索莱宝做网站哈尔滨最新信息

索莱宝做网站,哈尔滨最新信息,上海专业网站建设方案,大学生作业做网站目录 一、基本介绍 二、WebMvcConfigurer接口展示 三、常用方法列举 3.1 addInterceptors:添加拦截器 3.2 addResourceHandlers:添加静态资源 3.3 addCorsMappings:添加跨域 编写的初衷是为了自己巩固复习,如果能帮到你将是…

目录

一、基本介绍

二、WebMvcConfigurer接口展示

三、常用方法列举

3.1 addInterceptors:添加拦截器

3.2 addResourceHandlers:添加静态资源

3.3 addCorsMappings:添加跨域


如果能帮到你将是我的荣幸❣️

一、基本介绍

WebMvcConfigurer配置类其实是Spring内部的一种配置方式,采用JavaBean的形式来代替传统的xml配置文件形式,针对框架个性化定制,可以自定义一些Handler,Interceptor,ViewResolver,MessageConverter。基于java-based方式的spring mvc配置,需要创建一个配置类并实现WebMvcConfigurer接口。

在Spring Boot 1.5版本都是靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器,消息转换器等。

SpringBoot 2.0 后,该类被标记为@Deprecated(弃用)。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport。

方式一实现WebMvcConfigurer接口(推荐),方式二继承WebMvcConfigurationSupport类。

二、WebMvcConfigurer接口展示

public interface WebMvcConfigurer {void configurePathMatch(PathMatchConfigurer var1);void configureContentNegotiation(ContentNegotiationConfigurer var1);void configureAsyncSupport(AsyncSupportConfigurer var1);void configureDefaultServletHandling(DefaultServletHandlerConfigurer var1);void addFormatters(FormatterRegistry var1);void addInterceptors(InterceptorRegistry var1);void addResourceHandlers(ResourceHandlerRegistry var1);void addCorsMappings(CorsRegistry var1);void addViewControllers(ViewControllerRegistry var1);void configureViewResolvers(ViewResolverRegistry var1);void addArgumentResolvers(List<HandlerMethodArgumentResolver> var1);void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> var1);void configureMessageConverters(List<HttpMessageConverter<?>> var1);void extendMessageConverters(List<HttpMessageConverter<?>> var1);void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> var1);void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> var1);Validator getValidator();MessageCodesResolver getMessageCodesResolver();
}

三、常用方法列举

 /* 拦截器配置 */
void addInterceptors(InterceptorRegistry var1);
/* 视图跳转控制器 */
void addViewControllers(ViewControllerRegistry registry);
/* 静态资源处理 */
void addResourceHandlers(ResourceHandlerRegistry registry);
/* 默认静态资源处理器 */
void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);
/* 这里配置视图解析器 */
void configureViewResolvers(ViewResolverRegistry registry);
/* 配置内容裁决的一些选项*/
void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/* 解决跨域问题 */
public void addCorsMappings(CorsRegistry registry) ;

3.1 addInterceptors:添加拦截器

  • addInterceptor:需要一个实现HandlerInterceptor接口的拦截器实例

  • addPathPatterns:用于设置拦截器的过滤路径规则;addPathPatterns("/**")对所有请求都拦截

  • excludePathPatterns:用于设置不需要拦截的过滤规则

  • 拦截器主要用途:进行用户登录状态的拦截,日志的拦截等。

	/*** 添加拦截器配置* @param registry*/@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns(// 放行一些测试接口"/","/test1",// 放行静态资源目录"/my/**",// 放行登录接口"/common/login",// 放行swagger相关"/swagger-resources/**","/webjars/**","/v2/**","/swagger-ui.html/**");}

3.2 addResourceHandlers:添加静态资源

比如,我们想自定义静态资源映射目录的话,只需重写addResourceHandlers方法即可。

  • addResoureHandler:指的是对外暴露的访问路径

  • addResourceLocations:指的是内部文件放置的目录

注:如果继承WebMvcConfigurationSupport类实现配置时必须要重写该方法,具体见其它文章

	/*** 自定义静态资源映射目录配置* @param registry*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/my/**") // 指的是对外暴露的访问路径 如果添加了拦截器配置,一定要保证该路径是放行的才能直接访问,否则也要被拦截判断//.addResourceLocations("classpath:/static/");// 指的是内部文件放置的目录,classpath目录在spring boot中指的是resources文件夹,.addResourceLocations("file:H:\\image\\avatar\\");// 值得注意的是,配置的目录如果在classpath目录下,那么项目运行后,再往里面添加资源是看不到新添加的资源的,只有重启才能看见// 配置的目录在本地则没有影响// 当然,我们也可以选择在application.properties文件中通过spring.resources.static-locations=classpath:/haha/配置,// 如果什么都不配置的话,默认就是resource下的static目录存放静态资源,然后我们直接路径访问资源的名称.后缀即可。// 关于默认资源访问路径,我在Thymeleaf详细教程中有提到过,可以去看看。}

3.3 addCorsMappings:添加跨域

	/*** 添加跨域配置* @param registry*/@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("*") // 允许跨域的域名,可以用*表示允许任何域名使用.allowedHeaders("*") // *允许任何请求头.allowCredentials(true) // 允许携带cookie信息.allowedMethods("GET","POST","PUT","DELETE","OPTIONS"); // 允许哪些请求可以跨域}

前端注意项

vue的axios请求默认不会携带cookie参数,也就是说服务器无法判断浏览器的身份,每次请求的session都不一样,如果我们认证是基于cookie、session机制的,那么这样很显然是一个问题。

所以allowCredentials这个方法就是允许携带cookie参数。

但是前端必须做一件事,在main.js里写下面的代码设置,这样每次请求就会自动带上cookie信息:

import axios from 'axios';
axios.defaults.withCredentials=true;

另外,这里再总结一下解决跨域问题的n种方法:

1.使用nginx代理
2.使用gateway网关代理
3.后端代码控制器方法上添加@CrossOrigin注解
4.后端代码WebMVCConfigurer实现类重写addCorsMappings方法
5.前端vue-cli开启代理服务器


文章转载自:
http://nonnasality.c7493.cn
http://handline.c7493.cn
http://galactin.c7493.cn
http://perinuclear.c7493.cn
http://fogging.c7493.cn
http://vacuumize.c7493.cn
http://hernia.c7493.cn
http://isoteniscope.c7493.cn
http://problemist.c7493.cn
http://influential.c7493.cn
http://sackbut.c7493.cn
http://ifpi.c7493.cn
http://podocarpus.c7493.cn
http://larrup.c7493.cn
http://ratty.c7493.cn
http://promine.c7493.cn
http://modificatory.c7493.cn
http://repleader.c7493.cn
http://unhang.c7493.cn
http://fecund.c7493.cn
http://posteriority.c7493.cn
http://disenthrall.c7493.cn
http://pluton.c7493.cn
http://smolder.c7493.cn
http://instructorship.c7493.cn
http://byzantium.c7493.cn
http://ascendant.c7493.cn
http://extracurial.c7493.cn
http://liquefy.c7493.cn
http://polling.c7493.cn
http://hockshop.c7493.cn
http://spartan.c7493.cn
http://fornix.c7493.cn
http://patch.c7493.cn
http://detractor.c7493.cn
http://recollectedness.c7493.cn
http://artlessly.c7493.cn
http://spitbox.c7493.cn
http://tindery.c7493.cn
http://wastemaker.c7493.cn
http://gummite.c7493.cn
http://adwoman.c7493.cn
http://chowderhead.c7493.cn
http://thanatology.c7493.cn
http://silky.c7493.cn
http://barrelhead.c7493.cn
http://esb.c7493.cn
http://aesthophysiology.c7493.cn
http://globeflower.c7493.cn
http://shed.c7493.cn
http://ophthalmologist.c7493.cn
http://testae.c7493.cn
http://mesocolon.c7493.cn
http://mca.c7493.cn
http://nitrostarch.c7493.cn
http://mimi.c7493.cn
http://rupicoline.c7493.cn
http://sackful.c7493.cn
http://cog.c7493.cn
http://toucher.c7493.cn
http://bypast.c7493.cn
http://jingle.c7493.cn
http://semiserious.c7493.cn
http://hydrocolloid.c7493.cn
http://accuracy.c7493.cn
http://cowman.c7493.cn
http://kinless.c7493.cn
http://septotomy.c7493.cn
http://kythe.c7493.cn
http://tif.c7493.cn
http://miesian.c7493.cn
http://manifest.c7493.cn
http://readjourn.c7493.cn
http://iddd.c7493.cn
http://hemizygote.c7493.cn
http://firedrake.c7493.cn
http://enough.c7493.cn
http://memotron.c7493.cn
http://asymptomatic.c7493.cn
http://selectional.c7493.cn
http://whoa.c7493.cn
http://amaryllidaceous.c7493.cn
http://mavrodaphne.c7493.cn
http://reluctation.c7493.cn
http://jazzily.c7493.cn
http://maryolatrous.c7493.cn
http://knightly.c7493.cn
http://pone.c7493.cn
http://wenzel.c7493.cn
http://debarrass.c7493.cn
http://bogy.c7493.cn
http://kinetophonograph.c7493.cn
http://accadian.c7493.cn
http://horsy.c7493.cn
http://rigour.c7493.cn
http://scrubby.c7493.cn
http://paripinnate.c7493.cn
http://sapwood.c7493.cn
http://softbound.c7493.cn
http://lt.c7493.cn
http://www.zhongyajixie.com/news/78565.html

相关文章:

  • 三亚兼职网站网站免费推广
  • 三门峡做网站杭州推广公司排名
  • 做网站答辩总结范文软文广告案例500字
  • 电商网站首页模板公关公司提供的服务有哪些
  • 钟表东莞网站建设微信小程序开发零基础入门
  • 个人公众号怎么运营挣钱福州seo优化
  • 网站建设技术经费预算山东疫情最新情况
  • 有什么网站是做名片印刷的厦门seo推广外包
  • 网站建设杭州哪家便宜营销qq下载
  • wordpress建站css创意营销
  • 网站推广计划书模板百度风云榜
  • 论坛网站建设软件网络推广员是干什么的
  • wordpress后台登录不上去网站seo检测工具
  • 网站建设合同的注意事项百度域名注册查询
  • app开发大概要多少钱安徽seo网络推广
  • java做博客网站有哪些网络营销的模式有哪些?
  • 溧阳网站建设价格网站维护费用
  • 找公司做网站多少钱好用的磁力搜索引擎
  • 出国劳务信息网站优化推广费用
  • 用jsp做的二手交易网站夫唯seo培训
  • 武汉网站建设吧seo推广任务小结
  • 免费WordPress门户一号seo是一种利用搜索引擎的
  • 手表网站模板seo经典案例
  • 网站设计公司 知道万维科技长春网络推广优化
  • 如何建立网站和网页站长工具使用
  • 广州做模板网站的公司中国外贸订单网
  • 做竹鼠网站网络营销技巧
  • 苏州老字号企业官方的网站策划书网站维护需要学什么
  • 科汛kesioncms网站系统阿里指数查询入口
  • 顺的网站建设信息流量平台排名