2.0.2 • Published 2 years ago

user-auth-middle v2.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

INSTALL

yarn add @iwhale/user-auth-middle --registry=http://10.45.53.230:8084/nexus/repository/cb-npm

USAGE

1.中间件配置 edit your config located in $WORKDIR/config/config_xxx.js

...
    middleware:{
        iwhaleUserAuth:{
            enable:true,//是否启用该中间件
            module:'@iwhale/user-auth-middle',//中间件包 
            config:{
                clientId:'tic', // * secret id
                clientSecret:'328bcdb839eb9d6492855778666f691f', //* secret key
                cookieName:'honeycomb_token', // * saved cookie name
                apiAuth:['/api/proxy'], // path need to add token,support url regex
                ignore: ['/login'],// ignore path,support url regex
                redirectHost:'127.0.0.1:8001',// redirect host,default:req.get('host')
                casServerUrl:'http://10.45.70.55:8380/sso', // * for browser request
                localCasServerUrl:'http://10.45.70.55:8380/sso', // for node server request
                clientUrl:'http://10.45.70.55:8380/sso/oauth2.0', // <=v1.0.5(*) server url 
                logoutUrl: 'http://10.45.70.55:8380/sso/logout', // <=v1.0.5(*) path to logout(del req.session.user) 
                loginUrl:'http://10.45.70.55:8380/sso/oauth2.0/authorize',//path to login
                tokenUrl:'http://10.45.70.55:8380/sso/oauth2.0/accessToken',//path to get token
                profileUrl:'http://10.45.70.55:8380/sso/oauth2.0/profile'//path to get profile
            }
        },
        spa:{//honeycomb项目加一下这个,代表此中间件在spa中间件之前运行
            deps:"iwhaleUserAuth"
        }
    },
    extension: {
        redirect: {
            config: {
                allowDomains: [//添加下上方服务地址,如果不在同一个域名下的话
                    '10.45.70.55:8380'
                ]
            }
        },
    },

    注意:
      如oauth配置给出的授权地址为 'http://ip:port/sso/oauth2.0/authorize?client_id=key&redirect_uri=url&response_type=type'
      
      则相应配置对应如下
      config:{
        casServerUrl:'http://ip:port/sso',
        localCasServerUrl:'http://ip:port/sso'

        clientUrl:'http://ip:port/sso/oauth2.0',

        loginUrl:'http://ip:port/sso/oauth2.0/authorize',
        logoutUrl: 'http://ip:port/sso/logout', 
        tokenUrl:'http://ip:port/sso/oauth2.0/accessToken',
        profileUrl:'http://ip:port/sso/oauth2.0/profile'
      }
      实际使用中不需要配置如此多的,以上基本上以某一组的形式出现:
        目前支持形式为 :
          1. loginUrl、logoutUrl、tokenUrl、profileUrl
          2. casServerUrl外网、localCasServerUrl 内网,如果内外网一致,仅配置casServerUrl即可
          3. clientUrl、logoutUrl v1.0.5版本及以下必须走此配置
          配置优先级:loginUrl、logoutUrl、tokenUrl、profileUrl (每个可单独配优先级高于后面两个组装的) > casServerUrl、localCasServerUrl(如果内外网一致 可以只配置casServerUrl) > clientUrl
...

2.用户信息session获取

...
  登录成功后,中间件会获取用户数据,存储到session,变量名为user,如果登出或者token完全过期会重新获取。
  获取方式:【honeycomb框架配置可参考】
  已有中间件middleware/spa.js可做如下配置
  return (req, res, next) => {
      let userInfo = req.session.user;

      res.render('index.html'),{
        ...
        userInfo: escape(JSON.stringify(userInfo || {})),
        ...
      };
  }

  view/index.html中配置
  <script>
    window.userInfo = JSON.parse(unescape("<%= userInfo %>"));//全局配置user信息 
  </script>
...

3.api操作

3.1 支持token功能,需做以下处理

...
  const proxyInstance = new Proxy({
    service: {
      test:{//可修改:此处设置为test对应api为'/api/proxy/test' 配置到上方apiAuth中
        endpoint:"http://10.45.70.56:8889/tocc-platform-service-oauth",//可修改
        client: 'http',
        headerExtension: [
          function (req, serviceCfg) {
            return {
              'access_token':req.get('access_token')
            };
          }
        ],
        api:['/*']
      },
    })
...

3.2 api请求时,token过期处理

node层直接返回数据(object): { isError:true, status: -10000, message: 'login overtime',isLogin:false }

应用侧针对返回数据做处理,同登出操作即可 例 window.location.href=http://127.0.0.1:8080/ABC/logout;

4.登出功能支持,请做以下处理

...
  window.location.href=`http://127.0.0.1:8080/ABC/logout`;//地址重定向方式
  以上两处均需要注意 
  如果是拿http://127.0.0.1:8080/ABC 为系统路径
  则登出地址一定是基于http://127.0.0.1:8080/ABC 为基础
  比如
  http://127.0.0.1:8080/ABC/logout
  http://127.0.0.1:8080/ABC/logout/xxx
  http://127.0.0.1:8080/ABC/xxx/logout/xxx
...
···
  
服务端获取到
	1.access_token 
	2.expires 过期时间(比较短可能几分钟)
	3.refresh_token (服务端设定 已知是30天)
	
后续
	access_token过期
	通过refresh_token 获取新的 access_token 和 expires
	
浏览器端可以查看到的cookie信息
honeycomb_token 对应上方access_token
HC_THIRD_AUTH	对应上方access_token,带过期时间 设定为上方expires
refresh_token	对应上方refresh_token
...
2.0.2

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago