UserPass 身份验证库
UserPass Authentication Vault
我一直在尝试在我的应用程序中使用 Hashicorp Vault(来自 here)的身份验证方法来获取配置。
但无法在 Spring 的文档、示例等中获得有关此身份验证类型的任何信息。你能帮帮我吗,因为我需要这种类型的身份验证来帮助我在多用户中使用保险库环境。
这是我的解决方案:
配置class:
package com.company.myapp.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.vault.VaultException;
import org.springframework.vault.authentication.ClientAuthentication;
import org.springframework.vault.support.VaultToken;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
@Configuration
public class VaultConfig {
public static final String LOGIN_PATH = "/v1/auth/userpass/login/";
@Bean
public ClientAuthentication clientAuthentication(@Value("${VAULT_USERNAME}") String username,
@Value("${VAULT_PASSWORD}") String password,
@Value("${spring.cloud.vault.uri}") String host) {
return new UserPassAuthentication(host, LOGIN_PATH, username, password);
}
public static class UserPassAuthentication implements ClientAuthentication {
private RestOperations restOperations = new RestTemplate();
private String url;
private String password;
public UserPassAuthentication(String host, String path, String user, String password) {
this.url = new StringBuilder(host).append(path).append(user).toString();
this.password = password;
}
@Override
public VaultToken login() throws VaultException {
return VaultToken.of(
((Map<String, String>) restOperations.postForEntity(url, new Password(password), Map.class)
.getBody().get("auth")).get("client_token"));
}
}
static class Password {
private String password;
public Password(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
resources/bootstrap.属性:
spring.profiles.active=dev
spring.application.name=myapp
spring.cloud.vault.kv.enabled=true
spring.cloud.vault.kv.backend=test-backend
spring.cloud.vault.uri=https://localhost:8200
VAULT_USERNAME=usr
VAULT_PASSWORD=pwd
resources/META-INF/spring.工厂
org.springframework.cloud.bootstrap.BootstrapConfiguration=com.company.myapp.config.VaultConfig
使用这个解决方案我遇到了 2 个问题。
- 覆盖 spring 预配置 bean。
解决方法:添加
spring.main.allow-bean-definition-overriding =true
在您的 bootstrap.properties 文件中。这允许 bean 定义覆盖。
- beanInstantiationException: 因为身份验证步骤没有正确定义。
解决方案:我使用的token认证方式如下:
public ClientAuthentication clientAuthentication() {
return new TokenAuthentication(new UserPassAuthentication("host", path, "user", "password").login());
}
其余代码与之前完全相同。
我一直在尝试在我的应用程序中使用 Hashicorp Vault(来自 here)的身份验证方法来获取配置。
但无法在 Spring 的文档、示例等中获得有关此身份验证类型的任何信息。你能帮帮我吗,因为我需要这种类型的身份验证来帮助我在多用户中使用保险库环境。
这是我的解决方案:
配置class:
package com.company.myapp.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.vault.VaultException;
import org.springframework.vault.authentication.ClientAuthentication;
import org.springframework.vault.support.VaultToken;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
@Configuration
public class VaultConfig {
public static final String LOGIN_PATH = "/v1/auth/userpass/login/";
@Bean
public ClientAuthentication clientAuthentication(@Value("${VAULT_USERNAME}") String username,
@Value("${VAULT_PASSWORD}") String password,
@Value("${spring.cloud.vault.uri}") String host) {
return new UserPassAuthentication(host, LOGIN_PATH, username, password);
}
public static class UserPassAuthentication implements ClientAuthentication {
private RestOperations restOperations = new RestTemplate();
private String url;
private String password;
public UserPassAuthentication(String host, String path, String user, String password) {
this.url = new StringBuilder(host).append(path).append(user).toString();
this.password = password;
}
@Override
public VaultToken login() throws VaultException {
return VaultToken.of(
((Map<String, String>) restOperations.postForEntity(url, new Password(password), Map.class)
.getBody().get("auth")).get("client_token"));
}
}
static class Password {
private String password;
public Password(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
resources/bootstrap.属性:
spring.profiles.active=dev
spring.application.name=myapp
spring.cloud.vault.kv.enabled=true
spring.cloud.vault.kv.backend=test-backend
spring.cloud.vault.uri=https://localhost:8200
VAULT_USERNAME=usr
VAULT_PASSWORD=pwd
resources/META-INF/spring.工厂
org.springframework.cloud.bootstrap.BootstrapConfiguration=com.company.myapp.config.VaultConfig
使用这个解决方案我遇到了 2 个问题。
- 覆盖 spring 预配置 bean。
解决方法:添加
spring.main.allow-bean-definition-overriding =true
在您的 bootstrap.properties 文件中。这允许 bean 定义覆盖。
- beanInstantiationException: 因为身份验证步骤没有正确定义。
解决方案:我使用的token认证方式如下:
public ClientAuthentication clientAuthentication() { return new TokenAuthentication(new UserPassAuthentication("host", path, "user", "password").login()); }
其余代码与之前完全相同。