public AuthToken applyToken(String username, String password, String clientId, String clientSecret) {
? ? ? ? StringBuffer stringBuffer=new StringBuffer();
? ? ? ? stringBuffer.append("grant_type=password&scope=read write");
? ? ? ? stringBuffer.append("&client_id=" + clientId);
? ? ? ? stringBuffer.append("&client_secret="+clientSecret);
? ? ? ? stringBuffer.append("&username=" + username);
? ? ? ? stringBuffer.append("&password=" + password);
? ? ? ? //申請令牌的url
? ? ? ? String authUrl = httpUrl + "/oauth/token?" + stringBuffer.toString();
????????//? ? ? ? authUrl = authUrl.replaceAll(" ","%20");? //實踐不需要替換空格纵竖,restTemplate能識別
? ? ? ? log.info("-----------------authUrl:" + authUrl);
????????//定義header
? ? ? ? LinkedMultiValueMap<String, String> header = new LinkedMultiValueMap<>();
? ? ? ? String httpBasic = getHttpBasic(clientId, clientSecret);
? ? ? ? header.add("Authorization",httpBasic);
? ? ? ? //定義body,用body會報錯
? ? ? ? LinkedMultiValueMap<String, String> body = new LinkedMultiValueMap<>();
//? ? ? ? body.add("grant_type","password");
//? ? ? ? body.add("username",username);
//? ? ? ? body.add("password",password);
? ? ? ? HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(body, header);
? ? ? ? //String url, HttpMethod method, @Nullable HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables
? ? ? ? //設(shè)置restTemplate遠(yuǎn)程調(diào)用時候,對400和401不讓報錯,正確返回數(shù)據(jù)
? ? ? ? restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){
? ? ? ? ? ? @Override
? ? ? ? ? ? public void handleError(ClientHttpResponse response) throws IOException {
? ? ? ? ? ? ? ? if(response.getRawStatusCode()!=400 && response.getRawStatusCode()!=401){
? ? ? ? ? ? ? ? ? ? super.handleError(response);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? ResponseEntity<Map> exchange = restTemplate.exchange(authUrl, HttpMethod.POST, httpEntity, Map.class);
? ? ? ? //申請令牌信息
? ? ? ? Map bodyMap = exchange.getBody();
? ? ? ? if (bodyMap == null ||
? ? ? ? ? ? ? ? bodyMap.get("access_token") == null ||
? ? ? ? ? ? ? ? bodyMap.get("refresh_token") == null ||
? ? ? ? ? ? ? ? bodyMap.get("jti") == null) {
????????????//解析spring security返回的錯誤信息
? ? ? ? ? ? if (bodyMap != null && bodyMap.get("error_description") != null) {
? ? ? ? ? ? ? ? String error_description = (String) bodyMap.get("error_description");
? ? ? ? ? ? ? ? if (error_description.indexOf("UserDetailsService returned null") >= 0) {
//? ? ? ? ? ? ? ? ? ? ExceptionCast.cast(AuthCode.AUTH_ACCOUNT_NOTEXISTS);
? ? ? ? ? ? ? ? } else if (error_description.indexOf("壞的憑證") >= 0) {
//? ? ? ? ? ? ? ? ? ? ExceptionCast.cast(AuthCode.AUTH_CREDENTIAL_ERROR);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return null;
? ? ? ? }
? ? ? ?AuthToken authToken = new AuthToken();
? ? ? ? authToken.setAccess_token((String) bodyMap.get("jti"));//用戶身份令牌
? ? ? ? authToken.setRefresh_token((String) bodyMap.get("refresh_token"));//刷新令牌
? ? ? ? authToken.setJwt_token((String) bodyMap.get("access_token"));//jwt令牌
? ? ? ? return authToken;
? ? }
????//獲取httpbasic的串
? ? private String getHttpBasic(String clientId, String clientSecret) {
? ? ? ? String string = clientId + ":" + clientSecret;
? ? ? ? //將串進(jìn)行base64編碼
? ? ? ? byte[] encode = Base64Utils.encode(string.getBytes());
? ? ? ? return "Basic " + new String(encode);
? ? }