spring - 如何在 Spring Boot 2.x 中将 CORS 添加到 outh2/resource 服务器?
问题描述
我有一个使用 JWT 创建的 oauth 服务器和一个资源服务器。
第一个按钮调用认证服务器并获取 JWT 令牌并将其添加到输入框中。
第二个按钮使用 JWT 令牌作为不记名授权 http 标头调用其余服务器。
从 PostMan 调用 2 个服务可以完美运行,但我无法为后端服务正确配置 CORS 设置。
两个按钮都给我以下错误:
Access to XMLHttpRequest at 'http://localhost:8085/oauth/token' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.
我将所有 3 个项目添加到我的公共 github 存储库中。
我尝试通过多种方式添加 CORS:
资源休息服务上的配置较小,所以我将概述在这里我尝试在方法中添加默认值.cors()
以及HttpSecurity
手动设置它corsConfigurationSource()
。
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(final HttpSecurity http) throws Exception {
http.csrf().disable()
.cors()
.and().authorizeRequests().anyRequest().authenticated()
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
//I tried manually configured the cors as well
/*http.csrf().disable()
.cors().configurationSource(corsConfigurationSource())
.and().authorizeRequests().anyRequest().authenticated();
.and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);*/
}
/* @Bean
CorsConfigurationSource corsConfigurationSource() {
final CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(Arrays.asList("*"));
configuration.setAllowedMethods(Arrays.asList("GET", "POST"));
configuration.setAllowCredentials(true);
//the below three lines will add the relevant CORS response headers
configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.addAllowedMethod("*");
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
*/
}
我还尝试添加一个 servlet 过滤器
@Component @Order(Ordered.HIGHEST_PRECEDENCE) public class SimpleCorsFilter 实现 Filter { @Override public void init(final FilterConfig filterConfig) throws ServletException {
} @Override public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws
IOException, ServletException {
final HttpServletResponse response = (HttpServletResponse) servletResponse; final HttpServletRequest request = (HttpServletRequest) servletRequest; response.addHeader("Access-Control-Allow-Origin", "*"); response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS"); response.setHeader("Access-Control-Max-Age", "3600"); response.setHeader("Access-Control-Allow-Headers", "content-type, x-requested-with, authorisation"); if ("OPTIONS".equalsIgnoreCase(request.getMethod())) { response.setStatus(HttpServletResponse.SC_OK); } else { filterChain.doFilter(servletRequest, servletResponse); } } @Override public void destroy() { } }
只是无法让它工作。任何人都可以在这里给我一些指导吗?
解决方案
愚蠢的错误,因为在我的两个 SimpleCorsFilter.java 文件中,我都指定了authorisation
允许标头标签,但不是 S 授权,而是 Z 授权。
更改我的配置服务器中的两个文件
response.setHeader("Access-Control-Allow-Headers", "content-type, x-requested-with, Authorization");
推荐阅读
- pytorch - 在 Colab 上运行 TensorBoard
- python - 如何使用 Python Turtle 绘制一个点在顶部的七边形?
- javascript - 显示截断字符串的显示值
- arrays - 恐慌:json:如何在 Golang 中解组嵌套的 json 数组
- angular - 角材料步进样式
- python - 使用 QuantLib Python 为远期利率协议定价
- time - 想要特定时间的一部分
- spring - 是否可以在 Spring 中将 @Unwrapped 与自定义 mongodb 转换器结合使用?
- android-mediaplayer - 我如何处理媒体播放器通知中的搜索?
- java - Kafka RocksDB 不断增长的磁盘大小