当前位置:首页 > 技术文档 > 正文内容

CROS实现跨域时授权问题(401错误)的解决

admin7年前 (2018-04-16)技术文档1766

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 下,加入以下配置。

<filter>
       <filter-name>CorsFilter</filter-name>
       <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
     </filter>
     <filter-mapping>
       <filter-name>CorsFilter</filter-name>
       <url-pattern>/*</url-pattern>
     </filter-mapping>

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} OPTIONS
RewriteRule ^(.*)$ $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()

})


分享到:

扫描二维码推送至手机访问。

版权声明:本文由云河空间发布,如需转载请注明出处。

本文链接:https://yuyunhe.cn/index.php/post/255.html

分享给朋友:

“CROS实现跨域时授权问题(401错误)的解决” 的相关文章

网页背景全屏拉伸的css效果

background: url(“http://www.wyzu.cn/uploadfile/2013/0220/20130220112423140.jpg”) repeat fixed center top / cover #595959;...

php 获取页面内容

function get_contents($url){     if(function_exists('file_get_contents')){       &nbs...

PHP分页函数仿Google分页

/**  * 分页函数  * @param int $total    总页数  * @param int $pagesize 每页几条 &n...

php获取从百度搜索进入网站的关键词

<?php    function search_word_from() {     $referer = isset($_SERVER['HTTP_REFERER'])?...

php 获取客户端的ip、地理信息、浏览器信息、本地真实ip

<?php  // 作用取得客户端的ip、地理信息、浏览器http://blog.qita.in  class get_gust_info {        ////获得访客浏...

用php gettext库来开发多语言系统

用php gettext库来开发多语言系统

通常人们写程序时都是将文字写死在程序里的, 比如:echo "Hello World!";  ,假如要改成它国语言,写国际化程序,就要逐个打开进行修改,程序较短时还行,若程序有上万甚至更多,改起来就不是那么容易了。近来随着i18n的逐渐标 准化,我也来讲一讲在PHP中如...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。