我配置了一个 Eureka 服务器(它正在工作,因为我可以连接到它并查看 Eureka 控制台),但我的微服务不会在其中注册。我收到 403 服务器错误(禁止)。尝试注册的微服务中的身份验证配置似乎没问题,就好像我故意设置错误一样,我得到了 401。
我的gradle配置:
buildscript {
ext {
springBootVersion = '2.0.5.RELEASE'
gradleDockerVersion = '1.2'
}
repositories {
mavenCentral()
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE"
classpath("se.transmode.gradle:gradle-docker:${gradleDockerVersion}")
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
ext {
springCloudVersion = 'Finchley.RELEASE'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
mavenBom 'org.springframework.cloud:spring-cloud-netflix:2.0.1.RELEASE'
}
}
这是我的配置。我已将其包含在 bootstrap.yaml 和配置服务器中只是为了确保:
eureka:
client:
serviceUrl:
defaultZone: http://myuserid:mypassword@localhost:8761/eureka/
但由于某种原因它被忽略了。该属性的根是“eureka”,而不是“spring.eureka...”?
这是微服务中的错误日志,它似乎正在尝试本地主机和标准端口 8080,因此忽略上面的配置。
2018-09-28 09:31:12.763 INFO 58552 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_MTPMS_WEATHER/localhost:mtpms_weather:8080: registering service...
2018-09-28 09:31:12.767 INFO 58552 --- [ restartedMain] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 2147483647
2018-09-28 09:31:12.768 INFO 58552 --- [ restartedMain] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2018-09-28 09:31:12.803 WARN 58552 --- [nfoReplicator-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failure with status code 403; retrying on another server if available
2018-09-28 09:31:12.808 WARN 58552 --- [nfoReplicator-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_MTPMS_WEATHER/localhost:mtpms_weather:8080 - registration failed Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.3.jar:1.9.3]
我终于发现当前的 Eureka Server 存在一个错误,在这个答案中描述:
https://github.com/spring-cloud/spring-cloud-netflix/issues/2754#issuecomment-372808529
因此,将以下 @EnableWebSecurity 代码添加到 Gateway Spring Boot 应用程序服务器类即可修复该问题:
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.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(DiscoveryApplication.class).run(args);
}
@EnableWebSecurity
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
}
我遇到了同样的问题,我使用的是 Spring Boot 版本 3.1.3,以下是我的安全配置和应用程序配置
@Bean
public SecurityFilterChain configure(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests(authorize -> authorize
.requestMatchers("/actuator/health/**")
.permitAll()
.anyRequest()
.authenticated()
)
.httpBasic(Customizer.withDefaults())
.formLogin(Customizer.withDefaults())
.csrf((AbstractHttpConfigurer::disable))
.build();
}
在application.yml中
eureka: server: enableSelfPreservation: false client: register-with-eureka: true fetch-registry: false serviceUrl: defaultZone: http://admin:Qmic$123$@localhost:8761/eureka/ health: config: enabled: false server: port: 8761
如果对大家有帮助就点个赞吧