一、介绍authorizedgranttypes
在OAuth2中,授权模式可以说是至关重要的。在Java Spring Security中,我们可以使用authorizedgranttypes来指定此授权服务器支持的授权模式。授权模式定义了客户端应用程序与授权服务器之间进行交互的方式,从而获取令牌来访问受保护的资源。Spring Security支持多种授权模式,可以通过配置来启用或禁用它们。
二、各种授权模式
1、授权码模式
授权码模式是OAuth2中最常用的授权模式,它的安全性非常高。在授权码模式中,流程如下:
第一步:客户端发起授权请求 GET /authorize?response_type=code&client_id=clientapp&redirect_uri=http://localhost:9001/callback&scope=read_userinfo HTTP/1.1 Host: localhost:9000 Authorization: Basic Y2xpZW50YXBwOjEyMzQ1Ng== Accept: */* 第二步:用户同意授权 第三步:授权服务器返回授权码 GET /callback?code=授权码&state=xyz HTTP/1.1 Host: localhost:9001 Accept: */*
在Spring Security中可以这么配置:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  @Autowired
  private AuthenticationManager authenticationManager;
  @Bean
  public TokenStore tokenStore() {
      return new InMemoryTokenStore();
  }
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory()
              .withClient("clientapp")
              .secret("123456")
              .redirectUris("http://localhost:9001/callback")
              .authorizedGrantTypes("authorization_code")
              .scopes("read_userinfo")
              .autoApprove(true);
  }
  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
      endpoints.authenticationManager(authenticationManager)
              .tokenStore(tokenStore());
  }
}
2、刷新令牌模式
刷新令牌模式是当令牌过期时,可以通过刷新令牌来获取新的令牌。在Spring Security中,我们可以使用authorizedgranttypes来指定此授权服务器支持的授权模式。使用刷新令牌模式的客户端应用程序需要提供旧的令牌和刷新令牌。
在Spring Security中可以这么配置:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  @Autowired
  private AuthenticationManager authenticationManager;
  @Bean
  public TokenStore tokenStore() {
      return new InMemoryTokenStore();
  }
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory()
              .withClient("clientapp")
              .secret("123456")
              .redirectUris("http://localhost:9001/callback")
              .authorizedGrantTypes("refresh_token")
              .scopes("read_userinfo")
              .autoApprove(true);
  }
  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
      endpoints.authenticationManager(authenticationManager)
              .tokenStore(tokenStore());
  }
}
3、密码模式
密码模式适用于其它授权模式都无法满足需求的情况,比如第三方应用程序无法使用授权码模式或使用Refresh Token模式。在这种情况下,客户端应用程序会将用户名和密码发送到授权服务器,然后授权服务器会返回令牌。
在Spring Security中可以这么配置:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  @Autowired
  private AuthenticationManager authenticationManager;
  @Bean
  public TokenStore tokenStore() {
      return new InMemoryTokenStore();
  }
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory()
              .withClient("clientapp")
              .secret("123456")
              .authorizedGrantTypes("password")
              .scopes("write", "read")
              .autoApprove(true);
  }
  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
      endpoints.authenticationManager(authenticationManager)
              .tokenStore(tokenStore());
  }
}
4、客户端模式
客户端模式是在第三方应用程序访问自己的资源时使用的,它不需要用户的参与。在这种模式下,我们可以使用Spring Security来配置客户端,如下:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
  @Bean
  public TokenStore tokenStore() {
      return new InMemoryTokenStore();
  }
  @Override
  public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
      clients.inMemory()
              .withClient("clientapp")
              .secret("123456")
              .authorizedGrantTypes("client_credentials")
              .scopes("resource-server-read", "resource-server-write")
              .autoApprove(true);
  }
  @Override
  public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
      endpoints.tokenStore(tokenStore());
  }
}
三、结语
在本文中我们介绍了Spring Security中所有支持的授权模式,同时也给出了相关的代码展示。对于一个全能编程开发工程师来说,掌握这些授权模式,在开发过程中能够更灵活地运用其授权方式,实现更加高效、流畅的开发。
原创文章,作者:NEYZH,如若转载,请注明出处:https://www.506064.com/n/368910.html
 
 微信扫一扫
微信扫一扫  支付宝扫一扫
支付宝扫一扫 