Spring-boot oauth2使用RestTemplate進(jìn)行后臺(tái)自動(dòng)登錄的實(shí)現(xiàn)
內(nèi)容不限于登錄業(yè)務(wù),主要簡(jiǎn)單介紹RestTemplate的用法,包括
使用RestTemplate進(jìn)行post請(qǐng)求 postForObject 使用RestTemplate帶body/form-data進(jìn)行post請(qǐng)求 MultiValueMap 使用RestTemplate帶josn進(jìn)行post請(qǐng)求JSONObject 使用RestTemplate帶頭信息headers進(jìn)行post請(qǐng)求 HttpHeaders登錄流程
定義 RestTemplate 定義 MultiValueMap,構(gòu)造 post的body內(nèi)容 定義 HttpHeaders,構(gòu)造請(qǐng)求的頭部信息 定義 HttpEntity,發(fā)送請(qǐng)求的實(shí)體 定義 RestTemplate,進(jìn)行請(qǐng)求。返回?cái)?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你的認(rèn)證密鑰'); // 設(shè)置類型 'application/json;charset=UTF-8' headers.setContentType(MediaType.APPLICATION_JSON); // 構(gòu)造請(qǐng)求的實(shí)體。包含body和headers的內(nèi)容 HttpEntity<MultiValueMap<String, String>> request = new HttpEntity(paramsMap, headers); // 聲明 restTemplateAuth(用作請(qǐng)求) RestTemplate restTemplateAuth = new RestTemplate(); // 進(jìn)行請(qǐng)求,并返回?cái)?shù)據(jù) String authInfo = restTemplateAuth.postForObject('http://localhost:8089/oauth/token', request, String.class);
使用josn請(qǐng)求的示例代碼
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進(jìn)行后臺(tái)自動(dòng)登錄的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Spring-boot oauth2 后臺(tái)自動(dòng)登錄內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 用xslt+css讓RSS顯示的跟網(wǎng)頁一樣漂亮2. ASP.NET MVC把數(shù)據(jù)庫中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字3. 《CSS3實(shí)戰(zhàn)》筆記--漸變?cè)O(shè)計(jì)(一)4. 測(cè)試模式 - XSL教程 - 55. Ajax實(shí)現(xiàn)異步加載數(shù)據(jù)6. 教你JS更簡(jiǎn)單的獲取表單中數(shù)據(jù)(formdata)7. ASP.NET Core自定義中間件的方式詳解8. html5手機(jī)觸屏touch事件介紹9. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效10. 讓chatgpt將html中的圖片轉(zhuǎn)為base64方法示例
