您好,欢迎访问这里是深圳市硕远科技有限公司!
戴尔服务器价格_IBM联想配置_浪潮代理-深圳市硕远科技有限公司
联系我们
戴尔服务器价格_IBM联想配置_浪潮代理-深圳市硕远科技有限公司
邮箱:2324898850@qq.com
电话:400-080-6079
地址:深圳市龙华区河背工业区108创业园A301
当前位置:主页 > 新闻动态 > 行业新闻 >

行业新闻

如何快速实现一个代理授权(delegated authorizatio

发布时间:2021-08-07 06:00:46浏览次数:
如何快速实现一个代理授权(delegated authorization)服务器

OAuth存在的意义就是在不共享密码的情况下授权让第三方代表资源拥有者访问受保护的数据,授权可以随时撤销。第三方是通过token去访问受保护数据的。同时通过scope控制访问权限。Spring Security框架对OAuth有很好的支持。可以利用该框架快速搭建一个OAuth认证服务器。

创建工程

创建一个最简单的spring boot工程,添加web依赖

如何快速实现一个代理授权(delegated authorization)服务器

编程pom文件,添加spring Security的OAuth2支持包,完整的pom文件如下:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.beaver</groupId> <artifactId>oauth2-authserver-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>oauth2-authserver-demo</name> <description>Demo project for spring oauth2: authorization server</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.security.oauth</groupId> <artifactId>spring-security-oauth2</artifactId> <version>2.3.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

配置OAuth认证服务器

配置OAuth认证服务器只需要:

  1. 添加@EnableAuthorizationServer注释
  2. 扩展类AuthorizationServerConfigurerAdapter,并重载这个类的几个方法。

完整代码如下:

package com.beaver.oauth2authserverdemo.auth;import org.springframework.context.annotation.Configuration;import org.springframework.security.crypto.password.NoOpPasswordEncoder;import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;@EnableAuthorizationServer@Configurationpublic class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Override public void configure(AuthorizationServerSecurityConfigurer security) throws Exception { security.passwordEncoder(NoOpPasswordEncoder.getInstance()) .checkTokenAccess("permitAll()") .tokenKeyAccess("permitAll()"); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("guest_app") .scopes("READ","WRITE") .secret("secret") .autoApprove(true) .authorities("ROLE_GUEST_AUTHORIZATION_CLIENT") .authorizedGrantTypes("client_credentials") .and() .withClient("api_audit") .scopes("READ") .secret("secret") .autoApprove(true) .authorities("ROLE_GUEST_AUTHORIZATION_CLIENT") .authorizedGrantTypes("client_credentials"); } @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.tokenStore(new InMemoryTokenStore()); }}
如何快速实现一个代理授权(delegated authorization)服务器

上面的代码就完成了一个简单的OAuth2的认证服务器的全部配置。

在内存中配置了两个OAuth2账号guest_app和api_audit,密码都是secret,两个账号具有不同的scope,grant type是client_credential

配置资源服务器

配置OAuth2的资源服务器很简单,只需要在spring boot的主程序中添加注释@EnableResourceServer就行了,当然资源服务器和认证服务器可以分开,这里是放在一起的。

package com.beaver.oauth2authserverdemo;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;@SpringBootApplication@EnableResourceServerpublic class Oauth2AuthserverDemoApplication { public static void main(String[] args) { SpringApplication.run(Oauth2AuthserverDemoApplication.class, args); }}
如何快速实现一个代理授权(delegated authorization)服务器

测试服务器

一,获取access token

这里由于没有做客户端,我们使用postman测试

启动服务器(spring boot web中默认端口为8080)

使用post方法,url 为

http://localhost:8080/oauth/token

添加授权方式为Basic Auth:

		用户名:guest_app		密码:secret
如何快速实现一个代理授权(delegated authorization)服务器

添加参数:

key:grant_typevalue: client_credentials
如何快速实现一个代理授权(delegated authorization)服务器

发起请求

如何快速实现一个代理授权(delegated authorization)服务器

可以看到服务器返回的access token值,token类型,过期时间和scope值

二,查看token

用第一步获取的token来做认证,来检查token

url为
http://localhost:8080/oauth/check_token,只需要添加参数

token,其值为上一步获取的access token的值

如何快速实现一个代理授权(delegated authorization)服务器

发起请求后,可以看到该token的详细信息。

这里没有做oauth客户端,也没有演示,授权注册的全过程,以及撤销授权等。可参考相关文档自行探索

400-080-6079