获取用于身份验证的 kubernetes secret / env 进入 java 应用程序
Getting kubernetes secret / env for authentication into java application
我有一个使用 Kubernetes 的 java spring.boot
应用程序,我已经配置了这个 .yaml
文件
- name: ACTUATOR_USERNAME
valueFrom:
secretKeyRef:
name: actuator
key: username
- name: ACTUATOR_PASSWORD
valueFrom:
secretKeyRef:
name: actuator
key: password
将此属性添加到我的 application.propertis
security.user.name=${ACTUATOR_USERNAME}
security.user.password=${ACTUATOR_PASSWORD}
secret 是在服务器端创建的,我如何在 class
中检索这些值
package com.greenqloud.usage.healthcheck;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
.anyRequest().hasRole("USER")
.and()
.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("actuator username: " + System.getenv("ACTUATOR_USERNAME"));
System.out.println("actuator password: " + System.getenv("ACTUATOR_PASSWORD"));
auth.inMemoryAuthentication()
.withUser("actuator").password("{noop}actuator123").roles("USER");
}
}
我找到的唯一方法是使用 System.out.getenv("ACTUATOR_USERNAME")
,但我确定有更好的方法来实现它?
System.getenv()用于获取环境变量值;没有什么问题。但是,由于您使用的是 SpringBoot,您可能会发现以下问题很有价值:
我同意@Kuikiker 关于 getenv() 的观点。但有一个问题,您为什么要将凭据存储在 env 变量中。除非您有特殊需要,否则我相信您最好将它们以加密值存储在 application.properties 中。我通常为此使用 jasypt encipher (https://www.baeldung.com/spring-boot-jasypt)。
希望这有帮助。
我有一个使用 Kubernetes 的 java spring.boot
应用程序,我已经配置了这个 .yaml
文件
- name: ACTUATOR_USERNAME
valueFrom:
secretKeyRef:
name: actuator
key: username
- name: ACTUATOR_PASSWORD
valueFrom:
secretKeyRef:
name: actuator
key: password
将此属性添加到我的 application.propertis
security.user.name=${ACTUATOR_USERNAME}
security.user.password=${ACTUATOR_PASSWORD}
secret 是在服务器端创建的,我如何在 class
中检索这些值package com.greenqloud.usage.healthcheck;
import org.springframework.boot.actuate.autoconfigure.security.servlet.EndpointRequest;
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.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.requestMatcher(EndpointRequest.toAnyEndpoint()).authorizeRequests()
.anyRequest().hasRole("USER")
.and()
.httpBasic();
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("actuator username: " + System.getenv("ACTUATOR_USERNAME"));
System.out.println("actuator password: " + System.getenv("ACTUATOR_PASSWORD"));
auth.inMemoryAuthentication()
.withUser("actuator").password("{noop}actuator123").roles("USER");
}
}
我找到的唯一方法是使用 System.out.getenv("ACTUATOR_USERNAME")
,但我确定有更好的方法来实现它?
System.getenv()用于获取环境变量值;没有什么问题。但是,由于您使用的是 SpringBoot,您可能会发现以下问题很有价值:
我同意@Kuikiker 关于 getenv() 的观点。但有一个问题,您为什么要将凭据存储在 env 变量中。除非您有特殊需要,否则我相信您最好将它们以加密值存储在 application.properties 中。我通常为此使用 jasypt encipher (https://www.baeldung.com/spring-boot-jasypt)。 希望这有帮助。