温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么使用OAuth 2.0开发认证中心和资源服务器接入

发布时间:2021-11-17 16:08:05 来源:亿速云 阅读:194 作者:iii 栏目:大数据

本篇内容主要讲解“怎么使用OAuth 2.0开发认证中心和资源服务器接入”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么使用OAuth 2.0开发认证中心和资源服务器接入”吧!

  • 基于 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注入

  • 创建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  密码模式等死循环问题
	 *
	 * @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开发认证中心和资源服务器接入

  • 通过token 请求测试接口获取当前用户信息

怎么使用OAuth 2.0开发认证中心和资源服务器接入


到此,相信大家对“怎么使用OAuth 2.0开发认证中心和资源服务器接入”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI