首页 > 其他分享 >eureka加入认证功能

eureka加入认证功能

时间:2023-09-02 12:12:51浏览次数:28  
标签:加入 spring boot springframework 认证 org security eureka

为了注册中的安全性,我们需要加入eureka登录的认证功能,然后该功能的添加涉及两部分,一部分是eureka服务端,另一部分是eureka客户端

eureka服务端配置

 

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

  

 

#端口号
server.port=8763
eureka.instance.hostname=127.0.0.1
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#关闭自动保护机制
eureka.server.enable-self-preservation=false
#eureka清理无效节点的时间间隔,单位毫秒
eureka.server.eviction-interval-timer-in-ms=60000

#日志打印级别
logging.level.com.eureka=info
logging.level.web=info
spring.devtools.add-properties=false

spring.security.user.name=aa
spring.security.user.password=123

spring.security.basic.enable=true

  

 

package com.eureka.config;

import org.springframework.context.annotation.Configuration;
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;
import org.springframework.security.config.http.SessionCreationPolicy;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER);
        http.csrf().disable();
        //注意:为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic,
        // 如果是form方式,不能使用url格式登录
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

  eureka客户端配置

 

eureka.instance.hostname=127.0.0.1
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:8763/eureka/

spring.security.user.name=aa
spring.security.user.password=123

  

 再次登录会有这样的认证

 这里看到

 服务已经注册上来了

 我的eureka服务端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>

    <groupId>com.eureka</groupId>
    <artifactId>pingan-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>

    <dependencyManagement>

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>


        </dependencies>
    </dependencyManagement>


    <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-security</artifactId>
        </dependency>
    </dependencies>

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


</project>

  我的eureka客户端配置

<?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.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.ip</groupId>
    <artifactId>ip-service</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
        <skipTests>true</skipTests>
    </properties>

    <dependencyManagement>

        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <!--客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--tomcat容器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--添加fastjson依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.7</version>
        </dependency>
        <!--lombok依赖-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>
        <!--springboot整合mybatis-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.2</version>
        </dependency>
        <!-- 热部署模块 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>
        <!--java爬虫需要的jar包-->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.12.2</version>
        </dependency>
        <!--判断空的用法  -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.10.0</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
         <!--RateLimiter的底层是基于令牌桶算法来实现的,来自谷歌的Guava包中-->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.0-jre</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.6</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--时间日期工具类-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.8</version>
        </dependency>
         <!--ip2region xdb java-->
        <dependency>
            <groupId>org.lionsoul</groupId>
            <artifactId>ip2region</artifactId>
            <version>2.7.0</version>
        </dependency>

    </dependencies>

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

        </plugins>
        <finalName>server</finalName>
    </build>


</project>

  

 

标签:加入,spring,boot,springframework,认证,org,security,eureka
From: https://www.cnblogs.com/q202105271618/p/17673523.html

相关文章

  • 2023年华为认证培训(Hcia/Hcie)限时免费学
    腾科IT教育推出了专门的人才培养计划,旨在帮助更多学员获得华为HCIE证书!我们郑重承诺,将在6个月内全力支持学员成功通过HCIE-Datacom、HCIE-Cloud和HCIE-Security考试,并帮助他们获得相关证书。作为一家始终致力于提供最优质IT教育资源和服务的机构,腾科IT教育一直致力于帮助学员实现他......
  • 不再受限于学历!学习华为认证,实现财富与事业双丰收!
    学习华为认证会有哪些疑问呢?学习华为技术方向不知从哪开始有什么学习的推荐思路呢?可以看看我接下来的讲解1.确定认证项目你需要确定你想要获得哪个华为认证项目的认证。华为认证包括多个领域,如网络、云计算、存储等。选择一个你感兴趣且适合你职业发展的项目。主要还是以HCIE、HCIP......
  • 2023下半年深圳软考信息系统项目管理师认证开班中,快来报名
    信息系统项目管理师是全国计算机技术与软件专业技术资格(水平)考试(简称软考)项目之一,是由国家人力资源和社会保障部、工业和信息化部共同组织的国家级考试,既属于国家职业资格考试,又是职称资格考试。信息系统项目管理师,属于软考三个级别中的“高级”。  【报考要求】 不设学历与资历......
  • 2023年9月长沙/长春/深圳CDGA/CDGP数据治理认证考试报名
    据DAMA中国官方网站消息,2023年度第三期DAMA中国CDGA和CDGP认证考试定于2023年9月23日举行。 报名通道现已开启,相关事宜通知如下: 考试科目: 数据治理工程师(CertifiedDataGovernanceAssociate,CDGA)数据治理专家(CertifiedDataGovernanceProfessional,CDGP) 考试时间: CDGA:2023......
  • 2023下半年武汉/成都/深圳NPDP产品经理国际认证开班啦
    产品经理国际资格认证NPDP是新产品开发方面的认证,集理论、方法与实践为一体的全方位的知识体系,为公司组织层级进行规划、决策、执行提供良好的方法体系支撑。  【认证机构】 产品开发与管理协会(PDMA)成立于1979年,是全球范围内产品开发与管理专业人士最杰出的倡导者,协助个人、企业......
  • 祝贺!Databend Cloud 和阿里云 PolarDB 达成认证
    近日,北京数变科技有限公司旗下产品与阿里云PolarDB开源数据库社区展开产品集成认证。测试结果表明,北京数变科技有限公司旗下产品《DatabendCloud(V1.25)》正式通过了《阿里云PolarDB数据库管理软件》的技术认证,并收到了阿里云颁发的产品认证证书。经过严格联合测试,双方产品完......
  • 红帽认证 | RHCE证书,好考吗?
    RHCE证书在Linux行业中,有着非常高的认知度和一定的权威性。拥有RHCE证书的人,代表着他们具备了非常优秀的Linux系统管理能力。不仅能够有效地解决常见的系统管理问题,还能够在各种复杂的系统环境中熟练地进行配置和管理。那么,RHCE证书好考吗?01RHCE考试有一定难度对于Linux初学者来......
  • 每日一练 | 华为认证真题练习Day104
    1、下面关于免费ARP报文的作用描述错误的是()。A.在VRRP备份组中用来通告主备发生变换B.用于通告一个新的现AC地址:发送方更换网卡,AC地址发生改变,为了能够在AP表项老化前通告所有主机,发送方可以发送一个免费ARPC.用于检查重复的IP地址:正常情况下不会收到ARP回应,如果收到,则表明本网......
  • 网工内推 | IT网工,华为、华三认证优先,15k*13薪
    01广东善能科技发展股份有限公司招聘岗位:IT网络工程师职责描述:1、负责公司项目售后技术支持工作;2、负责项目交付实施,配置调试、运维等;3、参加合作厂商产品技术知识培训;4、参加合作厂商工程师认证考试;任职要求:1、大专及以上学历,计算机、网络、通信等理工类专业优先;2、具备HCIA或HCI......
  • 2023年DAMA-CDGA/CDGP数据治理认证线上到这里学习
    DAMA认证为数据管理专业人士提供职业目标晋升规划,彰显了职业发展里程碑及发展阶梯定义,帮助数据管理从业人士获得企业数字化转型战略下的必备职业能力,促进开展工作实践应用及实际问题解决,形成企业所需的新数字经济下的核心职业竞争能力。DAMA是数据管理方面的认证,帮助数据从业者提升......