对于预检,响应具有无效的HTTP状态代码401,用于oauth / token [英] Response for preflight has invalid HTTP status code 401 for oauth/token

查看:627
本文介绍了对于预检,响应具有无效的HTTP状态代码401,用于oauth / token的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在spring boot中为CORS实现了Filter。代码如下: -

I have implemented Filter for CORS in spring boot.The code is as follow:-

@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获取access_token,我从angularjs创建了以下对象: -

To get the access_token from oauth2 I have created following object from 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"}

我正在关注当我点击服务器时出错: -

I am getting following error when I hit the server:-

OPTIONS url... 401() Response for preflight has invalid HTTP status code 401

我已经找到了堆栈溢出中类似问题的解决方案,但没有一个解决我的问题。可能有人请帮忙吗?

I have looked for the solutions for similar problems in stack overflow but none of them fixed my problem.Could someone please help on this?

推荐答案

请阅读更多关于预检请求s

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

They simply suggest the browser if the server supports a cross-origin request. The response to such OPTIONS requests should good (i.e. < 400).

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

I think the statement filterChain.doFilter(servletRequest, servletResponse); is passing the request further, instead of returning a response.

您可以阅读更多关于在Java中使用Spring启用CORS 使用Spring Framework为OPTIONS请求启用CORS

You can read more about enabling CORS using Spring in Java here Enable CORS for OPTIONS request using Spring Framework

这篇关于对于预检,响应具有无效的HTTP状态代码401,用于oauth / token的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