Spring Security Basic 之 Eureka加密

前言

在实际的使用中,Eureka Server需要考虑安全问题,不能随意让人调用Eureka Server上面的API;
未认证的Eureka Client也禁止注册到Eureka Server中

搭建父工程

环境:Spring boot 2.2.6.RELEASE 、Spring cloud Hoxton.SR5

<?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>

    <groupId>org.example</groupId>
    <artifactId>eureka-security</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>eureka-server</module>
        <module>eureka-client</module>
    </modules>
    <parent>
        <artifactId>spring-boot-starter-parent</artifactId>
        <groupId>org.springframework.boot</groupId>
        <version>2.2.6.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

搭建Eureka Server 子模块

引入spring-boot-starter-security

<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
    <parent>
        <artifactId>eureka-security</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server</artifactId>
    <name>eureka-server</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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-security</artifactId>
        </dependency>
    </dependencies>

</project>

编写yml

server:
  port: 1111
spring:
  application:
    name: eureka-server
  security:
    enable: true
    user:
      name: admin
      password: admin
eureka:
  client:
    #是否将自己注册到Eureka Server上,默认为true
    register-with-eureka: false
    #是否从Eureka Server上获取注册信息,默认为true
    fetch-registry: false
    service-url:
      defaultZone: http://admin:admin@localhost:${server.port}/eureka/
    instance:
      prefer-ip-address: true
      instance-id: http://localhost:${server.port}

编写启动类

package com.eureka.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApp.class, args);
    }

    @EnableWebSecurity
    static class WebSecurityConfig extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
            super.configure(http);
        }
    }
}

搭建Eureka Client子模块

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>eureka-security</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.eureka.client.EurekaClientApp</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

yml

server:
  port: 2222
spring:
  application:
    name: eureka-client

eureka:
  instance:
    prefer-ip-address: true
    # 实例名
    instance-id: eureka-client:${server.port}
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:1111/eureka/

启动类

package com.eureka.client;

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

@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApp {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApp.class,args);
    }
}

效果

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security 是一个针对 Java 应用程序的安全框架,用于处理身份验证、授权和其他与安全相关的功能。它提供了一套灵活的机制来保护应用程序的资源,并支持多种认证方式(如基于表单、HTTP Basic、OAuth 等)。 Spring Cloud Security 是基于 Spring Security 的扩展,专门用于在分布式系统中提供安全性。它通过集成 Spring Security 和其他 Spring Cloud 组件,为微服务架构中的服务间通信和统一认证提供了便利。 以下是 Spring Cloud SecuritySpring Security 的主要区别: 1. 适用范围:Spring Security 主要关注单体应用程序的安全性,而 Spring Cloud Security 则专注于分布式系统中的服务安全性。 2. 服务间认证:Spring Cloud Security 提供了针对微服务架构中服务间通信的安全认证机制。它通过使用共享的安全上下文和令牌传递机制,使得在微服务之间进行身份验证和授权变得更加方便。 3. 配置管理:Spring Cloud Security 可以与 Spring Cloud Config 集成,从配置中心动态加载安全配置信息。这使得在分布式环境中管理和更新安全配置变得更加灵活和集中化。 4. 服务发现和负载均衡:Spring Cloud Security 可以与 Spring Cloud Netflix 中的 Eureka 和 Ribbon 等组件集成,实现服务发现和负载均衡。这为安全性提供了更好的扩展性和容错性。 总的来说,Spring Security 是一个通用的安全框架,适用于各种类型的应用程序,而 Spring Cloud Security 则是专门为分布式系统中的微服务架构提供的安全解决方案。它们共享相似的概念和原则,但在应用范围和功能上有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值