1.1.5 • Published 2 years ago

uaa-core-api v1.1.5

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

uaa-core-api

统一认证

Browser Support

> 1%
last 2 versions
not ie <= 8

Installing

Using npm:

npm install uaa-core-api

use

import uaa from 'uaa-core-api'
uaa.initServer({
  uaaServer: '', // uaa认证服务地址
  userServer: '', // user-center
  clientId: '', // uaa服务注册的clientId
  clientSecret: '',// uaa服务注册的clientSecret
  grantType: '',  // 授权方式
  scope: '' // 授权范围
})
uaa.checkLogin().then(res => {
  const userInfo = res
}).catch(() => {
  uaa.login(redirectUri)
})

API

uaa对象提供一组方法

  • uaa.initServer(options) 详细配置查看下方options说明

  • uaa.checkLogin() return promise,resolve(userInfo);reject(Error('未登录'))

  • uaa.getToken() return token,type: string;e.g: 'Bearer decf0911-cbc9-48ab-ac5b-1d6aac25ecdc'

  • uaa.getAccessToken() return access_token, type: string, e.g: decf0911-cbc9-48ab-ac5b-1d6aac25ecdc (和token的不同是不带有前缀“Bearer ”)

  • uaa.getUserInfo() return userInfo,type: Object

  • uaa.login(redirectUri) return promise,resolve(userInfo),reject(Error);redirectUri: 授权重定向地址,重定向地址必须与数据库配置保持一致

  • uaa.logout(redirectUri) redirectUri: 登出后重定向的地址,重定向地址必须与数据库配置保持一致

options

  • options.uaaServer: uaa认证服务地址

  • options.userServer: userCenter服务地址

  • options.clientId: uaa注册的clientId

  • option.clientSecret: uaa 注册的clientSecret

  • options.grant_type: 授权方式(password | authorization_code | implicit | client_credentials | openId);默认值: 'authorization_code'

  • options.scope: 授权范围,数据库配置的scope, 默认值: 'all'; 该参数只对grant_type值是authorization_code 或 implicit 生效

  • options.customUaaServer: 选填, 自定义认证授权服务,在统一认证后,需要再自定义应用自身的认证授权方案时用到。

  • options.customUserServer: 选填,自定义认证授权服务对应的用户服务, 获取应用自身的用户信息,不传则获取统一认证的用户信息。

  • options.customClientId: 选填, 默认值: lcdp

  • options.customClientSecret: 选填, 默认值: lcdp

  • options.customGrantType: 选填, 默认: mg

Example

  1. step1 路由守卫中检测是否登录,处理登录后流程
import uaa from 'uaa-core-api'
const initSystem = (userInfo, to, next) => {
  store.commit('user/setUserInfo', userInfo)
  const cacheRoutePath = getCacheRoutePath()
  if (cacheRoutePath) {
    removeCacheRoutePath()
    next({path: cacheRoutePath, replace: true})
  } else {
    next({...to, replace: true})
  }
}
const setCacheRoutePath = (path) => {
  if (path && !getCacheRoutePath()) {
    window.sessionStorage.setItem('cacheRoutePath', path)
  }
}
const getCacheRoutePath = () => {
  const cacheRoutePath = window.sessionStorage.getItem('cacheRoutePath')
  return cacheRoutePath
}
const removeCacheRoutePath = () => {
  window.sessionStorage.removeItem('cacheRoutePath')
}
uaa.initServer({
  uaaServer: '', // uaa认证服务地址
  userServer: '', // user-center
  clientId: '', // uaa服务注册的clientId
  clientSecret: '',// uaa服务注册的clientSecret
  grantType: '',  // 授权方式
  scope: '' // 授权范围
})
router.beforeEach((to, from ,next) => {
  uaa.checkLogin().then(res => {
    const userInfo = res
    const storeUserInfo = store.state.user.userInfo
    //  判断是否已初始化过系统数据
    if(storeUserInfo && storeUserInfo.id) {
      next()
    } else {
      // 初始化系统数据,存储userInfo, 加载异步路由、初始化菜单
      initSystem(userInfo, to, next)
    }
  }).catch(e => {
    // redirectUri必须与数据库配置保持一致
    const getBaseUrl = () => {
      const baseUrl = process.env.BASE_URL
      if (baseUrl.includes('http://') || baseUrl.includes('https://')) {
        return baseUrl
      } else {
        return window.location.protocol + '//' + window.location.host + baseUrl
      }
    }
    const redirectUri = getBaseUrl()
    // 存储登录前的path
    setCacheRoutePath(to.fullPath)
    uaa.login(redirectUri).then(res => {
      const userInfo = res
      console.log(userInfo)
      // 方案1: 通过重定向重新进页面,去掉url上的code 或 token
      location.href = redirectUri
      // 方案2: 或直接初始化数据再next(); 最后url会携带授权信息:code 或 token
      // initSystem(userInfo, to, next)
    }).catch(e => {
      //  提示登录失败信息
      console.log(e)
      next()
    })
  })
})
  1. step2 在请求头加上授权信息
  // aixos instance
  import uaa from 'uaa-core-api'
  const instance = axios.create({
    baseURL: '', //  配置后端服务地址
  })
  // 在拦截器上为每个请求加上授权信息
  instance.interceptors.request.use((config) => {
    config.headers['Authorization'] = uaa.getToken()
    return config
  }, error => {
    // 请求失败的处理
    return Promise.reject(error)
  })
  // 在请求响应拦截器上处理401错误请求
  instance.interceptors.response.use((res) => {
    // 在这里对返回的数据进行处理
    return res
  }, error => {
    if(error.response && error.response.status === 401) {
      // 未授权、或授权时效时,调用uaa.logout()清除token并跳到登录页面
      console.log('授权已过期,请重新登录')
      // 退出登录
      const getBaseUrl = () => {
        const baseUrl = process.env.BASE_URL
        if (baseUrl.includes('http://') || baseUrl.includes('https://')) {
          return baseUrl
        } else {
          return window.location.protocol + '//' + window.location.host + baseUrl
        }
      }
      const redirectUri = getBaseUrl()
      uaa.logout(redirectUri)
    }
  })
1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago