溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用OAuth 2.0開發(fā)認證中心和資源服務器接入

發(fā)布時間:2021-11-17 16:08:05 來源:億速云 閱讀:197 作者:iii 欄目:大數據

本篇內容主要講解“怎么使用OAuth 2.0開發(fā)認證中心和資源服務器接入”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么使用OAuth 2.0開發(fā)認證中心和資源服務器接入”吧!

  • 基于 Spring Cloud OAuth ,用簡潔的方式搭建oauth的認證中心,

  • 關于oauth3 的授權模式 請直接參考 阮一峰 OAuth 2.0 的四種方式的詳細介紹

  • 項目版本核心說明

名稱版本
Spring Boot2.2.0.M5
Spring CloudHoxton.M2
Spring Cloud OAuth32.2.0.M2

開始配置認證服務器

maven 依賴引入

  • 這里只需要引入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安全,攔截全部的請求

  • 獲取web 上下文AuthenticationManager 注入到spring中,方便后邊oauth server注入

  • 創(chuàng)建UserDetailsService的內存實現,注入一個測試用戶

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
	/**
	 * 必須注入 AuthenticationManager,不然oauth  無法處理四種授權方式
	 *
	 * @return
	 * @throws Exception
	 */
	@Bean
	@Override
	public AuthenticationManager authenticationManagerBean() throws Exception {
		return super.authenticationManagerBean();
	}

	/**
	 * 必須注入UserDetailsService ,不然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;
	}
}

配置oauth3 認證服務器

  • 配置clientId 信息,及其支持的授權模式,特別注意這里是五種包含一個刷新操作

@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);
	}

}

以上完成了認證服務器的功能

測試密碼模式

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'grant_type=password&amp;username=lengleng&amp;password=lengleng&amp;scope=all' "http://appid:secret@localhost:8764/oauth/token"

開始配置資源服務器

maven 依賴引入

  • 這里只需要引入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: # 認證中心的check_token 接口地址
      token-info-uri: http://127.0.0.1:8764/oauth/check_token

應用聲明資源服務器

  • @EnableResourceServer 即可完成接入

// 接入oauth3 ,聲明為資源服務器
@EnableResourceServer  
@EnableDiscoveryClient
@SpringBootApplication
public class BigUpmsServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(BigUpmsServerApplication.class, args);
	}

}

上文配置的認證服務器暴露check_token

  • 若不處理接口check_token 403

public class BigAuthServerConfiguration extends AuthorizationServerConfigurerAdapter {
	/**
	 * checkTokenAccess 權限設置為isAuthenticated,不然資源服務器 來請求403
	 * @param oauthServer
	 */
	@Override
	public void configure(AuthorizationServerSecurityConfigurer oauthServer) {
		oauthServer
				.allowFormAuthenticationForClients()
				.checkTokenAccess("isAuthenticated()");
	}
}

資源服務器demo 接口

@RestController
public class DemoController {

	@GetMapping("/info")
	public Authentication authentication(Authentication authentication) {
		return authentication;
	}
}

通過上文獲取的token 訪問測試接口

  • 獲取token

怎么使用OAuth 2.0開發(fā)認證中心和資源服務器接入

  • 通過token 請求測試接口獲取當前用戶信息

怎么使用OAuth 2.0開發(fā)認證中心和資源服務器接入


到此,相信大家對“怎么使用OAuth 2.0開發(fā)認證中心和資源服務器接入”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI