博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CROS实现跨域时授权问题(401错误)的解决
阅读量:4079 次
发布时间:2019-05-25

本文共 2837 字,大约阅读时间需要 9 分钟。

http://www.open-open.com/lib/view/open1463878276539.html

如果我们访问的资源是不需要授权的,也就是在HTTP请求头中不包含 authentication 头那么以上做法就足够了。但是如果该资源是需要权限验证的,那么这个时候跨域请求的预检测 option 请求,由于不会携带身份信息而被拒绝 。浏览器会报出401错误。

前几天的文章  中提到了如何解决跨域问题的基本思路,解决了跨域请求时浏览器403错误。

Response to preflight request doesn't pass access control check:No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'null' is therefore not allowed access. The response had HTTP status code 403

401错误信息如下:

Failed to load resource:the server responded with a status of 401 (Unauthorized)XMLHttpRequest cannot load http://localhost/api/test.Response for preflight has invalid HTTP status code 401

既然知道了问题的原因,答案也就很容易得出: 对需要进行跨域请求的资源(api),当服务端检测到是 OPTONS 请求时候统统放行,给出HTTP.OK(200)的状态和必要的响应头,哪怕它是不带身份信息的 。

这个问题既可以通过编写对应的后端代码实现,也可以通过设置服务器配置文件实现。也就是如何设置响应头和返回200状态码的办法了。

Spring+Shrio的解决方案

shiro中可以在自己实现的身份验证filter中加入以下代码:

@Override  protected boolean preHandle(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {      HttpServletRequest request = (HttpServletRequest) servletRequest;      HttpServletResponse response = (HttpServletResponse) servletResponse;      if(request.getMethod().equals(RequestMethod.OPTIONS.name()))      {          response.setStatus(HttpStatus.OK.value());          return false;      }      return super.preHandle(request, response);  }

shiro中AccessControlFilter提供了访问控制的基础功能;比如是否允许访问/当访问拒绝时如何处理等,也是我们一般自定义权限验证时候的一个父类,我们通过重写他的 onPreHandle 方法判断是否是 option 请求,如果是则设置相应状态,(响应头已经在之前文章中通过filter配置过了)返回false表示该拦截器实例已经处理了,将直接返回即可。

Tomcat配置

需要修改tomcat的全局web.xml文件在 CATALINA_HOME/conf 下,加入以下配置。

CorsFilter
org.apache.catalina.filters.CorsFilter
CorsFilter
/*

Nginx配置

add_header 'Access-Control-Allow-Methods' 'GET,OPTIONS,PUT,DELETE' always;add_header 'Access-Control-Allow-Credentials' 'true' always;add_header 'Access-Control-Allow-Origin' '$http_origin' always;add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With' always;if ($request_method = OPTIONS ) {    return 200;}

Apache配置

Header always set Access-Control-Allow-Origin "http://waffle"Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS"Header always set Access-Control-Allow-Credentials "true"Header always set Access-Control-Allow-Headers "Authorization,DNT,User-Agent,Keep-Alive,Content-Type,accept,origin,X-Requested-With"RewriteCond %{REQUEST_METHOD} OPTIONSRewriteRule ^(.*)$ $1 [R=200,L]

js请求示例

请求时候需要加上 Authorization 和 Content-Type 头。

$http({  method: 'POST',  url: scope.webdav.url,  withCredentials: true,  headers: {    Authorization: 'Basic ' + btoa(user + ':' + password),    'Content-Type': 'application/vnd.google-earth.kml+xml; charset=utf-8'  },  data: getKml() })

转载地址:http://yzsni.baihongyu.com/

你可能感兴趣的文章
fastcgi_param 详解
查看>>
搞定Java面试中的数据结构问题
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
layui插件的使用
查看>>
9、VUE面经
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
Oracle 异机恢复
查看>>
Oracle 12C DG 搭建(RAC-RAC/RAC-单机)
查看>>
Truncate 表之恢复
查看>>