我試圖用spring data rest解決一個CORS問題,但似乎CORS頭檔案沒有被附上。這是我的配置:
@Component
class DataRestConfig。RepositoryRestConfigurer {: DataRestConfig.
override fun configureRepositoryRestConfiguration(config: RepositoryRestConfiguration? , cors: CorsRegistry?) {
cors?.addMapping("/*"/span>)
?.allowedOrigins("*")
?. allowedMethods("GET"/span>, "PUT"/span>, "DELETE"/span>, "PATCH","POST","OPTIONS")
}
}
我在其他脫離spring data rest的API路由中也遇到了同樣的問題。下面是我的WebSecurityConfigurerAdapter的configure
override fun configure(http: HttpSecurity) {
http
.cors().and()
.csrf().disable()
.addFilterAfter(JWTAuthorizationFilter(userDetailsServices, secret, jwtService), UsernamePasswordAuthenticationFilter::class.java)
.authorizeRequests()
.antMatchers(HttpMethod.POST,UserController.LOGIN_URL) .permitAll()
.anyRequest().authenticated()
}
uj5u.com熱心網友回復:
如果使用Spring MVC,你應該像這樣配置CORS行為
。
public class CorsConfiguration implements WebMvcConfigurer {
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**"/span>)
.allowedOrigins("http://localhost:3000")
. .allowedMethods("GET"/span>, "POST"/span>, "PUT"/span>。"DELETE", "PATCH", "OPTIONS")。)
}
}
uj5u.com熱心網友回復:
你總是可以有一個CorsFilter來修改回應頭檔案。在這里,我已經回答了我們如何在Spring boot中擁有自定義的CorsFilter - https://stackoverflow.com/a/66882700/3709922。請看一下。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/308783.html
標籤:
下一篇:翻轉或旋轉一維陣列
