Service fails to connect to registry solution when authentication is turned on
The main modules provided by the SpringCloud component include: service discovery (Eureka), circuit breaker (Hystrix), smart road (Zuul), client load balancing (Ribbon), Archaius, Turbine, etc.
Eureka is equivalent to zookeeper, which is used for service registration and discovery in microservice projects. When using springBoot+springCloud to develop microservices, the basic purpose can be achieved through some simple configurations
Registry access rights configuration
Add dependencies in registry service pom.xml
org.springframework.boot spring-boot-starter-security ">
<!-- Add registry permission dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
In the registry service application.properties file (note), the content is as follows
#New version open permission#Registry center configuration spring.application.name=szq-serverserver.port=8761eureka.instance.hostname=localhosteureka.client.registerWithEureka=falseeureka.client.fetch-registry=falseeureka.client.serviceUrl.defaultZone=http ://admin:123456@${eureka.instance.hostname}:${server.port}/eureka/#The new version enables permissions spring.security.user.name=adminspring.security.user.password=123456
Start the registry service project, enter http://localhost:8761/ in the browser and the eureka console page will appear and ask for the user name and password box to be successful
<Img src = "https://camo.githubusercontent.com/0737b8866968ca7d99831ebe7a8427c4aa69ff14ce696adb8819bea20b593764/68747470733a2f2f696d616765732e67697465652e636f6d2f75706c6f6164732f696d616765732f323031382f303732342f3134303731355f34663666363833655f313437383337312e706e67" alt = "Enter image description" title = "WeChat picture_20180724140701.png" data-canonical-src = "https://images.gitee.com /uploads/images/2018/0724/140715_4f6f683e_1478371.png" 100%;"="" style="overflow-wrap: break-word; max-width: 100%; margin-bottom: 0px;">
The service cannot connect to the registry solution after the verification is turned on
run error message
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
The security of Spring Cloud 2.0 and above has csrf verification enabled by default. To configure the csrf verification of security on the eurekaServer side, it is false.
When registering in the service registry, add the account password eureka.client.serviceUrl.defaultZone=http://admin:123456@localhost:8761/eureka/
Add a class that inherits WebSecurityConfigurerAdapter
Add the @EnableWebSecurity annotation to the class;
Override the configure(HttpSecurity http) method of the parent class and turn off csrf
package com.pflm;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;/**
* Can't connect to the registration center after eureka opens the verification?
* The security of spring Cloud 2.0 and above) has csrf inspection enabled by default. To configure the csrf inspection of security on the eurekaServer side, it is false.
* @author qxw
*@data on July 24, 2018 at 1:58:31 PM
*/@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}}
0 Comments