SpringBootSecurity中OAuth2.0如何进行应用登记

这期内容当中小编将会给大家带来有关SpringBootSecurity中OAuth2.0如何进行应用登记,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

应用登记

一个应用要求 OAuth 授权,必须先到对方网站登记,让对方知道是谁在请求。举个例子,下面是github的登记页面:

  • https://github.com/settings/applications/new

SpringBootSecurity中OAuth2.0如何进行应用登记

下面我们来自己做一个简单的应用登记,根据表 oauth_client_details 的结构,我们登记的时候只填写应用名称和回调地址即可,其它的字段如下:

  • client_id:使用UUID生成

  • client_secret:使用UUID生成,并使用 BCryptPasswordEncoder 加密

  • scope:默认all

  • authorized_grant_types :默认 authorization_code,password,refresh_token 三个

下面是sql语句:

SpringBootSecurity中OAuth2.0如何进行应用登记

SpringBootSecurity中OAuth2.0如何进行应用登记

Service中的方法:

SpringBootSecurity中OAuth2.0如何进行应用登记

接口定义:

SpringBootSecurity中OAuth2.0如何进行应用登记

下面来测试接口:

SpringBootSecurity中OAuth2.0如何进行应用登记

返回了预期中的客户端id和秘钥。来看一下数据库:

SpringBootSecurity中OAuth2.0如何进行应用登记

现在我们就可以使用新登记的应用来请求令牌了:

  • http://localhost:8029/oauth/authorize?client_id=52f301a86511406ba5b4fbb4809614b0&response_type=code&redirect_uri=http://localhost:8029/

SpringBootSecurity中OAuth2.0如何进行应用登记

令牌请求结果:

SpringBootSecurity中OAuth2.0如何进行应用登记

状态state字段

为了防止CSRF攻击,在申请授权码这一步时,可以在参数中增加一个state状态参数,这个参数是由客户端生成的随机字符串,授权服务会原封不动的返回这个参数和参数值,用户进行授权客户端的请求时也会携带此字符串用于比较。如下:

  • http://localhost:8029/oauth/authorize?client_id=52f301a86511406ba5b4fbb4809614b0&response_type=code&redirect_uri=http://localhost:8029/&state=123456789

返回结果如下:

SpringBootSecurity中OAuth2.0如何进行应用登记

如果传递过去的和回来的不一样,可以认为不合法。

上述就是小编为大家分享的SpringBootSecurity中OAuth2.0如何进行应用登记了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。