如何在springboot2.x中實(shí)現(xiàn)oauth2授權(quán)碼登陸?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)是一家專注于成都做網(wǎng)站、成都網(wǎng)站建設(shè)與策劃設(shè)計(jì),淮陽網(wǎng)站建設(shè)哪家好?成都創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:淮陽等地區(qū)?;搓栕鼍W(wǎng)站價(jià)格咨詢:13518219792
@Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient(ClientID) .secret(passwordEncoder.encode(ClientSecret)) .authorizedGrantTypes("authorization_code", "refresh_token", "password", "implicit") .scopes("read","write","del","userinfo") .redirectUris(RedirectURLs); }
四 接到code
授權(quán)之后,系統(tǒng)會(huì)重定向到你的redirect_uri這個(gè)頁面,并帶上唯一的code
五 獲取access_token
我們拿著code就要再去授權(quán)服務(wù)器去獲取token了,你可以在你的代碼里寫這個(gè),也可以手動(dòng)拿著code,去拼成一個(gè)url,再去拿token,就像這下面的實(shí)例。
注意向oauth/token發(fā)的是post請(qǐng)求,client_id和client_secret如果在url上傳遞,如果在AuthorizationServerConfig
類的configure方法中開啟allowFormAuthenticationForClients
,代碼如下
@Override public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.tokenKeyAccess("permitAll()") .checkTokenAccess("isAuthenticated()") .allowFormAuthenticationForClients();//支持把secret和clientid寫在url上,否則需要在頭上 }
然后請(qǐng)求后給有下面的響應(yīng)
Authorization Ccode------RFRLFY access_token_url http://localhost:8081/oauth/token?client_id=android1&code=RFRLFY&grant_type=authorization_code&redirect_uri=http://localhost:8081/callback&client_secret=android1 Access Token Response ---------{"access_token":"faadf3bf-6488-4036-bc3b-21b0a979602c","token_type":"bearer","refresh_token":"1b01f133-c5ab-419f-8125-088c85916ecb","expires_in":43187,"scope":"read"}
回調(diào)頁面代碼,主要實(shí)現(xiàn)了對(duì)code的獲取,對(duì)access_token的組織,然后請(qǐng)求時(shí)把a(bǔ)ccess_token帶上,這個(gè)方法一般會(huì)做成公用的過濾器
@Controller public class UserController { @RequestMapping(value = "/callback", method = RequestMethod.GET) public ResponseEntitycallback(@RequestParam("code") String code) throws JsonProcessingException, IOException { ResponseEntity response = null; System.out.println("Authorization Ccode------" + code); RestTemplate restTemplate = new RestTemplate(); String access_token_url = "http://localhost:8081/oauth/token"; access_token_url += "?client_id=android1&code=" + code; access_token_url += "&grant_type=authorization_code"; access_token_url += "&redirect_uri=http://localhost:8081/callback"; access_token_url += "&client_secret=android1"; System.out.println("access_token_url " + access_token_url); response = restTemplate.exchange(access_token_url, HttpMethod.POST, null, String.class); ObjectMapper mapper = new ObjectMapper(); JsonNode node = mapper.readTree(response.getBody()); String token = node.path("access_token").asText(); System.out.println("access_token" +access_token); String url = "http://localhost:8081/index"; HttpHeaders headers1 = new HttpHeaders(); headers1.add("Authorization", "Bearer " + token); HttpEntity entity = new HttpEntity<>(headers1); ResponseEntity result = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); return result; }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。