springboot的RestTemplate客户端模式取得uaa的token
@Scheduled(fixedRate = 5000) public String getAuthorize() { HttpHeaders reqHeaders = new HttpHeaders(); reqHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String, String> formParams = new LinkedMultiValueMap<>(); formParams.set("client_id", "internal"); formParams.set("client-secret", "internal"); formParams.set("grant_type", "client_credentials"); String authorization = "Basic " + Base64Utils.encodeToString("internal:internal".getBytes(StandardCharsets.UTF_8)); reqHeaders.add("authorization", authorization); HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(formParams, reqHeaders); RestTemplate restTemplate = new RestTemplate(); String gatewayURL = "http://127.0.0.1:9999/oauth/token"; ResponseEntity<String> responseEntity = restTemplate.postForEntity(gatewayURL, entity, String.class); log.info(" token===" + responseEntity.getBody()); return responseEntity.getBody(); }
日志需要注解
@Slf4j
如果想启用定时器还需要在启动类增加注解
@EnableScheduling @SpringBootApplication public class ClientmodelApplication