Spring Cloud (F version) to add user authentication service registry

Introduced in front of [ set up service registry ], and [ build highly available service registry ], to see if you can build a service registry are not familiar with what the previous two little friends.

Before building service registry, only you need to enter the address and port can be registered, if this is a production environment is very insecure, I just know that your registry address, I can not be directly registered service up, and get you registry of the service. So tell us about this article to set up a registry for user authentication.

Still using the previous build eureka-server-test project to transform in order to facilitate the test, using a service registry here of a single node.

Add dependent

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

Modify the configuration file

Here profile from .properties changed .yml, still feel yml format looked comfortable.
Eureka.client.serviceUrl which has been modified to add a user name and password, the old version of the open spring.security certified security.basic.enabled = true no longer valid.

server:
  port: 8081
  waitTimeInMsWhenSyncEmpty: 0
  enableSelfPreservation: false
eureka:
   instance:
      hostname: localhost
   client:
      registerWithEureka: false
      fetchRegistry: false
      serviceUrl:
        defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/
spring:
  security:
    user:
      name: admin
      password: 123456

Add the startup file

In the spring boot 2.0.3, the default opened csrf certification, we are here to manually shut down, then turn httpBasic certification, similar to the previous versions and configuration files security.basic.enabled = true role.

package clarezhou.example.common;

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;

/**
 * @author clarezhou
 * @date 2019/6/25 10:31
 **/
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //开启认证
        //为了可以使用 http://${user}:${password}@${host}:${port}/eureka/ 这种方式登录,所以必须是httpBasic
        http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
    }
}

Eureka Server service registry to transform over, and then start the service, enter the registration center address in a browser, you will see the following bullet box.
Here Insert Picture Description
Enter the account number and password can enter the registry.

Transformation of the client

The client also use this project to transform a previously created test eureka-client-, here only need to modify the configuration file, modify serviceUrl.defaultZone
address, in front of the original address plus the username: password @ information.

eureka.client.serviceUrl.defaultZone=http://admin:123456@localhost:8081/eureka/

Then start the client, see the registration information on OK.

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/clarezhou/p/11082252.html