大佬教程收集整理的这篇文章主要介绍了oauth / token上的Spring Boot Rest Service选项401,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
您可以将此CORS过滤器添加到您的项目中
@Component
@Order(ordered.HIGHEST_PRECEDENCE)
public class SimpleCORSFilter implements Filter {
@OverrIDe
public voID init(FilterConfig fC) throws servletexception {
}
@OverrIDe
public voID doFilter(Servletrequest req, ServletResponse resp,
FilterChain chain) throws IOException, servletexception {
httpServletResponse response = (httpServletResponsE) resp;
httpServletrequest request = (httpServletrequest) req;
response.setheader("Access-Control-Allow-Origin", "*");
response.setheader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, deletE");
response.setheader("Access-Control-Max-Age", "3600");
response.setheader("Access-Control-Allow-headers", "x-requested-with, authorization, Content-Type, Authorization, credential, X-XSRF-TOKEN");
if ("OPTIONS".equalsIgnoreCase(request.getmethod())) {
response.setStatus(httpServletResponse.SC_OK);
} else {
chain.doFilter(req, resp);
}
}
@OverrIDe
public voID destroy() {
}
}
我正在使用Spring Boot进行简单的休息服务。为了在Angular 2中使用它,我在oauth/token端点上检索令牌时遇到了CORS问题。
Chrome中的错误消息如下。
错误信息
zone.js:101 OPTIONS http://192.168.0.9:8080/api/oauth/token
XMLhttprequest cAnnot load http://192.168.0.9:8080/api/oauth/token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had http status code 401.
相关文件如下。
@H_292_2@myConfig.java@Configuration
public class MyConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurerAdapter() {
@Override
public void addCorsmappings(CorsRegistry registry) {
registry.addMapping("**")
.allowedOrigins("*").allowedMethods("POST,GET,HEAD,OPTIONS")
.allowCredentials(true)
.allowedHeaders("Content-Type,X-requested-With,accept,Origin,Access-Control-request-Method,Access-Control-request-Headers")
.exposedHeaders("Access-Control-Allow-Origin,Access-Control-Allow-Credentials")
.maxAge(10);
}
};
}
}
OAuth2resourceServerConfig.java
@Configuration
@EnableresourceServer
class OAuth2resourceServerConfig extends resourceServerConfigurerAdapter {
@Override
public void configure(httpSecurity http) throws Exception {
http
.csrf().disable()
.anonymous()
.and()
.authorizerequests()
.antMatchers(httpR_75_11845@ethod.oPTIONS,"**").permitAll()
.antMatchers("/authenticated/**").authenticated()
;
}
}
请注意,正常的休息控制器端点工作正常。问题是oauth / token,选项请求返回401状态。
请给我看一些Spring Boot时的工作代码。谢谢!
以上是大佬教程为你收集整理的oauth / token上的Spring Boot Rest Service选项401全部内容,希望文章能够帮你解决oauth / token上的Spring Boot Rest Service选项401所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。