@gonsin/glicense

0.3.0 • Public • Published

授权接入

后端接入示例

添加依赖

<!-- 添加依赖 -->
<dependency>
    <groupId>com.gonsin</groupId>
    <artifactId>GLicense</artifactId>
    <version>1.1</version>
</dependency>

添加注解

特别注意,只能在main方法对应的类上添加该注解,整个可运行的代码不能有2处@EnableGLicense

/// 其他注解

// 添加如下代码,device表示你的项目类型,每个系统对应一个device,授权文件根据device的不同而互相区分
@EnableGLicense(device = "paperless_v3")
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }

}

前端接入示例

安装依赖

npm install @gonsin/glicense

添加路由

在需要授权接入的项目的路由中添加:

import GLicense from "@gonsin/glicense";

// ... 省略其他代码

const routes = [
  {
    path: "/license",
    name: "license",
    component: GLicense,
  }
]

添加拦截器

添加响应拦截配置:

以下为 axios 封装中响应拦截器的例子(需要将 this.router 替换成可跳转路由的 router 实例):

this.service.interceptors.response.use(
  (response: AxiosResponse) => {
      const { data } = response;
      const { state } = data;

      if (state == 200) {
          return data;
      } else if (state == "-409") {
          NProgress.done();

          // 跳转到授权页面
          const { device, uuid } = data.data;
          if ((this.router ?? "") != "") {
              this.router?.push({
                  name: "license",
                  query: {
                      device,
                      uuid,
                  },
              });
          }

          return data;
      }
  },
  async (error: AxiosError) => {
      MessagePlugin.error(error.message);
      return Promise.reject(error);
  },
);


效果演示

正常情况,按照如上步骤操作之后,访问未经过授权的服务器,会自动跳转到待授权页面,如下图:

扫码并获取到授权码之后,可以进行 联网授权离线授权

Readme

Keywords

none

Package Sidebar

Install

npm i @gonsin/glicense

Weekly Downloads

13

Version

0.3.0

License

none

Unpacked Size

22.9 kB

Total Files

4

Last publish

Collaborators

  • gonsin