反应授权标头不起作用

时间:2018-01-28 10:31:14

标签: javascript reactjs

我目前正在使用oAuth身份验证,该功能在休息客户端正常运行。

Rest Client for Access Token

但在React Js中显示错误(401)。反应的代码是:

{{1}}

1 个答案:

答案 0 :(得分:1)

谢谢所有这个问题是cors。我在spring应用程序中添加了cors过滤器,并允许弹簧安全选项。

添加了新过滤器

 @Component
    @Order(3)
    public class CorsFilter implements Filter {

        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            final HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, PUT, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type");
            response.setHeader("Access-Control-Max-Age", "3600");
            if ("OPTIONS".equalsIgnoreCase(((HttpServletRequest) req).getMethod())) {
                response.setStatus(HttpServletResponse.SC_OK);
            } else {
                chain.doFilter(req, res);
            }
        }

        @Override
        public void destroy() {
        }

        @Override
        public void init(FilterConfig config) throws ServletException {
        }
    }

在Spring安全性中,我忽略了OPTIONS http方法

@Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers(HttpMethod.OPTIONS, "/oauth/token");
}