我有一个spring.boot使用Kubernetes的java 应用程序,我配置了这个.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,如何在我的类中检索这个值
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")但是我确定有更好的方法来实现这一目标吗?
为什么你想在env变量中存储凭证。除非您有特殊需要,否则我相信您最好将它们存储在具有加密值的application.properties中。我通常使用jasypt encipher(https://www.baeldung.com/spring-boot-jasypt)。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。