Spring cloud oauth2.0学习总结

因为项目需要,调研并使用了Spring cloud oauth2.0,网上关于oauth2.0的介绍很多,包括交互的流程等等。这里,我不再介绍这些,直接深入讲解,并附上实践代码。希望能对大家有帮助。

相关链接:

Spring cloud oauth2.0的源码解析和本人实现的三个Demo,第一个Demo是使用Spring cloud oauth2.0来实现单点登陆;第二个Demo是使用Jdbc来做token的存储,其中我又分别实现了一个是资源服务器自己去校验token,另一个是资源服务器转到认证服务器去进行校验token;第三个Demo是使用jwt的方式进行token授权和校验。以上每种方式我都实现了认证服务器和资源服务器的分离,以便读者可以更快速的使用到自己的生产环境中,如果代码对大家有帮助,还望github上给个星星呐http://blog.csdn.net/j754379117/article/details/70176974


一、oauth中的角色

client:调用资源服务器API的应用

Oauth 2.0 Provider:包括Authorization Server和Resource Server

(1)Authorization Server:认证服务器,进行认证和授权

(2)Resource Server:资源服务器,保护受保护的资源

user:资源的拥有者

 

二、下面详细介绍一下Oauth 2.0 Provider

Authorization Server:

(1)AuthorizationEndpoint:进行授权的服务,Default URL: /oauth/authorize

(2)TokenEndpoint:获取token的服务,Default URL: /oauth/token

Resource Server:

OAuth2AuthenticationProcessingFilter:给带有访问令牌的请求加载认证


三、下面再来详细介绍一下Authorization Server:

一般情况下,创建两个配置类,一个继承AuthorizationServerConfigurerAdapter,一个继承WebSecurityConfigurerAdapter,再去复写里面的方法。

主要出现的两种注解:

1、@EnableAuthorizationServer:声明一个认证服务器,当用此注解后,应用启动后将自动生成几个Endpoint:(注:其实实现一个认证服务器就是这么简单,加一个注解就搞定,当然真正用到生产环境还是要进行一些配置和复写工作的。)

/oauth/authorize:验证

/oauth/token:获取token

/oauth/confirm_access:用户授权

/oauth/error:认证失败

/oauth/check_token:资源服务器用来校验token

/oauth/token_key:如果jwt模式则可以用此来从认证服务器获取公钥

以上这些endpoint都在源码里的endpoint包里面。

2、@Beans:需要实现AuthorizationServerConfigurer

AuthorizationServerConfigurer包含三种配置:

ClientDetailsServiceConfigurer:client客户端的信息配置,client信息包括:clientId、secret、scope、authorizedGrantTypes、authorities

(1)scope:表示权限范围,可选项,用户授权页面时进行选择

