本篇內(nèi)容主要講解“怎么使用OAuth 2.0開發(fā)認(rèn)證中心和資源服務(wù)器接入”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么使用OAuth 2.0開發(fā)認(rèn)證中心和資源服務(wù)器接入”吧!
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)山東,十載網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
基于 Spring Cloud OAuth ,用簡(jiǎn)潔的方式搭建oauth的認(rèn)證中心,
關(guān)于oauth3 的授權(quán)模式 請(qǐng)直接參考 阮一峰 OAuth 2.0 的四種方式的詳細(xì)介紹
項(xiàng)目版本核心說明
| 名稱 | 版本 |
|---|---|
| Spring Boot | 2.2.0.M5 |
| Spring Cloud | Hoxton.M2 |
| Spring Cloud OAuth3 | 2.2.0.M2 |
這里只需要引入web、 cloud-oauth 即可,暫不引入spring cloud 其他組件
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-oauth3</artifactid> </dependency> </dependencies>
獲取web 上下文AuthenticationManager 注入到spring中,方便后邊oauth server注入
創(chuàng)建UserDetailsService的內(nèi)存實(shí)現(xiàn),注入一個(gè)測(cè)試用戶
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
/**
* 必須注入 AuthenticationManager,不然oauth 無法處理四種授權(quán)方式
*
* @return
* @throws Exception
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* 必須注入U(xiǎn)serDetailsService ,不然oauth 密碼模式等死循環(huán)問題
*
* @return
*/
@Bean
@Override
protected UserDetailsService userDetailsService() {
InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
userDetailsManager.createUser(User.withUsername("lengleng").password("{noop}lengleng").authorities("USER").build());
return userDetailsManager;
}
}配置clientId 信息,及其支持的授權(quán)模式,特別注意這里是五種包含一個(gè)刷新操作
@Configuration
@EnableAuthorizationServer
public class BigAuthServerConfiguration extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("appid")
.secret("{noop}secret")
.authorizedGrantTypes("password", "authorization_code", "client_credentials", "implicit", "refresh_token")
.scopes("all");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService);
}
}以上完成了認(rèn)證服務(wù)器的功能
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&username=lengleng&password=lengleng&scope=all' "http://appid:secret@localhost:8764/oauth/token"
這里只需要引入web、 cloud-oauth 即可,暫不引入spring cloud 其他組件
<dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-oauth3</artifactid> </dependency> </dependencies>
security: oauth3: client: client-id: appid client-secret: secret scope: all resource: # 認(rèn)證中心的check_token 接口地址 token-info-uri: http://127.0.0.1:8764/oauth/check_token
@EnableResourceServer 即可完成接入
// 接入oauth3 ,聲明為資源服務(wù)器
@EnableResourceServer
@EnableDiscoveryClient
@SpringBootApplication
public class BigUpmsServerApplication {
public static void main(String[] args) {
SpringApplication.run(BigUpmsServerApplication.class, args);
}
}若不處理接口check_token 403
public class BigAuthServerConfiguration extends AuthorizationServerConfigurerAdapter {
/**
* checkTokenAccess 權(quán)限設(shè)置為isAuthenticated,不然資源服務(wù)器 來請(qǐng)求403
* @param oauthServer
*/
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
oauthServer
.allowFormAuthenticationForClients()
.checkTokenAccess("isAuthenticated()");
}
}@RestController
public class DemoController {
@GetMapping("/info")
public Authentication authentication(Authentication authentication) {
return authentication;
}
}獲取token

通過token 請(qǐng)求測(cè)試接口獲取當(dāng)前用戶信息

到此,相信大家對(duì)“怎么使用OAuth 2.0開發(fā)認(rèn)證中心和資源服務(wù)器接入”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
文章名稱:怎么使用OAuth2.0開發(fā)認(rèn)證中心和資源服務(wù)器接入
本文來源:http://chinadenli.net/article40/gospho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、品牌網(wǎng)站設(shè)計(jì)、面包屑導(dǎo)航、建站公司、網(wǎng)站排名、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)