Java Code Examples for org.springframework.web.servlet.config.annotation.WebMvcConfigurer

The following examples show how to use org.springframework.web.servlet.config.annotation.WebMvcConfigurer. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: mogu_blog_v2   Source File: AdminApplication.java    License: Apache License 2.0 6 votes vote down vote up
/**
     * 跨域过滤器
     *
     * @return
     */
//    @Bean
//    public CorsFilter corsFilter() {
//        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//        source.registerCorsConfiguration("/**", buildConfig());
//        return new CorsFilter(source);
//    }

    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                //配置允许跨域访问的路径
                registry.addMapping("/**/**")
                        .allowedOrigins("*")
                        .allowedMethods("*")
                        .allowedHeaders("*")
                        .allowCredentials(true)
                        .exposedHeaders("")
                        .maxAge(3600);
            }
        };
    }
 
Example 2
Source Project: n2o-framework   Source File: DemoApplication.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
    public WebMvcConfigurer forwardToIndex() {
        return new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                // forward requests to /admin and /user to their index.html
                registry.addViewController("/docs/manual/").setViewName(
                        "forward:/docs/manual/index.html");
                registry.addViewController("/docs/xml/").setViewName(
                        "forward:/docs/xml/index.html");
                registry.addViewController("/docs/storybook/").setViewName(
                        "forward:/docs/storybook/index.html");
                registry.addViewController("/docs/esdoc/").setViewName(
                        "forward:/docs/esdoc/index.html");

            }

//            @Override
//            public void addResourceHandlers(ResourceHandlerRegistry registry) {
//                registry.addResourceHandler("/{x:^(?!docs$).*$}/**")
//                        .addResourceLocations("classpath:/META-INF/resources/")
//                        .resourceChain(true)
//                        .addResolver(new SPAResolver());
//            }
        };
    }
 
Example 3
@Test
public void testConfigureContentNegotiationOnDefaultValues() {

    WebMvcConfigurer webMvcConfigurer =
            new ConfigurableContentNegotiationManagerWebMvcConfigurer(new HashMap<String, String>());

    ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(new MockServletContext());

    webMvcConfigurer.configureContentNegotiation(configurer);

    ContentNegotiationManagerFactoryBean factoryBean =
            FieldUtils.getFieldValue(configurer, "factory", ContentNegotiationManagerFactoryBean.class);

    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "favorPathExtension", boolean.class));
    Assert.assertFalse(FieldUtils.getFieldValue(factoryBean, "favorParameter", boolean.class));
    Assert.assertFalse(FieldUtils.getFieldValue(factoryBean, "ignoreAcceptHeader", boolean.class));
    Assert.assertNull(FieldUtils.getFieldValue(factoryBean, "useJaf", Boolean.class));
    Assert.assertEquals("format", FieldUtils.getFieldValue(factoryBean, "parameterName", String.class));
    Assert.assertTrue(FieldUtils.getFieldValue(factoryBean, "mediaTypes", Map.class).isEmpty());
    Assert.assertNull(FieldUtils.getFieldValue(factoryBean, "defaultContentType", MediaType.class));

}
 
Example 4
Source Project: basic   Source File: Application.java    License: MIT License 6 votes vote down vote up
@Bean
public WebMvcConfigurer webMvcConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addViewControllers(ViewControllerRegistry viewControllerRegistry) {

            // 首页默认加载web端
            viewControllerRegistry.addViewController("/").setViewName("/web/index.html");
            viewControllerRegistry.addViewController("/index.html").setViewName("redirect:/");

            // web首页
            viewControllerRegistry.addViewController("/web").setViewName("/web/index.html");

            // webapp首页
            viewControllerRegistry.addViewController("/webapp").setViewName("/webapp/index.html");

            viewControllerRegistry.setOrder(Ordered.HIGHEST_PRECEDENCE);
            super.addViewControllers(viewControllerRegistry);
        }
    };
}
 
