美文网首页
认证方式解决方案

认证方式解决方案

作者: 墨__神 | 来源:发表于2019-11-21 16:54 被阅读0次
参考文章:
     - http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html

一、代理方式

server {
        listen 80;
        server_name 127.0.0.1;    # 这是外网访问进来时的连接地址
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
        location ~/app {
                proxy_pass http://***.***.***.***:5050;
                proxy_cookie_path /app /;
                proxy_set_header Host $host;
                proxy_set_header   Remote_Addr    $remote_addr;
                proxy_set_header   X-Real-IP    $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_set_header   Authorization  $http_authorization;
                proxy_pass_header  Authorization;
                client_max_body_size 200m;
                proxy_connect_timeout 18000;
                proxy_send_timeout 18000;
                proxy_read_timeout 18000;
        }

}

二、auth2.0
可以支持各种各样登陆,是一种授权认证方式


111.jpg

三、jwt
JSONWebtoken(JWT)是一种开放标准(RFC7519),它定义了一种紧凑的、自包含的方式,用于作为JSON对象在各方之间安全地传输信息。可以验证和信任此信息,因为它是数字签名的。JWT可以使用密钥(使用HMAC算法)或使用RSA或ECDSA的公钥/私钥对进行签名。
虽然可以对JWT进行加密以在各方之间提内容的安全,但实际上我们仅仅将关注重点放到已签名的令牌。签名的令牌可以验证其中包含的声明的完整性,而加密的令牌则对其他方隐藏这些声明。当使用公钥/私钥对签名令牌时,签名还证明只有持有私钥的一方才是签名方

112.jpeg

四、session+jwt双重认证
jwt认证判断逻辑
第一种逻辑
// 判断header里面有没有jwt
// 如果没有,判断url里面有没有
// 如果没有,判断localStorage里面有没有
// 如果没有,走老逻辑

// 读
const authorization=(response)=>{
   if(getQueryString('authorization')){
    // 从url获取值
    return getQueryString('authorization')
    
  }else if(localStorage.getItem('authorization')){
    // 从localStorage里获取
    return localStorage.getItem('authorization')
    
  }
  return null;
}
// 写
if(response.headers.has('authorization')){
      localStorage.setItem('authorization',response.headers.get('authorization'));
      console.log(response.headers.get('authorization'))
}

第二种逻辑
// 找到jwt,登陆是否超时
// 如果超时期望弹出登陆控件重新登陆

方式:
header:'authorization': token //常规方式
备注:header的key按照w3规范不会区别大小写,有些框架比如(fetch会全部转成小写)
https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2
https://github.com/github/fetch/issues/249

url: ?authorization=token //后端跳转兼容认证(跨域)

localStorage:setItem('authorization',token)//前端把jwt设置到本地
localStorage:getItem('authorization')//调用接口使用

HMACSHA256(
base64UrlEncode({
"alg": "HS256",
"typ": "JWT"
}) + "." +
base64UrlEncode({authToken=, iss=APP-PORTAL, sessionId=, exp=1558939016, iat=1558938016, jti=4c034691-b9ca-4443-8401-578141cb8768}) + ".",
签名
)

1、登陆->后端生成jwt串放到header里->前端保存在localStorage->前端请求接口->把值放到header->请求数据->返回数据(有效)/?重新登陆(静默/弹出登陆组件)?返回入口页面
ge->前端请求接口->把值放到header->请求数据->返回数据(有效)/?重新登陆(静默/弹出登陆组件)?返回入口页面

相关文章

网友评论

      本文标题:认证方式解决方案

      本文链接:https://www.haomeiwen.com/subject/ehvtwctx.html