Spring Security身份验证提供程序可以支持与其返回的身份验证不同的身份验证吗?

时间:2018-08-02 07:00:12

标签: java spring spring-security

我需要能够为用户端点和内部服务端点添加身份验证。两者都将通过验证承载标头令牌进行验证。

我正在考虑拥有2个自定义AuthenticationProvider,它们可能处理每种情况。

InternalAuthTokenAuthenticationProvider-用于内部服务端点

public class InternalAuthTokenAuthenticationProvider implements AuthenticationProvider {
  public Authentication authenticate(Authentication authentication) throws AuthenticationException {
  //process to authentication service
  if (authenticationSuccess) {
     return new InternalAuthTokenAuthentication();//this contains internal service principal details
  } else return null;
}
  @Override
    public boolean supports(Class<?> authentication) {
        return (authentication.equals(AuthTokenAuthentication.class));
    }

}

UserAuthTokenAuthenticationProvider-用于用户端点

public class UserAuthTokenAuthenticationProvider implements AuthenticationProvider {

    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
      //process to authentication service
      if (authenticationSuccess) {
         return new UserAuthTokenAuthentication();//this contains user specific principal details
      } else return null;
    }
      @Override
        public boolean supports(Class<?> authentication) {
            return (authentication.equals(AuthTokenAuthentication.class));
        }
 }

我的问题是,我可以使用公共身份验证对象作为两个提供程序的输入,但是在authenticate方法上返回不同的Authentication实现吗?这意味着它们将在“支持”方法上具有相同的实现。还是滥用API?

想法是,一旦进行身份验证,主体详细信息将有所不同。

0 个答案:

没有答案