zl程序教程

您现在的位置是:首页 >  前端

当前栏目

OAuth 2.0实现分布式认证授权-注册中心Eureaka搭建(7)

注册认证分布式分布式 实现 搭建 中心 授权
2023-09-27 14:22:12 时间

一 架构说明

所有微服务的请求都经过网关,网关从注册中心读取微服务的地址,将请求转发至微服务。

1 UAA 认证服务负责认证授权。
2 、所有请求经过 网关到达微服务
3 、网关负责鉴权客户端以及请求转发
4 、网关将 token 解析后传给微服务,微服务进行授权。

二 工程搭建

2.1 工程结构

2.2 配置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>spt-ds-oauth-server</artifactId>
        <groupId>com.ljf.springsecurity.oauth</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ljf.springsecurity.oauth</groupId>
    <artifactId>com.ds.ereuka..server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

    </dependencies>

</project>

2.3 application配置文件

spring:
  application:
    name: spt-ds-eureka-server

server:
  port: 53000 #启动端口

eureka:
  server:
    enable-self-preservation: false    #关闭服务器自我保护,客户端心跳检测15分钟内错误达到80%服务会保护,导致别人还认为是好用的服务
    eviction-interval-timer-in-ms: 10000 #清理间隔(单位毫秒,默认是60*1000)5秒将客户端剔除的服务在服务注册列表中剔除#
    shouldUseReadOnlyResponseCache: true #eureka是CAP理论种基于AP策略,为了保证强一致性关闭此切换CP 默认不关闭 false关闭
  client:
    register-with-eureka: false  #false:不作为一个客户端注册到注册中心
    fetch-registry: false      #为true时,可以启动,但报异常:Cannot execute request on any known server
    instance-info-replication-interval-seconds: 10
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/
  instance:
    hostname: ${spring.cloud.client.ip-address}
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}

 2.4 启动类

package com.ljf.springsecurity.oauth;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * Hello world!
 *
 */
@SpringBootApplication
@EnableEurekaServer
public class App 
{
    public static void main( String[] args )
    {

        SpringApplication.run(App.class,args);
        System.out.println("eureka服务启动成功!");
    }
}

2.5 启动