【问题标题】:Consuming Secure rest services in Spring在 Spring 中使用 Secure REST 服务
【发布时间】:2017-04-28 05:38:17
【问题描述】:

我必须使用一个安全的休息服务,我最初将凭据传递给服务,它将返回令牌,对于后续请求,我必须传递生成的令牌。如何在 springs 中实现这些功能?

【问题讨论】:

    标签: spring rest model-view-controller service


    【解决方案1】:

    对于消费休息服务,您可以使用RestTemplate

    1.简单的解决方案如果您已经拥有令牌:

    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", "Bearer " + token); // add auth header with token
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    restTemplate.exchange("URL",HttpMethod.GET,entity,Object.class);
    

    2。或者更好的解决方案是使用ClientHttpRequestInterceptor。您创建新的RestTemplate 并添加您自己的拦截器,它将获取令牌并设置为headers

    BasicAuth 的简单示例

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 2017-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      相关资源
      最近更新 更多