服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - 详解使用Spring Security OAuth 实现OAuth 2.0 授权

详解使用Spring Security OAuth 实现OAuth 2.0 授权

2021-03-19 10:47liuyatao JAVA教程

本篇文章主要介绍了详解使用Spring Security OAuth 实现OAuth 2.0 授权,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

oauth 2.0 是一种工业级的授权协议。oauth 2.0是从创建于2006年的oauth 1.0继承而来的。oauth 2.0致力于帮助开发者简化授权并为web应用、桌面应用、移动应用、嵌入式应用提供具体的授权流程。

oauth 2.0 is the industry-standard protocol for authorization. oauth 2.0 supersedes the work done on the original oauth protocol created in 2006. oauth 2.0 focuses on client developer simplicity while providing specific authorization flows for web applications, desktop applications, mobile phones, and living room devices.

oauth 2.0的四个角色

为了方便理解,以常用的 使用微信登录 为例

resource owner

资源拥有者,对应微信的每个用户微信上设置的个人信息是属于每个用户的,不属于腾讯。

resource server

资源服务器,一般就是用户数据的一些操作(增删改查)的rest api,比如微信的获取用户基本信息的接口。

client application

第三方客户端,对比微信中就是各种微信公众号开发的应用,第三方应用经过 认证服务器 授权后即可访问 资源服务器 的rest api来获取用户的头像、性别、地区等基本信息。

authorization server

认证服务器,验证第三方客户端是否合法。如果合法就给客户端颁布token,第三方通过token来调用资源服务器的api。

四种授权方式(grant type)

anthorization_code

授权码类型,适用于web server application。模式为:客户端先调用 /oauth/authorize/ 进到用户授权界面,用户授权后返回 code ,客户端然后根据code和 appsecret 获取 access token 。

implicit简化类型,相对于授权码类型少了授权码获取的步骤。客户端应用授权后认证服务器会直接将access token放在客户端的url。客户端解析url获取token。这种方式其实是不太安全的,可以通过 https安全通道 和 缩短access token的有效时间 来较少风险。

password

密码类型,客户端应用通过用户的username和password获access token。适用于资源服务器、认证服务器与客户端具有完全的信任关系,因为要将用户要将用户的用户名密码直接发送给客户端应用,客户端应用通过用户发送过来的用户名密码获取token,然后访问资源服务器资源。比如支付宝就可以直接用淘宝用户名和密码登录,因为它们属于同一家公司,彼此 充分信任 。

client_credentials

客户端类型,是不需要用户参与的一种方式,用于不同服务之间的对接。比如自己开发的应用程序要调用短信验证码服务商的服务,调用地图服务商的服务、调用手机消息推送服务商的服务。当需要调用服务是可以直接使用服务商给的 appid 和 appsecret 来获取token,得到token之后就可以直接调用服务。

其他概念

  1. scope :访问资源服务器的哪些作用域。
  2. refresh token :当access token 过期后,可以通过refresh token重新获取access token。

实现

有的时候资源服务器和认证服务器是两个不同的应用,有的时候资源服务器和认证服务器在通一个应用中,不同之处在于资源服务器是否需要检查token的有效性,前者需要检查,后者不需要。这里实现后者。

application的安全配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@configuration
public class securityconfiguration extends websecurityconfigureradapter {
 
  @override
  protected void configure(httpsecurity http) throws exception {
    http.formlogin()
        .and().csrf().disable()
        .authorizerequests().anyrequest().authenticated();
  }
 
  @override
  public void configure(websecurity web) throws exception {
    super.configure(web);
  }
 
  @override
  protected void configure(authenticationmanagerbuilder auth) throws exception {
    auth.inmemoryauthentication().withuser("lyt").password("lyt").authorities("role_user")
        .and().withuser("admin").password("admin").authorities("role_admin");
  }
 
  @bean
  @override
  public authenticationmanager authenticationmanagerbean() throws exception {
    return super.authenticationmanagerbean();
  }
}

认证服务器配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
@enableauthorizationserver
@configuration
public class authorizationserverconfiguration extends authorizationserverconfigureradapter {
 
  @override
  public void configure(clientdetailsserviceconfigurer clients) throws exception {
    clients.inmemory().withclient("client")
        .scopes("read","write")
        .secret("secret")
        .authorizedgranttypes("authorization_code","password","implicit","client_credentials");}
 
  @override
  public void configure(authorizationserversecurityconfigurer security) throws exception {
    super.configure(security);
  }
 
  @override
  public void configure(authorizationserverendpointsconfigurer endpoints) throws exception {
    endpoints.authenticationmanager(authenticationmanager);
  }
 
  @autowired
  @qualifier("authenticationmanagerbean")
  private authenticationmanager authenticationmanager;
}

资源服务器配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@enableglobalmethodsecurity(prepostenabled = true)
@enableresourceserver
@configuration
public class resourceserverconfiguration extends resourceserverconfigureradapter {
 @override
 public void configure(httpsecurity http) throws exception {
 http.antmatcher("/oauth2/api/**").authorizerequests()
  .antmatchers(httpmethod.get, "/read/**").access("#oauth2.hasscope('read')")
  .antmatchers(httpmethod.post, "/write/**").access("#oauth2.hasscope('write')")
  .antmatchers(httpmethod.put, "/write/**").access("#oauth2.hasscope('write')")
  .antmatchers(httpmethod.delete, "/write/**").access("#oauth2.hasscope('write')");
 }
 
}

资源服务器 filter-order 设置

需要在 application.yml 中将filter-order设置成3,具体原因参考链接

防止cookie冲突

为了避免认证服务器的cookie和客户端的cookie冲突,出现错误,最好修改 cookie name 或者设置 contextpath 。

测试

postman 中提供oauth 2.0的认证方式,可以获取到token之后再把认证加入http请求中,即可请求资源服务器的rest api

客户端信息

详解使用Spring Security OAuth 实现OAuth 2.0 授权

授权

详解使用Spring Security OAuth 实现OAuth 2.0 授权

获取的token

详解使用Spring Security OAuth 实现OAuth 2.0 授权

访问资源服务器api

详解使用Spring Security OAuth 实现OAuth 2.0 授权

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://juejin.im/post/5a580e726fb9a01caf3757a1

延伸 · 阅读

精彩推荐