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

网站合同需要注意什么呢创建一个互联网媒体公司

网站合同需要注意什么呢,创建一个互联网媒体公司,做的比较简约的网站,做标书的任务网站WebMvcConfigurer 介绍 1. 什么是WebMvcConfigurer 介绍2. WebMvcConfigurer接口常用的方法3. 使用WebMvcConfigurer实现跨域4. 使用WebMvcConfigurer配置拦截器5. 使用WebMvcConfigurer配置静态资源5.1 配置外部目录#xff08;本地文件系统#xff09;详细解释 6. 使用 Web… WebMvcConfigurer 介绍 1. 什么是WebMvcConfigurer 介绍2. WebMvcConfigurer接口常用的方法3. 使用WebMvcConfigurer实现跨域4. 使用WebMvcConfigurer配置拦截器5. 使用WebMvcConfigurer配置静态资源5.1 配置外部目录本地文件系统详细解释 6. 使用 WebMvcConfigurer 配置视图解析6.1 详细说明6.1.1 使用 WebMvcConfigurer 配置 Thymeleaf 视图解析 7. 使用 WebMvcConfigurer 配置消息转换器 1. 什么是WebMvcConfigurer 介绍 WebMvcConfigurer 是 Spring Boot 中用于扩展 Spring MVC 功能的接口可以用来自定义 Web 相关的配置例如 ✅ 跨域CORS 配置 addCorsMappings ✅ 拦截器Interceptor addInterceptors ✅ 静态资源映射 addResourceHandlers ✅ 消息转换器Message Converter configureMessageConverters ✅ 视图解析器View Resolver configureViewResolvers ✅ 参数解析 它的主要作用是在不改变 Spring Boot 默认行为的情况下提供灵活的扩展能力。 复杂的解释 WebMvcConfigurer配置类其实是Spring内部的一种配置方式采用JavaBean的形式来代替传统的xml配置文件形式进行针对框架个性化定制可以自定义一些HandlerInterceptorViewResolverMessageConverter。基于java-based方式的spring mvc配置需要创建一个配置类并实现WebMvcConfigurer 接口 在Spring Boot 1.5版本都是靠重写WebMvcConfigurerAdapter的方法来添加自定义拦截器消息转换器等。SpringBoot 2.0 后该类被标记为Deprecated弃用。官方推荐直接实现WebMvcConfigurer或者直接继承WebMvcConfigurationSupport。 2. WebMvcConfigurer接口常用的方法 /* 拦截器配置 */ 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. 使用WebMvcConfigurer实现跨域 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration public class CorsConfig {Beanpublic WebMvcConfigurer corsConfigurer() {return new WebMvcConfigurer() {Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping(/**) // 允许所有路径.allowedOrigins(https://frontend.com) // 允许特定域名.allowedMethods(GET, POST, PUT, DELETE, OPTIONS) // 允许的请求方法.allowedHeaders(*) // 允许所有请求头.allowCredentials(true) // 允许携带 Cookie.maxAge(3600); // 预检请求的缓存时间}};} } 当然Nginx也可以实现 location / {proxy_pass http://localhost:8080;add_header Access-Control-Allow-Origin https://frontend.com;add_header Access-Control-Allow-Methods GET, POST, OPTIONS, PUT, DELETE;add_header Access-Control-Allow-Headers Authorization, Content-Type;add_header Access-Control-Allow-Credentials true;if ($request_method OPTIONS) {return 204;} }a. 其中location / 其中/代表匹配所有请求。 b. proxy_pass http://localhost:8080; Nginx 会把所有匹配 / 的请求转发到 http://localhost:8080假设 Spring Boot 运行在 8080 端口。 这样前端请求 NginxNginx 代理请求到 Spring Boot对外隐藏后端服务。 c. add_header Access-Control-Allow-Origin https://frontend.com; 允许跨域的前端来源 允许 https://frontend.com 访问后端 API。 如果改成*add_header Access-Control-Allow-Origin *; 表示所有域都可以访问 d. add_header Access-Control-Allow-Methods GET, POST, OPTIONS, PUT, DELETE; 允许前端调用 GET、POST、OPTIONS、PUT、DELETE 方法。 e. add_header Access-Control-Allow-Headers Authorization, Content-Type; 允许前端在请求中携带 Authorization比如 Bearer Token和 Content-Type 头。 f. add_header Access-Control-Allow-Credentials true; 允许携带 Cookie 允许前端发送带有 Cookie 的跨域请求如身份验证。 ⚠️ 如果启用 Access-Control-Allow-Credentials必须指定具体的 Access-Control-Allow-Origin不能用 *。 g. if ($request_method OPTIONS) {return 204; }处理预检请求OPTIONS 当浏览器发送 OPTIONS 预检请求比如跨域的 POST 请求Nginx 直接返回 204 No Content避免 Spring Boot 处理这类请求提高性能。 总结 将请求转发到 Spring Boot (http://localhost:8080)允许 https://frontend.com 访问后端 API支持跨域CORS允许 GET, POST, OPTIONS, PUT, DELETE 请求允许 Authorization 和 Content-Type 请求头允许前端携带 Cookie优化预检请求OPTIONS直接返回 204减少后端负担 适用场景 ✅ 前后端分离项目前端 https://frontend.com后端 http://localhost:8080 ✅ 使用 Nginx 代理后端 API同时处理跨域 ✅ 后端 API 需要支持身份验证携带 Cookie 或 Token 4. 使用WebMvcConfigurer配置拦截器 拦截器HandlerInterceptor可以用于日志记录、权限校验、请求处理等。 import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration public class WebConfig implements WebMvcConfigurer {Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(new MyInterceptor()).addPathPatterns(/api/**) // 只拦截 /api/ 开头的请求.excludePathPatterns(/api/login, /api/register); // 这些路径不拦截} } ✅ 适用场景 用户登录拦截请求日志记录权限控制 5. 使用WebMvcConfigurer配置静态资源 import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration public class WebConfig implements WebMvcConfigurer {Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {// 将 /static/ 目录下的资源映射到 /resources/** 访问路径registry.addResourceHandler(/resources/**).addResourceLocations(classpath:/static/);// 访问 http://localhost:8080/images/test.jpg 时会去 D:/uploads/ 目录查找registry.addResourceHandler(/images/**).addResourceLocations(file:D:/uploads/);// 访问 http://localhost:8080/files/doc.pdf 时会去 /home/user/docs/ 目录查找registry.addResourceHandler(/files/**).addResourceLocations(file:/home/user/docs/);} }说明 Configuration标记该类为 Spring 配置类使其生效。实现 WebMvcConfigurer 接口 通过 addResourceHandlers 方法自定义静态资源映射规则。registry.addResourceHandler(/resources/**) 访问路径形如 /resources/** 的请求会被映射到 classpath:/static/目录下的资源即 src/main/resources/static/。 例如src/main/resources/static/css/style.css 可以通过 http://localhost:8080/resources/css/style.css 访问。配置外部目录本地文件系统 /images/** 访问路径映射到 D:/uploads/ 目录必须加 file: 前缀。 /files/** 访问 /home/user/docs/ 目录下的资源。 默认静态资源位置 如果没有自定义 WebMvcConfigurerSpring Boot 默认会从以下位置加载静态资源 classpath:/static/ classpath:/public/ classpath:/resources/ classpath:/META-INF/resources/ 例如 src/main/resources/static/index.html 可以直接通过 http://localhost:8080/index.html 访问。 但如果自定义了 WebMvcConfigurer默认配置可能会被覆盖因此需要手动添加 addResourceHandlers 方法来保持默认行为。 5.1 配置外部目录本地文件系统详细解释 在 Spring Boot 中默认的静态资源如 CSS、JS、图片等通常存放在 src/main/resources/static/ 目录下并且可以直接通过 http://localhost:8080/资源路径 访问。 但是如果你想让应用访问 本地文件系统比如 D:/uploads/ 或 /home/user/docs/就需要手动配置 addResourceHandlers()并使用 file: 前缀 指定外部目录。 示例 1Windows 配置外部目录 Override public void addResourceHandlers(ResourceHandlerRegistry registry) {// 让 /images/** 访问 D:/uploads/ 目录下的资源registry.addResourceHandler(/images/**).addResourceLocations(file:D:/uploads/); }解释 addResourceHandler(“/images/**”) 表示访问 http://localhost:8080/images/xxx.jpg 时会去 D:/uploads/ 目录查找 xxx.jpg。 addResourceLocations(“file:D:/uploads/”) file: 必须加上表明这是一个文件系统路径而不是类路径classpath。这个目录是 Windows 本地磁盘不是 Spring Boot 默认的 static/ 目录。 示例 D:/uploads/test.jpg 可以通过 http://localhost:8080/images/test.jpg 访问。 示例2:同时支持多个目录 Override public void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler(/uploads/**).addResourceLocations(file:D:/uploads/, file:/home/user/docs/); }这样访问 http://localhost:8080/uploads/test.jpg 时Spring 会先去 D:/uploads/ 目录找再去 /home/user/docs/ 目录找。 6. 使用 WebMvcConfigurer 配置视图解析 WebMvcConfigurer 还可以用于配置自定义视图解析器。 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;Configuration public class WebConfig implements WebMvcConfigurer {Beanpublic ViewResolver viewResolver() {InternalResourceViewResolver resolver new InternalResourceViewResolver();resolver.setPrefix(/WEB-INF/views/); // 视图文件前缀resolver.setSuffix(.jsp); // 视图文件后缀return resolver;} }✅ 适用场景 Spring MVC JSPThymeleaf 或 FreeMarker 视图解析 6.1 详细说明 在 Spring Boot 中视图解析器View Resolver 负责将控制器返回的视图名称解析为实际的 视图文件如 HTML、JSP、Thymeleaf 等。如果你想自定义视图解析规则可以实现 WebMvcConfigurer 接口。 6.1.1 使用 WebMvcConfigurer 配置 Thymeleaf 视图解析 Spring Boot 默认集成了 Thymeleaf 作为模板引擎我们可以使用 WebMvcConfigurer 进行自定义配置比如 修改视图文件存放路径自定义前缀/后缀 import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;Configuration public class WebConfig implements WebMvcConfigurer {Overridepublic void configureViewResolvers(ViewResolverRegistry registry) {// 配置 Thymeleaf 视图解析器registry.jsp(/WEB-INF/views/, .jsp); // 仅用于 JSP} }解释 registry.jsp(/WEB-INF/views/, .jsp)前缀/WEB-INF/views/后缀.jsp例如当控制器返回 “home”最终视图路径解析为 /WEB-INF/views/home.jsp。 注意 这个registry.jsp() 主要用于 JSP 视图解析如果使用 ThymeleafSpring Boot 会自动管理无需额外配置。 7. 使用 WebMvcConfigurer 配置消息转换器 Spring Boot 提供了默认的 JSON 处理方式Jackson但如果你想使用 Gson 或自定义格式可以在 WebMvcConfigurer 里配置消息转换器。 import com.google.gson.Gson; import com.google.gson.GsonBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.GsonHttpMessageConverter; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;import java.util.List;Configuration public class WebConfig implements WebMvcConfigurer {Beanpublic Gson gson() {return new GsonBuilder().setPrettyPrinting().create();}Overridepublic void extendMessageConverters(ListHttpMessageConverter? converters) {converters.add(new GsonHttpMessageConverter(gson()));} }✅ 适用场景 替换默认的 Jackson 为 Gson支持自定义 JSON 格式
http://www.hkea.cn/news/14496654/

相关文章:

  • 建一个网站怎么赚钱吗东道设计公司
  • 网站要怎么备案网页设计作品评价
  • 建设工程新工艺网站wordpress 等待响应
  • 网站备份文件网站建设都包括哪几个方面
  • 欢迎回来请牢记网站域名扬州网站建设网站
  • 做网站用windows还是linux南京做网站建设搭建的公司
  • 分栏式网站广告制作公司简介怎么写
  • 沈阳网站如何制作零基础怎么开网店
  • 网站建设公告网站建设立项报告
  • 做网络销售哪些网站比较好360度全景街景地图
  • wordpress博客页面修改做搜索引擎优化网站费用
  • 网站信息备案变更 哪里做成都网络营销策划公司
  • t么做文献索引ot网站工作室怎么注册
  • 福建省住房和城乡建设厅网站电话wordpress配置文件下载
  • 信用中国 网站截图怎么做多钱网网站
  • 网站建设 小程序开发射阳网页设计
  • 城关区建设局网站网站服务包括什么
  • 学做网站平台软件工程最吃香的三个专业
  • 在网站建设中经历的流程wordpress 新建
  • 网站尺寸自适应推荐网址
  • 网站开发人员的前景宁波网站建设信息
  • 思淘网站建设电商网页的特点
  • 网站怎么盈利站长工具seo综合查询引流
  • 定制高端网站的公司龙岗 营销型网站建设
  • 海外访问国内网站 dns全国设计网站建设
  • 网站创建服务wordpress 下雪插件
  • 如何进行网站性能优化企业邮箱注册免费申请
  • 如何做个购物网站河北邢台房价多少钱一平方
  • 推荐昆明做网站建设深圳公司网站
  • 军队房地产与建设工程法律实务在哪个网站可以购买seo自媒体运营技巧