Các rất cơ bản "xác thực cơ bản" (từ đây https://github.com/spring-cloud-samples/configserver)
Bạn có thể thêm xác thực HTTP Basic bằng cách bao gồm một sự phụ thuộc thêm vào Xuân An (ví dụ như thông qua lò xo khởi động khởi động -Bảo vệ). Tên người dùng là "người dùng" và mật khẩu được in trên bàn điều khiển khi khởi động (phương pháp khởi động Spring Boot chuẩn). Nếu sử dụng maven (pom.xml
):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Nếu bạn muốn cặp người dùng/mật khẩu tùy chỉnh, bạn cần chỉ rõ trong tập tin cấu hình máy chủ
security:
basic:
enabled: false
và thêm lớp tối thiểu này trong mã của bạn (BasicSecurityConfiguration.java
):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
//@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class BasicSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("#{'${qa.admin.password:admin}'}") //property with default value
String admin_password;
@Value("#{'${qa.user.password:user}'}") //property with default value
String user_password;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password(user_password).roles("USER")
.and()
.withUser("admin").password(admin_password).roles("USER", "ACTUATOR");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/encrypt/**").authenticated()
.antMatchers("/decrypt/**").authenticated()
//.antMatchers("/admin/**").hasAuthority("ROLE_ACTUATOR")
//.antMatchers("/qa/**").permitAll()
;
}
}
@Value ("# {'$ {qa.admin.password: admin}'}") cho phép mật khẩu được xác định trong tệp cấu hình thuộc tính, biến môi trường hoặc dòng lệnh.
Ví dụ (application.yml
):
server:
port: 8888
security:
basic:
enabled: false
qa:
admin:
password: adminadmin
user:
password: useruser
management:
port: 8888
context-path: /admin
logging:
level:
org.springframework.cloud: 'DEBUG'
spring:
cloud:
config:
server:
git:
ignoreLocalSshSettings: true
uri: ssh://[email protected]/repo/configuration.git
này làm việc cho tôi.
Chỉnh sửa: Thay vì các Class, bạn có thể đặt cấu hình người dùng cơ bản trực tiếp trong application.yaml
:
security:
basic:
enabled: true
path: /**
ignored: /health**,/info**,/metrics**,/trace**
user:
name: admin
password: tupassword
Bạn có thể cố gắng để có một giữ chỗ cho mật khẩu trong bootstrap.yml của bạn và đề cập đến những mật khẩu như là một biến môi trường. –