(2)authorizedGrantTypes:有四种授权方式 

  • Authorization Code:用验证获取code,再用code去获取token(用的最多的方式,也是最安全的方式
  • Implicit: 隐式授权模式
  • Client Credentials (用來取得 App Access Token)
  • Resource Owner Password Credentials

(3)authorities:授予client的权限


这里的具体实现有多种,in-memory、JdbcClientDetailsService、jwt等。

AuthorizationServerSecurityConfigurer:声明安全约束,哪些允许访问,哪些不允许访问

AuthorizationServerEndpointsConfigurer:声明授权和token的端点以及token的服务的一些配置信息,比如采用什么存储方式、token的有效期等

 

client的信息的读取:在ClientDetailsServiceConfigurer类里面进行配置,可以有in-memory、jdbc等多种读取方式。

jdbc需要调用JdbcClientDetailsService类,此类需要传入相应的DataSource.

 

下面再介绍一下如何管理token:

AuthorizationServerTokenServices接口:声明必要的关于token的操作

(1)当token创建后,保存起来,以便之后的接受访问令牌的资源可以引用它。

(2)访问令牌用来加载认证

接口的实现也有多种,DefaultTokenServices是其默认实现,他使用了默认的InMemoryTokenStore,不会持久化token;


token存储方式共有三种分别是:

(1)InMemoryTokenStore:存放内存中,不会持久化

(2)JdbcTokenStore:存放数据库中

(3)Jwt: json web token,详见 http://blog.leapoahead.com/2015/09/06/understanding-jwt/


授权类型:

可以通过AuthorizationServerEndpointsConfigurer来进行配置,默认情况下,支持除了密码外的所有授权类型。相关授权类型的一些类:

(1)authenticationManager:直接注入一个AuthenticationManager,自动开启密码授权类型

(2)userDetailsService:如果注入UserDetailsService,那么将会启动刷新token授权类型,会判断用户是否还是存活的

(3)authorizationCodeServices:AuthorizationCodeServices的实例,auth code 授权类型的服务

(4)implicitGrantService:imlpicit grant

(5)tokenGranter:

 

endpoint的URL的配置:

(1)AuthorizationServerEndpointsConfigurer的pathMapping()方法,有两个参数,第一个是默认的URL路径,第二个是自定义的路径

(2)WebSecurityConfigurer的实例,可以配置哪些路径不需要保护,哪些需要保护。默认全都保护。


自定义UI:

(1)有时候,我们可能需要自定义的登录页面和认证页面。登陆页面的话,只需要创建一个login为前缀名的网页即可,在代码里,设置为允许访问,这样,系统会自动执行你的登陆页。此登陆页的action要注意一下,必须是跳转到认证的地址。

(2)另外一个是授权页,让你勾选选项的页面。此页面可以参考源码里的实现,自己生成一个controller的类,再创建一个对应的web页面即可实现自定义的功能。


下面梳理一下授权获取token流程:

(1)端口号换成你自己的认证服务器的端口号,client_id也换成你自己的,response_type类型为code。

 localhost:8080/uaa/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.baidu.com

 

(2)这时候你将获得一个code值:


(3)使用此code值来获取最终的token:

curl -X POST -H "Cant-Type: application/x-www-form-urlencoded" -d 'grant_type=authorization_code&code=G0C20Z&redirect_uri=http://www.baidu.com' "http://client:secret@localhost:8080/uaa/oauth/token"

返回值:

{"access_token":"b251b453-cc08-4520-9dd0-9aedf58e6ca3","token_type":"bearer","expires_in":2591324,"scope":"app"}


(4)用此token值来调用资源服务器内容(如果资源服务器和认证服务器在同一个应用中,那么资源服务器会自己解析token值,如果不在,那么你要自己去做处理)

curl -H "Authorization: Bearer b251b453-cc08-4520-9dd0-9aedf58e6ca3" "localhost:8081/service2(此处换上你自己的url)"


四、Resource Server:保护资源,需要令牌才能访问

在配置类上加上注解@EnableResourceServer即启动。使用ResourceServerConfigurer进行配置:

(1)tokenServices:ResourceServerTokenServices的实例,声明了token的服务

(2)resourceId:资源Id,由auth Server验证。

(3)其它一些扩展点,比如可以从请求中提取token的tokenExtractor

(4)一些自定义的资源保护配置,通过HttpSecurity来设置

 

使用token的方式也有两种:

(1)Bearer Token(https传输方式保证传输过程的安全):主流

(2)Mac(http+sign)


如何访问资源服务器中的API?

如果资源服务器和授权服务器在同一个应用程序中,并且您使用DefaultTokenServices,那么您不必太考虑这一点,因为它实现所有必要的接口,因此它是自动一致的。如果您的资源服务器是一个单独的应用程序,那么您必须确保您匹配授权服务器的功能,并提供知道如何正确解码令牌的ResourceServerTokenServices。与授权服务器一样,您可以经常使用DefaultTokenServices,并且选项大多通过TokenStore(后端存储或本地编码)表示。

(1)在校验request中的token时,使用RemoteTokenServices去调用AuthServer中的/auth/check_token。

(2)共享数据库,使用Jdbc存储和校验token,避免再去访问AuthServer。

(3)使用JWT签名的方式,资源服务器自己直接进行校验,不借助任何中间媒介。

 

五、oauth client

在客户端获取到token之后,想去调用下游服务API时,为了能将token进行传递,可以使用RestTemplate.然后使用restTemplate进行调用Api。

注:

scopes和authorities的区别:

scopes是client权限,至少授予一个scope的权限,否则报错。

authorities是用户权限。

 

六、各大开放平台的鉴权对比:

百度:http://developer.baidu.com/wiki/index.php?title=docs/oauth#.E6.8E.88.E6.9D.83.E5.9B.9E.E8.B0.83.E5.9C.B0.E5.9D.80

github: https://developer.github.com/v3/oauth/#scopes

 

七、参考资料:

官方文档:

1.OAuth 2 Developers Guide:http://projects.spring.io/spring-security-oauth/docs/oauth2.html

2.boot-features-security-oauth2-resource-server:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-security-oauth2-resource-server

3.spring_cloud_security:http://cloud.spring.io/spring-cloud-static/Camden.SR5/#_spring_cloud_security

4.jwt:https://jwt.io/

5.oauth wiki:https://en.wikipedia.org/wiki/OAuth#List_of_OAuth_service_providers

6.spring boot and oauth2: https://spring.io/guides/tutorials/spring-boot-oauth2/#_social_login_simple

7.spring boot security: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-security-oauth2-single-sign-on

8.oauth:https://www.oauth.com/oauth2-servers/definitions/

9.spring security:http://docs.spring.io/spring-security/site/docs/4.2.1.RELEASE/reference/htmlsingle/#jc-method

非官方文档:

《Data Modelling for oauth2》: https://www.slideshare.net/SpringCentral/syer-oauth-model

 

博客:

《Securing Spring Cloud Microservices With OAuth2》:http://stytex.de/blog/2016/02/01/spring-cloud-security-with-oauth2/

使用OAuth2的SSO分析》:http://blog.csdn.net/xiejx618/article/details/51039653

使用Spring Cloud Security OAuth2搭建授权服务》:http://blog.csdn.net/neosmith/article/details/52539927#reply

《各大網站 OAuth 2.0 實作差異》:https://blog.yorkxin.org/2013/09/30/oauth2-implementation-differences-among-famous-sites

《Using UAA for Microservice Security》:https://jhipster.github.io/using-uaa/#claims

问答社区:

1.Spring security scope VS authorities:http://stackoverflow.com/questions/32092749/spring-oauth2-scope-vs-authoritiesroles?answertab=votes#tab-top

2.Oauth的access token 安全么?:https://www.zhihu.com/question/20274730

3.Spring Security OAuth2 pure resource server


  • 11
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 25
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值