Outh2 + RestTemplate,我无法获取列表属性。未经授权的401

时间:2019-03-09 11:23:05

标签: spring rest spring-boot oauth-2.0 resttemplate

我不知道为什么我无法获得列表产品。请帮我

@RestController @RequestMapping(“ / flat”) 公共类AttrController {

ResponseEntity responseEntity;
String client_id="web-app-agent";
String 

client_secret =“ Z | AOSFVM5oIa3354645f〜No〜Aq0xgmV6 @ L#y6z {”;     @RequestMapping(值=“ / att”,方法= RequestMethod.GET,产生= MediaType.APPLICATION_JSON_VALUE)     公共ResponseEntity >> getAllByApi()引发IOException {         RestTemplate restTemplate =新的RestTemplate();         HttpHeaders标头=新的HttpHeaders();

    String auth = client_id + ":" + client_secret;

    headers.set("Accept", "application/json");
    headers.set("grant_type", "password");
    headers.set("username", "test@test");
    headers.set("password", "1234");
    headers.set("Authorization", "Basic " + Base64.encodeBase64String(auth.getBytes(Charset.forName("UTF-8"))));

    HttpEntity< String> request = new HttpEntity<>(headers);
    ResponseEntity<String> res = restTemplate.exchange("http://agent/oauth/token",HttpMethod.POST,request,String.class);
    System.out.println("#### get response = " + res.getBody());

    String responseText = res.getBody();
    HashMap tokenMap = new ObjectMapper().readValue(responseText, HashMap.class);
    String tokenN = (String) tokenMap.get("access_token");

    System.out.println("Tokennn"+tokenN);
    HttpHeaders headers1 = new HttpHeaders();
    headers1.set("Authorization", "Bearer " + tokenN);
    headers1.set("Accept", "application/json");
    headers1.set("grant_type", "password");
    headers1.set("username", "test@test");
    headers1.set("password", "1234");
    HttpEntity<String> request1 = new HttpEntity<>(headers1);

    ResponseEntity<Attribute> attributes = restTemplate.exchange("http://agent:8082/flat/attributes",HttpMethod.GET,request1,Attribute.class);

    return new ResponseEntity(attributes, HttpStatus.OK);
}
}

0 个答案:

没有答案