Example 5
@Bean
@ConditionalOnMissingBean(TracingFilter.class)
public WebMvcConfigurer tracingHandlerInterceptor(final Tracer tracer) {
    log.info("Creating " + WebMvcConfigurer.class.getSimpleName() + " bean with " +
            TracingHandlerInterceptor.class);

    return new WebMvcConfigurer() {
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            List<HandlerInterceptorSpanDecorator> decorators = interceptorSpanDecorator.getIfAvailable();
            if (CollectionUtils.isEmpty(decorators)) {
                decorators = Arrays.asList(HandlerInterceptorSpanDecorator.STANDARD_LOGS,
                        HandlerInterceptorSpanDecorator.HANDLER_METHOD_OPERATION_NAME);
            }

            registry.addInterceptor(new TracingHandlerInterceptor(tracer, decorators));
        }
    };
}
 
Example 6
Source Project: syndesis   Source File: Application.java    License: Apache License 2.0 6 votes vote down vote up
@Bean
public WebMvcConfigurer staticResourceConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry
                .addResourceHandler("/mapper/**")
                .addResourceLocations(
                    "classpath:/META-INF/syndesis/mapper/",
                    "classpath:/META-INF/resources/mapper/",
                    "classpath:/static/mapper/",
                    "classpath:/resources/mapper/"
                );
        }
    };
}
 
Example 7
Source Project: data-highway   Source File: LdapSecurityConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer webConfig() {
  return new WebMvcConfigurer() {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
      registry.addMapping("/**").allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
    }
  };
}
 
Example 8
/** Configure async support for Spring MVC. */
@Bean
public WebMvcConfigurer webMvcConfigurerConfigurer(AsyncTaskExecutor taskExecutor, CallableProcessingInterceptor callableProcessingInterceptor) {
    return new WebMvcConfigurer() {
        @Override
        public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
            configurer.setDefaultTimeout(360000).setTaskExecutor(taskExecutor);
            configurer.registerCallableInterceptors(callableProcessingInterceptor);
            WebMvcConfigurer.super.configureAsyncSupport(configurer);
        }
    };
}
 
Example 9
Source Project: cloud-service   Source File: LocalFilePathConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public WebMvcConfigurer webMvcConfigurerAdapter() {
	return new WebMvcConfigurer() {

		/**
		 * 外部文件访问<br>
		 */
		@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
			registry.addResourceHandler(localFilePrefix + "/**")
					.addResourceLocations(ResourceUtils.FILE_URL_PREFIX + localFilePath + File.separator);
		}

	};
}
 
Example 10
@Bean
public WebMvcConfigurer corsConfigurer() {
	return new WebMvcConfigurer() {
		@Override
		public void addCorsMappings(CorsRegistry registry) {
			registry.addMapping("/**").allowedMethods("*").allowedOrigins("*");
		}
	};
}
 
Example 11
@Bean
public WebMvcConfigurer corsConfigurer() {
	return new WebMvcConfigurer() {
		@Override
		public void addCorsMappings(CorsRegistry registry) {
			CorsRegistration reg = registry.addMapping("/api/**");
			reg.allowedOrigins("*");
		}
	};
}
 
Example 12
Source Project: web-flash   Source File: CORSConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedHeaders("*")
                    .allowedMethods("*")
                    .allowedOrigins("*");
        }
    };
}
 
Example 13
Source Project: WeEvent   Source File: BrokerApplication.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer webMvcConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedHeaders("*")
                    .allowedMethods("*")
                    .allowedOrigins("*");
        }
    };
}
 
Example 14
Source Project: arcusipcd   Source File: Ap.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/v1/network").allowedMethods("PUT, OPTIONS");
        }
    };
}
 
Example 15
Source Project: grpc-swagger   Source File: WebConfig.java    License: MIT License 5 votes vote down vote up
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedHeaders("*")
                    .allowedMethods("*")
                    .allowedOrigins("*");
        }
    };
}
 
Example 16
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedMethods("GET", "POST", "OPTIONS")
                    .allowedOrigins("*");
        }
    };
}
 
