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