在春季启动中无法从vcap_services中提供的用户中检索凭证 [英] Unable to retrieve credentials from user-provided in vcap_services in spring boot

查看:70
本文介绍了在春季启动中无法从vcap_services中提供的用户中检索凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的spring boot应用程序在PCF中,因为PCF没有在运行时更改属性文件的选项,所以我试图将这些值放入PCF VCAP_SERVICES用户提供的凭据中.

My spring boot application is in PCF, since PCF doesnt have option to change properties file in run time, I was trying to put the values in PCF VCAP_SERVICES- user-provided credentials.

我尝试了根据枢纽和我有空异常.

I tried using @ConfigurationProperties as per tutorial provided by pivotal and i got null exception.

@Data
@Configuration
@ConfigurationProperties("vcap.services.app-properties.credentials")
public class RsTest {
private String username;
private String password;
//getter and setter
};

我的控制器看起来像

@RestController
public class RestApiController {
@Autowired
RsTest rsTest;

public void test() {
logger.info("RSTest: "+rsTest.getUsername());
return ResponseEntity.ok().body("some value");
}

我期望RsTest对象中的凭据.但是有错误路径为[/myservice]的Servlet [dispatcherServlet]的Servlet.service()抛出异常2019-08-20T17:32:43.728-04:00 [APP/PROC/WEB/0] [OUT] java.lang.NullPointerException:空

I'm expecting the credential in RsTest object. But having error Servlet.service() for servlet [dispatcherServlet] in context with path [/myservice] threw exception 2019-08-20T17:32:43.728-04:00 [APP/PROC/WEB/0] [OUT] java.lang.NullPointerException: null

推荐答案

嗯,理论上您应该拥有的东西.但是,这是一种从VCAP_SERVICES解析配置的脆弱方法,这就是我猜测您为什么会遇到问题的原因. @ConfigurationProperties 的前缀必须完全正确,以使Spring可以查找该值,并且该前缀将取决于您绑定的服务的名称.

Well, what you have should in theory work. It is a fragile approach to parsing config from VCAP_SERVICES though, and that's my guess as why you're having issues. The prefix for @ConfigurationProperties must be exactly correct for Spring to look up the value, and the prefix will depend on the name of the service you bind.

Spring Boot将以以下格式映射与您的应用程序绑定的服务: vcap.services.<服务名称> .credentials.< credential-key> .有关详细信息,请参见文档这里.

Spring Boot will map services bound to your app in the format: vcap.services.<service name>.credentials.<credential-key>. For details, see the docs here.

如果您没有正确的服务实例名称,则它将无法绑定到您的配置属性对象.

If you do not have the correct service instance name, then it will fail to bind to your config properties object.

这是一个例子:

  1. 我有一个名为 scheduler 的服务.
  2. 它产生以下VCAP_SERVICES env变量:

  1. I have a service named scheduler.
  2. It produces the following VCAP_SERVICES env variable:

{
  "scheduler-for-pcf": [
   {
    "binding_name": null,
    "credentials": {
     "api_endpoint": "https://scheduler.run.pivotal.io"
    },
    "instance_name": "scheduler",
    "label": "scheduler-for-pcf",
    "name": "scheduler",
    "plan": "standard",
    "provider": null,
    "syslog_drain_url": null,
    "tags": [
     "scheduler"
    ],
    "volume_mounts": []
   }
  ]
 }

  • 我可以使用以下类来读取其凭据.

  • I can use the following class to read it's credentials.

    @Configuration
    @ConfigurationProperties(prefix = "vcap.services.scheduler.credentials")
    public class SchedulerConfig {
        private String api_endpoint;
    
        public String getApiEndpoint() {
            return api_endpoint;
        }
    
        public void setApiEndpoint(String api_endpoint) {
            this.api_endpoint = api_endpoint;
        }
    }
    

  • 如果我将服务名称更改为 fred ,则前缀将需要更改为 vcap.services.fred.credentials .

    If I changed the service name to fred, then the prefix would need to change to vcap.services.fred.credentials.

    话虽如此,您应该看看使用 java-cfenv .它更加灵活,是在Java应用程序中读取VCAP_SERVICES的推荐方法(请注意-代替了Spring Cloud Connectors).

    Having said all that, you should look at using java-cfenv instead. It's more flexible and is the recommended way to read VCAP_SERVICES in your Java apps (note - this replaces Spring Cloud Connectors).

    有关更多详细信息,请阅读

    For more details, read this blog post

    这篇关于在春季启动中无法从vcap_services中提供的用户中检索凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