Example 17
/**
 * 注册 拦截器
 *
 * @param registry
 */
@Override
public void addInterceptors(InterceptorRegistry registry) {
    String[] commonPathPatterns = getExcludeCommonPathPatterns();
    registry.addInterceptor(getTokenHandlerInterceptor())
            .addPathPatterns("/**")
            .order(5)
            .excludePathPatterns(commonPathPatterns);
    WebMvcConfigurer.super.addInterceptors(registry);
}
 
Example 18
@Bean
public WebMvcConfigurer corsConfigurer() {
	return new WebMvcConfigurer() {
		@Override
		public void addCorsMappings(CorsRegistry registry) {
			registry.addMapping("/**").allowedMethods("*").allowedOrigins("*");
		}
	};
}
 
Example 19
Source Project: java-interview   Source File: MyConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            // 设置允许跨域的请求规则
            registry.addMapping("/api/**");
        }
    };
}
 
Example 20
Source Project: springBoot   Source File: MyConfiguration.java    License: MIT License 5 votes vote down vote up
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/cors/corsByConfig")
                    .allowedOrigins("https://www.cnblogs.com")
                    .allowedMethods("POST")
                    .allowedHeaders("*")
                    .allowCredentials(true).maxAge(3600);
        }
    };
}
 
Example 21
@Bean
public WebMvcConfigurer corsConfiger(){
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("*")
                    .allowCredentials(true);
            super.addCorsMappings(registry);
        }
    };
}
 
Example 22
Source Project: AnyMock   Source File: WebMvcConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer crossOriginConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping(URL_PREFIX_API_V2_PATTERN);
        }
    };
}
 
Example 23
Source Project: AnyMock   Source File: WebMvcConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer crossOriginConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**/*");
        }
    };
}
 
Example 24
Source Project: flink-statefun   Source File: WebSocketConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer corsConfigurer() {
  return new WebMvcConfigurerAdapter() {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
      registry.addMapping("/**").allowedMethods("HEAD", "GET", "PUT", "POST", "DELETE", "PATCH");
    }
  };
}
 
Example 25
Source Project: SpringBoot-Home   Source File: CorsConfig.java    License: Apache License 2.0 5 votes vote down vote up
public WebMvcConfigurer corsConfigurer()
{
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").
                    allowedOrigins("https://www.dustyblog.cn"). //允许跨域的域名,可以用*表示允许任何域名使用
                    allowedMethods("*"). //允许任何方法(post、get等)
              allowedHeaders("*"). //允许任何请求头
                    allowCredentials(true). //带上cookie信息
                    exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
        }
    };
}
 
Example 26
Source Project: tac   Source File: ContainerApplication.java    License: MIT License 5 votes vote down vote up
@Bean
public WebMvcConfigurer webMvcConfigurer() {
    return new WebMvcConfigurerAdapter() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/api/**");
        }
    };
}
 
Example 27
Source Project: openapi-generator   Source File: OpenAPI2SpringBoot.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer corsConfigurer() {
    return new WebMvcConfigurer() {
        @Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**").allowedOrigins("*").allowedHeaders("Content-Type");
        }
    };
}
 
Example 28
Source Project: openapi-generator   Source File: OpenAPI2SpringBoot.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer webConfigurer() {
    return new WebMvcConfigurer() {
        /*@Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("Content-Type");
        }*/
    };
}
 
Example 29
Source Project: openapi-generator   Source File: OpenAPI2SpringBoot.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer webConfigurer() {
    return new WebMvcConfigurerAdapter() {
        /*@Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("Content-Type");
        }*/
    };
}
 
Example 30
Source Project: openapi-generator   Source File: OpenAPI2SpringBoot.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public WebMvcConfigurer webConfigurer() {
    return new WebMvcConfigurer() {
        /*@Override
        public void addCorsMappings(CorsRegistry registry) {
            registry.addMapping("/**")
                    .allowedOrigins("*")
                    .allowedMethods("*")
                    .allowedHeaders("Content-Type");
        }*/
    };
}