老生常談的配置 但是還是需要說明一下
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 出現下圖 需要填寫賬號密碼

在其他服務注冊時 也需要指定賬號密碼
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://www.uj5u.com/houduan/258407.html
標籤:java
