解析spring cloud ouath2中的Eureka

作者:小黄鸡1992 时间:2023-10-12 04:07:54 

老生常谈的配置 但是还是需要说明一下

EurekaApplication 

@EnableEurekaServer指定为server端


@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

}

WebSecurityConfig 

看到这眼熟不呢 没错 Eureka也开启spring security 在访问前台页面时也需要输入账号密码


@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

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

application.yml

security:user以下配置的账号密码 在访问页面需要输入的


server:
port: 7001
spring:
application:
name: eureka
security:
user:
 name: admin
 password: xxxxxxxxxx
eureka:
client:
fetch-registry: false
register-with-eureka: false
service-url:
 defaultZone: http://admin:Xk38CNHigBP5jK75@localhost:5001/eureka
instance:
hostname: localhost
prefer-ip-address: true
logging:
config: classpath:logback.xml
level:
root: info

pom.xml


<dependencies>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
 </dependency>
 <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-test</artifactId>
  <scope>test</scope>
  <exclusions>
   <exclusion>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
   </exclusion>
  </exclusions>
 </dependency>
 <dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-test</artifactId>
  <scope>test</scope>
 </dependency>
</dependencies>

当访问localhost:7001 出现下图 需要填写账号密码

解析spring cloud ouath2中的Eureka

在其他服务注册时 也需要指定账号密码


eureka:
instance:
hostname: ${spring.cloud.client.ip-address}
prefer-ip-address: true
instance-id: ${eureka.instance.hostname}:${server.port}
//与server一致
client:
security:
 basic:
 user: admin
 password: xxxxxxxxxxx
service-url:
 defaultZone: http://${eureka.client.security.basic.user}:${eureka.client.security.basic.password}@localhost:7001/eureka

来源:https://blog.csdn.net/qq_20143059/article/details/113759124

标签:spring,cloud,ouath2,Eureka
0
投稿

猜你喜欢

  • java字符串的重要使用方法以及实例

    2023-11-13 23:11:51
  • Java组件commons fileupload实现文件上传功能

    2022-05-03 15:03:07
  • SpringBoot整合Zookeeper详细教程

    2022-07-24 11:33:09
  • Java基于LoadingCache实现本地缓存的示例代码

    2022-08-28 12:05:04
  • Groovy的规则脚本引擎实例解读

    2023-07-11 21:24:04
  • Android app第三方支付宝支付接入教程

    2022-06-05 20:02:19
  • ZooKeeper入门教程一简介与核心概念

    2022-11-24 18:36:00
  • Java用BigDecimal类解决Double类型精度丢失的问题

    2022-01-07 16:49:24
  • C#实战之备忘录的制作详解

    2023-08-13 02:17:21
  • java实现简单的俄罗斯方块

    2021-08-02 18:13:11
  • Java8 Stream教程之collect()的技巧

    2023-05-01 00:45:15
  • 优化spring boot应用后6s内启动内存减半

    2021-09-13 02:47:59
  • Android编程实现图片的颜色处理功能示例

    2022-10-08 23:15:25
  • Java Socket实现传输压缩对象的方法示例

    2022-12-18 06:52:30
  • java中的this引用及对象构造初始化

    2023-03-07 09:38:17
  • C#使用linq查询大数据集的方法

    2023-05-24 16:52:17
  • 详解C#通过反射获取对象的几种方式比较

    2021-07-26 17:45:55
  • Java日期时间类(Date、DateFormat、Calendar)解析

    2022-08-06 18:02:14
  • Java自定义实现链队列详解

    2023-06-22 12:47:31
  • 获取Java线程转储的常用方法(推荐)

    2023-05-15 02:30:19
  • asp之家 软件编程 m.aspxhome.com