Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用SpringRESTTemplate在JHipster中访问SpringRESTAPI_Spring_Rest_Spring Security_Resttemplate_Jhipster - Fatal编程技术网

如何使用SpringRESTTemplate在JHipster中访问SpringRESTAPI

如何使用SpringRESTTemplate在JHipster中访问SpringRESTAPI,spring,rest,spring-security,resttemplate,jhipster,Spring,Rest,Spring Security,Resttemplate,Jhipster,我已经与一些实体建立了JHipster主页上所描述的那样。使用AngularJS的前端工作得很好,还有API页面,让我可以按预期测试我的服务 现在,我尝试使用Spring的RestTemplate编写一个REST客户端,如下所示: public List<SomeEntity> getAllEntities(){ URI uri = URI.create("http://localhost:8080/api/entities"); HttpHeaders http

我已经与一些实体建立了JHipster主页上所描述的那样。使用AngularJS的前端工作得很好,还有API页面,让我可以按预期测试我的服务

现在,我尝试使用Spring的RestTemplate编写一个REST客户端,如下所示:

public List<SomeEntity> getAllEntities(){
     URI uri = URI.create("http://localhost:8080/api/entities");
     HttpHeaders httpHeaders = this.createHeaders("admin", "admin")
     ResponseEntity<SomeEntity[]> responseEntity =  restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<SomeEntity>(httpHeaders), SomeEntity[].class);
     return Arrays.asList(responseEntity.getBody());
}


private HttpHeaders createHeaders(final String username, final String password ){
HttpHeaders headers =  new HttpHeaders(){
      {
         String auth = username + ":" + password;
         byte[] encodedAuth = Base64.encode(
            auth.getBytes(Charset.forName("US-ASCII")) );
         String authHeader = "Basic " + new String( encodedAuth );
         set( "Authorization", authHeader );
      }
   };
   headers.add("Content-Type", "application/json");
   headers.add("Accept", "application/json");

   return headers;
}
公共列表getAllenties(){
URI=URI.create(“http://localhost:8080/api/entities");
HttpHeaders HttpHeaders=this.createHeaders(“admin”、“admin”)
ResponseEntity ResponseEntity=restTemplate.exchange(uri、HttpMethod.GET、新HttpEntity(httpHeaders)、SomeEntity[].class);
返回Arrays.asList(responseEntity.getBody());
}
私有HttpHeaders createHeaders(最终字符串用户名、最终字符串密码){
HttpHeaders=新的HttpHeaders(){
{
字符串auth=username+“:”+密码;
字节[]encodedAuth=Base64.encode(
auth.getBytes(Charset.forName(“US-ASCII”));
字符串authHeader=“Basic”+新字符串(encodedAuth);
设置(“授权”,authHeader);
}
};
添加(“内容类型”、“应用程序/json”);
添加(“接受”、“应用程序/json”);
返回标题;
}
但这会导致以下错误: [警告]org.springframework.web.client.restemplate-获取“”请求导致401(未经授权);调用错误处理程序


现在我不确定是否需要以及如何调整我的HttpHeaders,或者我简单的基本身份验证处理方法是否错误。

您的身份验证方式是错误的,似乎您在生成应用程序时选择了会话身份验证,因此,这需要基于表单的身份验证,而不是http基本身份验证,并且需要能够存储会话cookie和CSRF cookie,因此最有可能使用commons http客户端

也许在生成应用程序时选择xauth令牌身份验证会更简单

一旦您的客户端无法在与JHipster应用程序相同的主机上运行,您就会遇到CORS问题。

这看起来很相关: