【发布时间】:2020-11-09 02:12:24
【问题描述】:
我有一个 oauth 服务器和一个使用 JWT 创建的资源服务器。
第一个按钮调用鉴权服务器,获取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 repo。
我尝试了几种方式添加 CORS:
资源休息服务上的配置较小,所以我将在此处概述
我尝试在HttpSecurity 上添加默认的.cors(),并在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) 公共类 SimpleCorsFilter 实现 Filter { @Override public void init(final FilterConfig filterConfig) throws ServletException {
} @Override public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throwsIOException, 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() { } }
只是无法让它工作。有人可以在这里给我一些指导吗?
【问题讨论】:
-
找到解决办法了吗?
-
@NafazBenzema 我做到了。请看下面我的回答。这只是一个拼写错误。看看你得到的错误。就我而言,我得到的 CORS 错误告诉我不允许授权。我从浏览器复制了标题并在我的代码中搜索它,但找不到它。然后我正确地查看了每个标签
标签: spring spring-boot spring-security oauth-2.0