Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 飞行前响应的oauth/token的HTTP状态代码401无效_Java_Angularjs_Spring_Spring Boot_Oauth 2.0 - Fatal编程技术网

Java 飞行前响应的oauth/token的HTTP状态代码401无效

Java 飞行前响应的oauth/token的HTTP状态代码401无效,java,angularjs,spring,spring-boot,oauth-2.0,Java,Angularjs,Spring,Spring Boot,Oauth 2.0,我在spring boot中为CORS实现了过滤器,代码如下:- @SpringBootApplication @Component public class Application implements Filter { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void init(FilterConfig fi

我在spring boot中为CORS实现了过滤器,代码如下:-

@SpringBootApplication
@Component
public class Application implements Filter {
public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Credentials", "true");
    response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age", "3600");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

    filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() {

}
}
为了从oauth2获取访问令牌,我从angularjs创建了以下对象:-

 {"url":"http://localhost:8080/oauth/token?grant_type=client_credentials&client_id=clientId&client_secret=clientSecret","headers":{"Authorization":"Basic token_value"},"withCredentials":true,"method":"POST"}
当我点击服务器时,出现以下错误:-

OPTIONS url... 401() Response for preflight has invalid HTTP status code 401
我已经为堆栈溢出中的类似问题寻找了解决方案,但没有一个解决了我的问题。有人能帮我解决这个问题吗?

请阅读更多信息

如果服务器支持跨源请求,他们只建议浏览器。对此类
选项
请求的响应应良好(即<400)

我认为语句
filterChain.doFilter(servletRequest,servletResponse)
正在进一步传递请求,而不是返回响应


您可以在这里阅读有关在Java中使用Spring启用CORS的更多信息

下面的一段代码解决了我的问题

@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.addAllowedOrigin("*");
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(Ordered.HIGHEST_PRECEDENCE);
    return bean;
}

代码示例取自

我也尝试了中提供的解决方案,但没有任何帮助