OAuth2简易实战(一)-四种模式

引自:OAuth2简易实战(一)-四种模式html

1. OAuth2简易实战(一)-四种模式

1.1. 受权码受权模式(Authorization code Grant)

1.1.1. 流程图

1.1.2. 受权服务器配置

  1. 配置受权服务器中 client,secret,redirectUri,受权模式,权限配置
//受权服务器配置 @Configuration @EnableAuthorizationServer public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter { @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .redirectUris("http://localhost:9001/callback") // 受权码模式 .authorizedGrantTypes("authorization_code") .scopes("read_userinfo", "read_contacts"); } }

1.1.3. 资源服务器配置

  1. 配置须要资源受权的接口地址
//资源服务配置 @Configuration @EnableResourceServer public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest() .authenticated() .and() .requestMatchers() .antMatchers("/api/**"); } } 

1.1.4. 操做步骤

  1. 浏览器请求下列地址,获取受权code,请求参数client_id,redirect_uri回调地址,response_type响应类型,scope权限
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo
  1. 输入用户名密码,该密码为Spring Security的登路密码,application.properties里配置

# Spring Security Setting security.user.name=bobo security.user.password=xyz
  1. 登录后显示

  1. 选择Approve,点击Authorize,会调用回调地址并返回code参数

  1. 在得到受权码后,接下去获取访问令牌,访问
http://localhost:8080/oauth/token?code=ghN0hF&grant_type=authorization_code&redirect_uri=http://localhost:9001/callback&scope=read_userinfo

注意:须要在headers里添加认证

认证参数就是受权服务器配置的client和secretgit

  1. 获取token后访问
http://localhost:8080/api/userinfo?access_token=f4345f3a-34a3-4887-bc02-e95150c54bf4

若是token错误,则
github

1.1.5. 使用场景

  1. 受权码模式是最多见的一种受权模式,在oauth2.0内是最安全和最完善的。
  2. 适用于全部有Server端的应用,如Web站点、有Server端的手机客户端。
  3. 能够获得较长期限受权。

1.2. 隐式受权模式(Implicit Grant)

1.2.1. 流程图

1.2.2. 改动 authorizedGrantTypes

@Configuration @EnableAuthorizationServer public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{ @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .accessTokenValiditySeconds(60) .redirectUris("http://localhost:9001/callback") .authorizedGrantTypes("implicit") .scopes("admin", "visitor"); } }

1.2.3. 操做步骤

  1. 申请受权token,参数和申请受权码相似,client_id,redirect_uri回调地址,response_type有变更,改成直接获取token,scope权限,state用于认证标记,传过去什么回调时传回来什么
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=token&scope=admin&state=abc

  1. 操做同上,输入密码跳转认证确认,选Approve后点Authorize,跳转

  1. 能够看到直接返回了access_token,state也是原样返回
  2. 以后按受权码模式第六步操做,把access_token参数带上,进行接口调用就能够了

1.2.4. 使用场景

  1. 适用于全部无Server端配合的应用
  2. 如手机/桌面客户端程序、浏览器插件。
  3. 基于JavaScript等脚本客户端脚本语言实现的应用。

注意:由于Access token是附着在 redirect_uri 上面被返回的,因此这个 Access token就可能会暴露给资源全部者或者设置内的其它方(对资源全部者来讲,能够看到redirect_uri,对其它方来讲,能够经过监测浏览器的地址变化来获得 Access token)。spring

1.3. 密码模式(Resource Owner Password Credentials Grant)

1.3.1. 流程图

1.3.2. 改动

  1. 受权服务器配置,须要添加用户认证管理端点authenticationManager,修改模式authorizedGrantTypes为password
// 受权服务器配置 @Configuration @EnableAuthorizationServer public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{ @Autowired private AuthenticationManager authenticationManager; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .accessTokenValiditySeconds(60) .redirectUris("http://localhost:9001/callback") .authorizedGrantTypes("password") .scopes("admin", "visitor"); } }

1.3.3. 操做步骤

  1. 调用如下连接,向客户端和服务器提供用户名密码
http://localhost:8080/oauth/token?password=123456&grant_type=password&username=lll&scope=admin

注意:和受权码模式同样,须要在headers里添加认证docker

  1. 结果:
    api

  2. 获取token后,步骤同1.1和1.2模式浏览器

1.3.4. 使用场景

  1. 这种模式适用于用户对应用程序高度信任的状况。好比是用户操做系统的一部分。
  2. 认证服务器只有在其余受权模式没法执行的状况下,才能考虑使用这种模式。

1.4. 客户端凭证模式(Client Credentials Grant)

1.4.1. 流程图

1.4.2. 改动

  1. 只需修改受权服务器,authorizedGrantTypes类型client_credentials

1.4.3. 操做步骤

http://localhost:8080/oauth/token?grant_type=client_credentials&scope=admin

  1. 能够看到客户端凭证模式也须要在header里添加认证帐户密码
  2. 得到token后操做同上

1.4.4. 使用场景

  1. 客户端模式应用于应用程序想要以本身的名义与受权服务器以及资源服务器进行互动。
  2. 例如使用了第三方的静态文件服务

1.5. 刷新TOKEN

1.5.1. 流程图

1.5.2. 改动

1.5.3. 操做步骤

  1. 以受权码模式为例,步骤同受权码模式,取得受权码后,去取token时,返回

  1. 在token过时后,调用
http://localhost:8080/oauth/token?grant_type=refresh_token&refresh_token=ad3941d1-c6dd-4a2e-a9c8-eac6a9a59dd2

  1. 返回
  2. 就能够拿新的access_token继续调用了
  3. 建议将access_token和refresh_token的过时时间保存下来,每次调用平台方的业务api前先对access_token和refresh_token进行一下时间判断,若是过时则执行刷新access_token或从新受权操做。refersh_token若是过时就只能让用户从新受权。

参考 https://www.cnblogs.com/maoxiaolv/p/5838680.html安全

代码学习地址 https://github.com/spring2go/oauth2labruby

相关文章
相关标签/搜索