OAuth 2.0 是一个关于授权的开放的网络协议,是目前最流行的授权机制。
数据的所有者告诉系统,同意授权第三方应用进入系统,获取这些数据。系统从而产生一个短期的进入令牌(token),用来代替密码,供第三方应用使用。
由于授权的场景众多,OAuth 2.0 协议定义了获取令牌的四种授权方式,分别是:
四种授权模式分别使用不同的
grant_type
来区分
虽然 OAuth2 协议定义了4种标准的授权模式,但是在实际开发过程中还是远远满足不了各种变态的业务场景,需要我们去扩展。
例如增加图形验证码、手机验证码、手机号密码登录等等的场景
而常见的做法都是通过增加 过滤器Filter
的方式来扩展 Spring Security
授权,但是这样的实现方式有两个问题:
OAuth2
的管理Filter
的方式去实现就比较麻烦了。所以目前在 Spring Security
中比较优雅和灵活的扩展方式就是通过自定义 grant_type 来增加授权模式。
在扩展之前首先需要先了解 Spring Security
的整个授权流程,我以 密码模式 为例去展开分析,如下图所示
整个授权流程关键点分为以下两个部分:
第一部分:关于授权类型 grant_type
的解析
grant_type
都会有一个对应的 TokenGranter
实现类。TokenGranter
实现类都通过 CompositeTokenGranter
中的 tokenGranters
集合存起来。grantType
参数来定位具体使用那个 TokenGranter
实现类来处理授权。第二部分:关于授权登录逻辑
授权方式
都会有一个对应的 AuthenticationProvider
实现类来实现。AuthenticationProvider
实现类都通过 ProviderManager
中的 providers
集合存起来。TokenGranter
类会 new 一个 AuthenticationToken
实现类,如 UsernamePasswordAuthenticationToken
传给 ProviderManager
类。ProviderManager
则通过 AuthenticationToken
来判断具体使用那个 AuthenticationProvider
实现类来处理授权。AuthenticationProvider
实现类来实现,如 DaoAuthenticationProvider
。根据上面的流程,扩展分为以下两种场景
场景一:只对原有的授权逻辑进行增强或者扩展,如:用户名密码登录前增加图形验证码校验。
该场景需要定义一个新的 grantType
类型,并新增对应的 TokenGranter
实现类 添加扩展内容,然后加到 CompositeTokenGranter
中的 tokenGranters
集合里即可。
场景二:新加一种授权方式,如:手机号加密码登录。
该场景需要实现以下内容:
grantType
类型,并新增对应的 TokenGranter
实现类添加到 CompositeTokenGranter
中的 tokenGranters
集合里AuthenticationToken
实现类,用于存放该授权所需的信息。AuthenticationProvider
实现类 实现授权的逻辑,并重写 supports
方法绑定步骤二的 AuthenticationToken
实现类下面以 场景二 新增手机号加密码授权方式为例,展示核心的代码实现
创建 MobileAuthenticationToken
类,用于存储手机号和密码信息
public class MobileAuthenticationToken extends AbstractAuthenticationToken {
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
private final Object principal;
private Object credentials;
public MobileAuthenticationToken(String mobile, String password) {
super(null);
this.principal = mobile;
this.credentials = password;
setAuthenticated(false);
}
public MobileAuthenticationToken(Object principal, Object credentials,
Collection<? extends GrantedAuthority> authorities) {
super(authorities);
this.principal = principal;
this.credentials = credentials;
super.setAuthenticated(true);
}
@Override
public Object getCredentials() {
return this.credentials;
}
@Override
public Object getPrincipal() {
return this.principal;
}
@Override
public void setAuthenticated(boolean isAuthenticated) {
if (isAuthenticated) {
throw new IllegalArgumentException(
"Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
}
super.setAuthenticated(false);
}
@Override
public void eraseCredentials() {
super.eraseCredentials();
}
}
创建 MobileAuthenticationProvider
类,实现登录逻辑,并绑定 MobileAuthenticationToken
类
@Setter
public class MobileAuthenticationProvider implements AuthenticationProvider {
private ZltUserDetailsService userDetailsService;
private PasswordEncoder passwordEncoder;
@Override
public Authentication authenticate(Authentication authentication) {
MobileAuthenticationToken authenticationToken = (MobileAuthenticationToken) authentication;
String mobile = (String) authenticationToken.getPrincipal();
String password = (String) authenticationToken.getCredentials();
UserDetails user = userDetailsService.loadUserByMobile(mobile);
if (user == null) {
throw new InternalAuthenticationServiceException("手机号或密码错误");
}
if (!passwordEncoder.matches(password, user.getPassword())) {
throw new BadCredentialsException("手机号或密码错误");
}
MobileAuthenticationToken authenticationResult = new MobileAuthenticationToken(user, password, user.getAuthorities());
authenticationResult.setDetails(authenticationToken.getDetails());
return authenticationResult;
}
@Override
public boolean supports(Class<?> authentication) {
return MobileAuthenticationToken.class.isAssignableFrom(authentication);
}
}
创建 MobilePwdGranter
类并定义 grant_type
的值为 mobile_password
public class MobilePwdGranter extends AbstractTokenGranter {
private static final String GRANT_TYPE = "mobile_password";
private final AuthenticationManager authenticationManager;
public MobilePwdGranter(AuthenticationManager authenticationManager, AuthorizationServerTokenServices tokenServices
, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory) {
super(tokenServices, clientDetailsService, requestFactory, GRANT_TYPE);
this.authenticationManager = authenticationManager;
}
@Override
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
Map<String, String> parameters = new LinkedHashMap<>(tokenRequest.getRequestParameters());
String mobile = parameters.get("mobile");
String password = parameters.get("password");
parameters.remove("password");
Authentication userAuth = new MobileAuthenticationToken(mobile, password);
((AbstractAuthenticationToken) userAuth).setDetails(parameters);
userAuth = authenticationManager.authenticate(userAuth);
if (userAuth == null || !userAuth.isAuthenticated()) {
throw new InvalidGrantException("Could not authenticate mobile: " + mobile);
}
OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
return new OAuth2Authentication(storedOAuth2Request, userAuth);
}
}
// 添加手机号加密码授权模式
tokenGranters.add(new MobilePwdGranter(authenticationManager, tokenServices, clientDetailsService, requestFactory));
使用以下地址,指定 grant_type
为 mobile_password
进行授权获取 access_token
/oauth/token?grant_type=mobile_password&mobile={mobile}&password={password}
详细的代码实现可以参考
https://gitee.com/zlt2000/microservices-platform/tree/master/zlt-uaa
扫码关注有惊喜!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。