【发布时间】:2019-11-16 23:30:09
【问题描述】:
我使用以下技术: -Java 8 -Spring(启动 2,安全性) -反应,axios -雄猫 9 -IntelijIdea
在我的项目中,我使用以下 http 方法:GET、POST、PUT、PATCH 等。我有 CORS 配置。在开发环境中运行我的项目时,或者在 Postman -> CORS 中测试方法时效果很好。但是,当我收集 war 文件并在 Tomcat 服务器上运行它时,PATCH 方法拒绝工作。 (错误:预检响应中的访问控制允许方法不允许方法路径)。请帮忙,修复它。
安全配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.headers().frameOptions().sameOrigin()
.and()
.authorizeRequests()
.antMatchers("/",
"/favicon.ico",
"/**/*.png",
"/**/*.gif",
"/**/*.svg",
"/**/*.jpg",
"/**/*.html",
"/**/*.css",
"/**/*.js",
"/static/**",
"/static/css/*",
"/static/js/*",
"/*.js",
"/*.json",
"/*.ico"
).permitAll()
.antMatchers(SIGN_UP_URLS).permitAll()
.antMatchers(H2_URL).permitAll()
.anyRequest().authenticated()
;
http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}
CORS 配置:
@Bean
public CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("HEAD",
"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"));
configuration.setAllowCredentials(true);
configuration.setAllowedHeaders(Arrays.asList("*"));
configuration.setExposedHeaders(Arrays.asList("X-Auth-Token","Authorization","Access-Control-Allow-Origin","Access-Control-Allow-Credentials"));
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
【问题讨论】:
-
如果您将现代技术用于其他一切,为什么还要部署到外部 Tomcat 中?
-
除了PATCH之外,你能执行其他方法吗?
-
什么意思? @chrylis
-
@PatelRomil 其他方法效果很好。
-
对新项目使用war文件已经过时了; Spring Boot 为您处理所有这些,而这类事情正是 Boot 的内部支持使问题变得更容易。
标签: java spring-boot spring-security cors axios