マイクロサービスをゼロから構築する - ゲートウェイ センター (1)

最初に書きます

このプロジェクトから何かを得た場合は、Star をフォローすることを忘れないでください。これは私にとって非常に良い励ましとサポートです。

ソースアドレス: gitee.com/csps/mingyu…

文書アドレス: gitee.com/csps/mingyu…

新しい明月ゲートウェイ

このモジュールは [ゼロからマイクロサービスを構築する - 登録センター (2)] で構築されました。

コアの依存関係

<!-- SpringCloud Gateway,内置 webflux 依赖-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency><!-- LB 扩展 -->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>

コア構成

application.yml または nacos に保存できます。

spring:
    cloud:
        # 网关配置
        gateway:
            # 打印请求日志(自定义)
            requestLog: true
            discovery:
                locator:
                    enabled: true
            routes:
                # 认证中心
                - id: mingyue-auth
                  uri: lb://mingyue-auth
                  predicates:
                      - Path=/auth/**
                  filters:
                      - StripPrefix=1

テストゲートウェイ

ミンギュ認証を開始する

ログイン インターフェイスを直接呼び出します: http://localhost:9000/oauth2/token

curl --location --request POST 'http://localhost:9000/oauth2/token?grant_type=password&client_id=1001&client_secret=aaaa-bbbb-cccc-dddd-eeee&username=sa&password=123456' \
--header 'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Host: localhost:9000' \
--header 'Connection: keep-alive'

返される結果:

{
    "code": 200,
    "msg": "ok",
    "data": {
        "access_token": "xHJR3aIsYOZxZiCpbXzDjKMS32JfLV2m5jtme1gEsItvKR8ZiEZ3GUVkJBom",
        "refresh_token": "O0SmM1Pb7MP18rE7Uz72MIY9uucCTBnAJ2CkWXCq4FeaTU7o8e0BPExfvQGX",
        "expires_in": 7199,
        "refresh_expires_in": 2591999,
        "client_id": "1001",
        "scope": "",
        "openid": "gr_SwoIN0MC1ewxHX_vfCW3BothWDZMMtx__"
    }
}

ミンギュエゲートウェイを開始

間接呼び出し (ゲートウェイ経由) ログイン インターフェイス: http://localhost:9100/auth/oauth2/token

curl --location --request POST 'http://localhost:9100/auth/oauth2/token?grant_type=password&client_id=1001&client_secret=aaaa-bbbb-cccc-dddd-eeee&username=sa&password=123456' \
--header 'User-Agent: Apifox/1.0.0 (https://www.apifox.cn)' \
--header 'Content-Type: application/json' \
--header 'Accept: */*' \
--header 'Host: localhost:9100' \
--header 'Connection: keep-alive' \
--header 'Cookie: Authorization=d7dc6f87-bf4a-4903-ad6c-2ac668f5e5a7'

返される結果:

{
    "code": 200,
    "msg": "ok",
    "data": {
        "access_token": "PRMy2RJXTA8SAtHChWLXYRHfANQiOeFPxyfilCdUnz8j0oZrnpXZqkkh06BB",
        "refresh_token": "najLtYrf2SZhtsayHTKEeG5yVeZEmcgu7ePVf00C8QcX0uVjh8yw2SaH8INP",
        "expires_in": 7199,
        "refresh_expires_in": 2591999,
        "client_id": "1001",
        "scope": "",
        "openid": "gr_SwoIN0MC1ewxHX_vfCW3BothWDZMMtx__"
    }
}

改造 mingyue-ui

1. vite.config.tsを変更する

http://localhost:9000 => http://localhost:9100

'/api': {
          target: 'http://localhost:9100/',
          ws: true,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^/api/, ''),
},

2. ログイン関連のインターフェースアドレスを変更する

src/api/login/index.ts

signIn: (data: object) => {
      let client_id = 1001;
      let grant_type = 'password';
      let client_secret = 'aaaa-bbbb-cccc-dddd-eeee';
      // TODO 规范入参类型
      let username = data.username;
      let password = data.password;
​
      return request({
        url: '/api/auth/oauth2/token',
        method: 'post',
        params: {grant_type, client_id, client_secret, username, password}
      });
    },
signOut: () => {
      let client_id = 1001;
      let client_secret = 'aaaa-bbbb-cccc-dddd-eeee';
      let access_token = Session.get('token');
​
      // 调用回收 Access-Token 接口
      return request({
        url: '/api/auth/oauth2/revoke',
        method: 'post',
        params: {client_id, client_secret, access_token}
      });
    }

3. テストを開始する

ログインもログアウトもできますよ〜

まとめ

mingyue-ui => mingyue-gateway => mingyue-auth以下のフローチャートに示すように、これまでに を完了しました。

image-20230531201001455

次に、mingyue-systemユーザー、mingyue-authユーザーのクエリをデータベースに変換しました。

おすすめ

転載: juejin.im/post/7240636320762314810
おすすめ