Moon wu Ti frost all over the sky, jiangfeng fishing on sorrow sleep. Hanshan Temple outside Gusu, midnight bell to the passenger ship. Night Mooring on maple Bridge -- Zhang JiCopy the code

System architecture

System specifications

Source code address:Gitee.com/Mr-LuXiaoHu…
Micro – gateway (gateway)
  1. Port: 8080

  2. Based on the Springcloud-Gateway implementation, the application.yml configuration is as follows

spring:
  cloud:
    gateway:
      routes:
        - id: micro-auth-application
          uri: lb://micro-auth-application
          predicates:
            - Path=/micro-auth-application/**
          filters:
            - StripPrefix=1
        - id: micro-tmall-application
          uri: lb://micro-tmall-application
          predicates:
            - Path=/micro-tmall-application/**
          filters:
            - StripPrefix=1

Copy the code
  1. The interceptor
  • Release access token interface (login interface) : / micro – auth – application/the authorize/get – token
  • Intercept all other interfaces
Micro-auth-application (Authorized Application)
  1. Get the token(login) based on the username and password and store the token in Redis
  2. Authentication token
  3. Deleting a Token (Logging out)
Micro-tmallt-Application
  1. The application’s interface requires a login to access

How to run a project

  1. Install Nacos and configure the Nacos service registration and configuration addresses in the bootstrap.properties file or bootstrap.yml file for each service or application, as follows:
Spring: Cloud: nacos: config: file-extension: yML server-addr: 127.0.0.1:8848 Discovery: server-addr: 127.0.0.1:8848 application: name: micro - gatewayCopy the code
  1. To change the database connection address of the micro-user-service, create a table as follows:
CREATE TABLE 'user' (' id 'bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键', 'username' varchar(64) NOT NULL COMMENT 'iD ',' password 'varchar(255) NOT NULL COMMENT' id ', 'state' tinyint(4) DEFAULT '1' COMMENT 'status: 0- Disabled; 1- enable ', 'create_time' datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'create_time ', 'update_time' timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'UPDATE time ', PRIMARY KEY (' id'), UNIQUE KEY `uk_username` (`username`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4; INSERT INTO `user`.`user`(`username`, `password`, `state` ) VALUES ('chuliuxiang', '123456', 1);Copy the code
  1. Start the service
  • Micro-gateway Port: 8080
  • Micro-auth-application port: 8081
  • Micro-user-service Port: 8082
  • Micro-tmall-application port: 8083
  1. Login (obtain access token)
A POST request to http://localhost:8080/micro-auth-application/authorize/get-token {" username ":" chuliuxiang ", } response: {"code": 0, "message": "SUCCESS", "data": {"accessToken": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNSUNSTyIsImV4cCI6MTYwMjA3MjQ2NywiaWF0IjoxNjAyMDcxODY3LCJ1c2VybmFtZSI6Im NodWxpdXhpYW5nIn0. 9 tndahwx3o_dphzaqqhmi60xdj5f8abqzud7xezmnwc accessTokenExpireTime ", "" : 1602072467008," refreshToken ": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJNSUNSTyIsImV4cCI6MTYwMjY3NjY2NywiaWF0IjoxNjAyMDcxODY3LCJ1c2VybmFtZSI6Im NodWxpdXhpYW5nIn0. 04 eim8gwiihrsw3pr9413hfdqkhu9d5dnvviqxpwylu refreshTokenExpireTime ": 1602676667008}}","Copy the code
  1. Access protected resources
  • Set access-token in the request Header to login to accessToken
  • Accessing the interface you need to log in to:
http://localhost:8080/micro-tmall-application/test/getByUsername?username=chuliuxiang
Copy the code
  1. The front-end determines whether the token is valid according to the code of the response and prompts the token or jumps to the login page
{
    "code": 4003,
    "message": "Token is not exist or expire.",
    "data": {}
}
Copy the code