【Spring Cloud Alibaba 温故而知新】(三)授权、鉴权中心微服务

本文详细介绍了如何使用Spring Cloud Alibaba搭建一个授权、鉴权中心微服务,涉及JWT、RSA256公钥私钥管理,以及授权服务接口的实现。通过创建sca-commerce-authority-center子工程,进行数据表建模、逆向生成实体,实现基于JWT+RSA256的授权,并对外提供授权服务接口,最后总结了基于Token与基于服务器的身份认证的优缺点。
摘要由CSDN通过智能技术生成

目录

6.1.1 JWT

  • JWT的基本概念

    • Json Web Token (JWT) 是一个开放标准,它定义了一种紧凑的、自包含的方式,用于作为 JSON 对象在各方之间安全地传输信息
    • 哪些场景下可以考虑使用 JWT?
      • 用户授权
      • 信息交换
  • JWT的结构以及含义

    • JWT由三个部分组成:Header、Payload、Signature,且用圆点连接 xxx.yyyy.zzzz
    • Header:由两部分(Token类型、加密算法名称)组成,并使用 Base64 编码
    • Payload:K,V形式的数据,即你想传递的数据(授权的话就是 Token 信息)
    • Signature:为了得到签名部分,你必需有编码过的 Header、编码过的 payload、一个密钥,签名算法是 Header中指定的那个,然后它们签名即可
      • HMACSHA256(BASE64uRLeNCODE(HEADER) + “.” BASE64uRLeNCODE(PAYLOAD), SECRET)
  • 授权、鉴权中心微服务功能逻辑架构
    在这里插入图片描述

6.2.1 搭建授权、鉴权中心微服务

6.2.1.2 创建 sca-commerce-authority-center 子工程

子工程:sca-commerce-authority-center

pom.xml
<?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>sca-commerce</artifactId>
        <groupId>com.edcode.commerce</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>sca-commerce-authority-center</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!-- 模块名及描述信息 -->
    <name>sca-commerce-authority-center</name>
    <description>授权中心</description>

    <dependencies>
        <!-- spring cloud alibaba nacos discovery 依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.3.RELEASE</version>
        </dependency>
        <!-- Java Persistence API, ORM 规范 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- tk.mybatis 先使用jpa, 之后有空在修改 mybatis -->
<!--        <dependency>-->
<!--            <groupId>tk.mybatis</groupId>-->
<!--            <artifactId>mapper-spring-boot-starter</artifactId>-->
<!--            <version>2.1.5</version>-->
<!--        </dependency>-->
<!--        <dependency>-->
<!--            <groupId>tk.mybatis</groupId>-->
<!--            <artifactId>mapper</artifactId>-->
<!--            <version>4.1.5</version>-->
<!--        </dependency>-->
        <!-- MySQL 驱动, 注意, 这个需要与 MySQL 版本对应 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.27</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.edcode.commerce</groupId>
            <artifactId>sca-commerce-mvc-config</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!-- screw 生成数据库文档 -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.30</version>
        </dependency>
        <dependency>
            <groupId>cn.smallbun.screw</groupId>
            <artifactId>screw-core</artifactId>
            <version>1.0.3</version>
        </dependency>
    </dependencies>

    <!--
        SpringBoot的Maven插件, 能够以Maven的方式为应用提供SpringBoot的支持,可以将
        SpringBoot应用打包为可执行的jar或war文件, 然后以通常的方式运行SpringBoot应用
     -->
    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

<!--            <plugin>-->
<!--                <groupId>org.mybatis.generator</groupId>-->
<!--                <artifactId>mybatis-generator-maven-plugin</artifactId>-->
<!--                <version>1.4.0</version>-->
<!--                <configuration>-->
<!--                    <configurationFile>-->
<!--                        ${basedir}/src/main/resources/generator/generatorConfig.xml-->
<!--                    </configurationFile>-->
<!--                    <overwrite>true</overwrite>-->
<!--                    <verbose>true</verbose>-->
<!--                </configuration>-->
<!--                <dependencies>-->
<!--                    <dependency>-->
<!--                        <groupId>mysql</groupId>-->
<!--                        <artifactId>mysql-connector-java</artifactId>-->
<!--                        &lt;!&ndash;对应 MySQL 驱动&ndash;&gt;-->
<!--                        <version>8.0.27</version>-->
<!--                    </dependency>-->
<!--                    <dependency>-->
<!--                        <groupId>tk.mybatis</groupId>-->
<!--                        <artifactId>mapper</artifactId>-->
<!--                        &lt;!&ndash;mapper 版本&ndash;&gt;-->
<!--                        <version>4.1.5</version>-->
<!--                    </dependency>-->
<!--                </dependencies>-->
<!--            </plugin>-->

        </plugins>
    </build>

</project>
bootstrap.yml
server:
  port: 7000
  servlet:
    context-path: /scacommerce-authority-center

spring:
  application:
    name: sca-commerce-authority-center
  cloud:
    nacos:
      # 服务注册发现
      discovery:
        enabled: true # 如果不想使用 Nacos 进行服务注册和发现, 设置为 false 即可
        #server-addr: ${NACOS_ADDR:127.0.0.1}:8848
        server-addr: ${
   NACOS_ADDR_1:127.0.0.1}:8848,${
   NACOS_ADDR_2:127.0.0.1}:8849,${
   NACOS_ADDR_3:127.0.0.1}:8850 # Nacos 服务器地址
        namespace: ${
   NAMESPACE_ID:1adcfdd8-5763-4768-9a15-9c7157988950}
        metadata:
          management:
            context-path: ${
   server.servlet.context-path}/actuator
  jpa:
    show-sql: true
    hibernate:
      ddl-auto: none
    properties:
      hibernate.show_sql: true
      hibernate.format_sql: true
    open-in-view: false
  datasource:
    # 数据源
    url: jdbc:mysql://${
   MYSQL_HOST:127.0.0.1}:${
   MYSQL_PORT:3306}/${
   MYSQL_DATABASE:sca_commerce}?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false
    username: ${
   MYSQL_USERNAME:root}
    password: ${
   MYSQL_PASSWORD:123456}
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 连接池
    hikari:
      maximum-pool-size: 8
      minimum-idle: 4
      idle-timeout: 30000
      connection-timeout: 30000
      max-lifetime: 45000
      auto-commit: true
      pool-name: ScaCommerceHikariCP

# 暴露端点
management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: always
启动类
package com.edcode.commerce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author eddie.lee
 * @blog blog.eddilee.cn
 * @description 授权中心启动入口
 */
@EnableDiscoveryClient
@SpringBootApplication
public class AuthorityCenterApplication {
   

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

}
查看 Nacos Web

在这里插入图片描述

6.3.1 数据表及ORM过程

6.3.1.1 运行 SQL 建表语句

t_scacommerce_user.sql

-- 创建 t_scacommerce_user 数据表
CREATE TABLE IF NOT EXISTS `sca_commerce`.`t_scacommerce_user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '自增主键',
  `username` varchar(64) NOT NULL DEFAULT '' COMMENT '
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

eddie_k2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值