Spring-boot oauth2使用RestTemplate進行后臺自動登錄的實現(xiàn)
內(nèi)容不限于登錄業(yè)務(wù),主要簡單介紹RestTemplate的用法,包括
使用RestTemplate進行post請求 postForObject 使用RestTemplate帶body/form-data進行post請求 MultiValueMap 使用RestTemplate帶josn進行post請求JSONObject 使用RestTemplate帶頭信息headers進行post請求 HttpHeaders登錄流程
定義 RestTemplate 定義 MultiValueMap,構(gòu)造 post的body內(nèi)容 定義 HttpHeaders,構(gòu)造請求的頭部信息 定義 HttpEntity,發(fā)送請求的實體 定義 RestTemplate,進行請求。返回數(shù)據(jù)主要代碼
// 構(gòu)造 post的body內(nèi)容(要post的內(nèi)容,按需定義) MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>(); paramsMap.set('grant_type', 'password'); paramsMap.set('username', 'yourname'); paramsMap.set('password', 'yourpassword'); // 構(gòu)造頭部信息(若有需要) HttpHeaders headers = new HttpHeaders(); headers.add('Authorization', 'Basic xxxxxx你的認證密鑰'); // 設(shè)置類型 'application/json;charset=UTF-8' headers.setContentType(MediaType.APPLICATION_JSON); // 構(gòu)造請求的實體。包含body和headers的內(nèi)容 HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(paramsMap, headers); // 聲明 restTemplateAuth(用作請求) RestTemplate restTemplateAuth = new RestTemplate(); // 進行請求,并返回數(shù)據(jù) String authInfo = restTemplateAuth.postForObject('http://localhost:8089/oauth/token', request, String.class);
使用josn請求的示例代碼
Posting JSON with postForObject JSONObject personJsonObject = new JSONObject(); personJsonObject.put('id', 1); personJsonObject.put('name', 'John'); HttpEntity<String> request = new HttpEntity<String>(personJsonObject.toString(), headers); String personResultAsJsonStr = restTemplate.postForObject('url', request, String.class);
到此這篇關(guān)于Spring-boot oauth2使用RestTemplate進行后臺自動登錄的實現(xiàn)的文章就介紹到這了,更多相關(guān)Spring-boot oauth2 后臺自動登錄內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP基礎(chǔ)知識VBScript基本元素講解2. ajax請求添加自定義header參數(shù)代碼3. Python requests庫參數(shù)提交的注意事項總結(jié)4. IntelliJ IDEA導(dǎo)入jar包的方法5. Kotlin + Flow 實現(xiàn)Android 應(yīng)用初始化任務(wù)啟動庫6. 詳談ajax返回數(shù)據(jù)成功 卻進入error的方法7. 使用Python和百度語音識別生成視頻字幕的實現(xiàn)8. 使用python 計算百分位數(shù)實現(xiàn)數(shù)據(jù)分箱代碼9. python操作mysql、excel、pdf的示例10. vue-electron中修改表格內(nèi)容并修改樣式
