# vue-wonders-sso-util

> wonders-sso vue工具

Latest version **1.1.7** (published 2023-01-18) · ISC license · 0 weekly downloads

## Install

```sh
npm install vue-wonders-sso-util
pnpm add vue-wonders-sso-util
yarn add vue-wonders-sso-util
bun add vue-wonders-sso-util
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.7 |
| Published | 2023-01-18 |
| First published | 2023-01-17 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | wy |
| Maintainers | wy1995gogo |
| Keywords | wonders |

## Links

- npm: https://www.npmjs.com/package/vue-wonders-sso-util
- npm.io page: https://npm.io/package/vue-wonders-sso-util

## Recent versions

- 1.1.7 (latest) — 2023-01-18
- 1.1.6 — 2023-01-18
- 1.1.5 — 2023-01-17
- 2.0.0 — 2023-01-17
- 1.0.0 — 2023-01-17

## README

## 使用方式

### 安装

#### NPM
```shell
npm install vue-wonders-sso-util@1.1.7 --save
```

#### YARN
```shell
yarn add vue-wonders-sso-util@1.1.7
```

### 使用

#### 全局注册 main.js中
```javascript
import SSOGuard from 'vue-wonders-sso-util'
// 参数1：vue路由 VueRouter 
// 参数2：axios对象  
// 参数3：axios 的baseURL （①baseURL可以是指向应用后端的，后端需要对应的拦截，详见springboot后端内容 ②baseURL可以是请求单点中心的baseURL，但是work时候需要自己定义回调）
const ssoGuard = new SSOGuard(router,axios,process.env.VUE_APP_API_ROOT)
//注册到vue实例中
Vue.prototype.$SSOGuard = ssoGuard;
//开启单点拦截
ssoGuard.work();
//开启单点网络拦截
ssoGuard.axiosWork();

//这样就完成单点拦截，
// 获取用户信息在需要的地方调用this.$SSOGuard.getSSOUser()即可
// 注销方式在需要的地方调用this.$SSOGuard.logout()即可
/**
 * axios的完整拦截，需要自己在axios配置添加拦截器。
 * 如以下示例代码，当未授权时候，跳转到单点登录页面
 */
axios.defaults.validateStatus = function(status) {
    return status >= 200 && status <= 500 // 默认的
}
axios.interceptors.response.use(res => {
    if (res.status===401){
        let obj = res.data.result;
        let projectUrl = window.location.href
        window.location.href = `${obj.ssoUrl}?service=${obj.proUrl ? obj.proUrl : projectUrl}`;
        return ;
    }
    return res
}, error => {
    return Promise.reject(error)
})
```

#### 一些方法

##### work

```text
开启vue路由拦截
work方法有3个入参 errorBack, successBack, unauthorizedBack
errorBack：异常回调函数
successBack：成功（通过单点校验）回调函数，默认是直接next()
unauthorizedBack：401回调函数，默认是跳转到单点登录页面
```

##### logout

```text
this.$SSOGuard.logout()
用于注销，注意此方法只会清除单点用到的缓存，cookie等。业务项目的请在调用此方法前自行清除
```

##### getSSOUser

```text
this.$SSOGuard.getSSOUser()
获取单点用户
返回值例子
{
	"expireFreshTime": 1674029403195,
	"expireMinute": 30,
	"loginname": "srm_admin",//登录名
	"userid": "402819b96e1e334b016e8cf7e57c00a7",
	"username": "市人民医院管理员",//用户名
	"version": "ac64301d8e8f4f0ca50c4aa570e82bc1"
}
```

##### validate

```text
校验是否登录
返回值为promise
```

### springboot后端

> 使用此单点工具，后端需要对应的单点拦截。需要配置后端对应jar包 版本>=1.1.7
```xml
<dependency>
    <groupId>com.wonders</groupId>
    <artifactId>wonders-sso-backstage</artifactId>
    <version>1.1.7</version>
</dependency>
```

---
_Source: https://npm.io/package/vue-wonders-sso-util · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
