• SpringApplicationBuilder作用,以及设置跨域请求过滤器


    https://blog.csdn.net/qq_42714869/article/details/93052175

    springboot项目,若打包成war包,使用外置的tomcat启动

    1、需要继承 org.springframework.boot.context.web.SpringBootServletInitializer类

    2、然后重写configure(SpringApplicationBuilder application)方法

    1.  
      import org.mybatis.spring.annotation.MapperScan;
    2.  
      import org.springframework.boot.SpringApplication;
    3.  
      import org.springframework.boot.autoconfigure.SpringBootApplication;
    4.  
      import org.springframework.boot.builder.SpringApplicationBuilder;
    5.  
      import org.springframework.boot.web.servlet.FilterRegistrationBean;
    6.  
      import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
    7.  
      import org.springframework.cache.annotation.EnableCaching;
    8.  
      import org.springframework.context.annotation.Bean;
    9.  
      import org.springframework.core.annotation.Order;
    10.  
      import org.springframework.scheduling.annotation.EnableAsync;
    11.  
      import org.springframework.web.cors.CorsConfiguration;
    12.  
      import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
    13.  
      import org.springframework.web.filter.CorsFilter;
    14.  
       
    15.  
      import com.upincar.dms.filter.CharacterEncodingFilter;
    16.  
       
    17.  
      /**
    18.  
      *
    19.  
      * @Description:启动类
    20.  
      */
    21.  
      @SpringBootApplication
    22.  
      @EnableAsync
    23.  
      @MapperScan(basePackages = "com.upincar.dms.user.dao", sqlSessionFactoryRef = "sqlSessionFactory")
    24.  
      public class DmsApplication extends SpringBootServletInitializer {
    25.  
       
    26.  
      public static void main(String[] args) {
    27.  
      try{
    28.  
      SpringApplication.run(DmsApplication.class, args);
    29.  
      } catch (Exception e) {
    30.  
      e.printStackTrace();
    31.  
      }
    32.  
       
    33.  
      }
    34.  
       
    35.  
       
    36.  
      /**
    37.  
      * 如此配置打包后可以war包才可在tomcat下使用
    38.  
      * @param application
    39.  
      * @return
    40.  
      */
    41.  
      @Override
    42.  
      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    43.  
      return application.sources(DmsApplication.class);
    44.  
      }
    45.  
       
    46.  
       
    47.  
      /**
    48.  
      * 设置全局UTF-8编码过滤器
    49.  
      * @return
    50.  
      */
    51.  
      @Bean
    52.  
      @Order(Integer.MAX_VALUE)//设置执行先后顺序
    53.  
      public FilterRegistrationBean<CharacterEncodingFilter> characterFilterRegistration() {
    54.  
      FilterRegistrationBean<CharacterEncodingFilter> registration = new FilterRegistrationBean<CharacterEncodingFilter>();
    55.  
      registration.setFilter(new CharacterEncodingFilter());
    56.  
      registration.addUrlPatterns("/*");
    57.  
      registration.addInitParameter("paramName", "paramValue");
    58.  
      registration.setName("characterEncodingFilter");
    59.  
      return registration;
    60.  
      }
    61.  
       
    62.  
       
    63.  
      /**
    64.  
      * 跨域过滤器
    65.  
      *
    66.  
      * @return
    67.  
      */
    68.  
      @Bean
    69.  
      public CorsFilter corsFilter() {
    70.  
      UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    71.  
      source.registerCorsConfiguration("/**", buildConfig());
    72.  
      return new CorsFilter(source);
    73.  
      }
    74.  
       
    75.  
      private CorsConfiguration buildConfig() {
    76.  
      CorsConfiguration corsConfiguration = new CorsConfiguration();
    77.  
      corsConfiguration.addAllowedOrigin("*");
    78.  
      corsConfiguration.addAllowedHeader("*");
    79.  
      corsConfiguration.addAllowedMethod("*");
    80.  
      corsConfiguration.setMaxAge(3600L);
    81.  
      corsConfiguration.addExposedHeader("x-requested-with");
    82.  
      return corsConfiguration;
    83.  
      }
    84.  
       
    85.  
      }
  • 相关阅读:
    Spring简介和基础
    el表达式
    Spring MVC 文件上传下载
    el表达式的function标签
    JSTL核心标签库使用
    基于注解的SpringMVC
    new一个Object对象占用多少内存?
    leetcode-第k个排列(Java和c++版)
    使用maven构建web项目(简易版)
    leetcode-电话号码的字母组合
  • 原文地址:https://www.cnblogs.com/zhoading/p/13792044.html
Copyright © 2020-2023  润新知