用户登录鉴权JWT代码实现

Posted 一只楠喃

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户登录鉴权JWT代码实现相关的知识,希望对你有一定的参考价值。

实现流程

1、用户请求登录

2、Zuul将请求转发到授权中心,请求授权

3、授权中心校验完成,颁发JWT凭证

4、客户端请求其它功能,携带JWT

5、Zuul将jwt交给授权中心校验,通过后放行

6、用户请求到达微服务

7、微服务将jwt交给鉴权中心,鉴权同时解析用户信息

8、鉴权中心返回用户数据给微服务

9、微服务处理请求,返回响应

  1. jwt-parent:统一jar包版本控制
  2. jwt-pojo:实体类存放位置
  3. jwt-common:工具类、常量类等存放的位置
  4. jwt-auth:认证中心
  5. goods-search:商品搜索服务,对外暴露商品搜索相关接口
  6. user-service:用户服务,对外暴露用户操作相关接口,如新增用户等

搭建父项目工程jwt-parent

创建项目

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.czxy</groupId>
    <artifactId>jwt-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>jwt-parent</name>
    <description>Demo project for Spring Boot</description>
<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR10</spring-cloud.version>
        <mybatis.starter.version>2.1.1</mybatis.starter.version>
        <mapper.starter.version>1.2.3</mapper.starter.version>
        <druid.starter.version>1.1.9</druid.starter.version>
        <mysql.version>5.1.32</mysql.version>
        <pageHelper.starter.version>1.2.3</pageHelper.starter.version>
        <jjwt.version>0.7.0</jjwt.version>
        <joda-time.version>2.9.6</joda-time.version>
        <lombok.version>1.18.18</lombok.version>
    </properties>

    <!-- dependencyManagement
        这个标签一般用在父项目中,他不是导入jar包的标签,只是用来限定jar包版本的标签
        然后子项目依赖当前父项目,在子项目中导入需要的jar包坐标
        子项目无需填入版本号,完全由父项目控制
    -->
    <dependencyManagement>
        <dependencies>
            <!-- springCloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>$spring-cloud.version</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- mybatis启动器 -->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>$mybatis.starter.version</version>
            </dependency>
            <!-- 通用Mapper启动器 -->
            <dependency>
                <groupId>tk.mybatis</groupId>
                <artifactId>mapper-spring-boot-starter</artifactId>
                <version>$mapper.starter.version</version>
            </dependency>
            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper-spring-boot-starter</artifactId>
                <version>$pageHelper.starter.version</version>
            </dependency>
            <!-- druid启动器 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
                <version>$druid.starter.version</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>$mysql.version</version>
            </dependency>
            <dependency>
                <groupId>io.jsonwebtoken</groupId>
                <artifactId>jjwt</artifactId>
                <version>$jjwt.version</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>$joda-time.version</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>$lombok.version</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

启动Nacos注册中心

我这里启动是单节点模式

启动之后可以去页面访问Nacos,http://localhost:8845/nacos

其他模块搭建

创建jwt-common模块

创建项目

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>jwt-parent</artifactId>
        <groupId>com.czxy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jwt-common</artifactId>

    <dependencies>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/joda-time/joda-time -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
		<dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

加入工具类

项目结构

创建jwt-pojo模块

创建项目

POM文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>jwt-parent</artifactId>
        <groupId>com.czxy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>jwt-pojo</artifactId>
	<dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>


</project>

创建实体类

Goods(我这里是商品和用户)

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods 

    private Integer skuid;
    private String goodsName;
    private Double price;
    

User实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User 

    private Integer id;
    private String username;
    private String password;
    

搭建goods-search

功能分析

1、用户未登陆状态,可以搜索商品信息

2、goods-search为商品搜索服务,接收用户页面搜索请求

实现步骤:

1、pojo

2、controller

4、service

5、dao

创建项目

pom

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.czxy</groupId>
        <artifactId>jwt-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>goods-search</artifactId>
    <packaging>jar</packaging>
    <name>goods-search</name>
    <description>Demo project for Spring Boot</description>


  <dependencies>
        <dependency>
            <groupId>com.czxy</groupId>
            <artifactId>jwt-pojo01</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>com.czxy</groupId>
            <artifactId>jwt-common01</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--mybatis-->
<!--        <dependency>-->
<!--            <groupId>org.mybatis.spring.boot</groupId>-->
<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--        </dependency>-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--nacos配置管理依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

bootstrap.yml配置

spring:
  application:
    name: goods-service
  profiles:
    active: dev
  cloud:
    nacos:
      server-addr: localhost:8845
      config:
        file-extension: yaml
server:
  port: 7000

功能实现

模拟商品搜索功能

启动类

@SpringBootApplication
public class GoodsSearch01Application
public static void main(String[] args)
SpringApplication.run(GoodsSearch01Application.class, args);

功能测试

搭建user-service服务

功能分析

提供用户操作相关的接口

搭建项目

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocatio

以上是关于用户登录鉴权JWT代码实现的主要内容,如果未能解决你的问题,请参考以下文章

JWT如何实现登录、鉴权

eggjs 怎么使用 egg-jwt 进行用户鉴权实现登录功能?

如何在.net6webapi中配置Jwt实现鉴权验证

.NET Core5.0 JWT鉴权SSO单点登录

SpringSecurity注解鉴权(整合springboot,jwt,redis)

实现JWT鉴权机制