keycloak~自定义directgrant直接认证

direct grant我们把它理解为通过rest接口直接认证,这是oauth2里的密码认证方式,即grant_type=password,它不需要走授权码这种复杂的流程,相当于传统的表单认证;keycloak事实上为我们准备了一个direct grant,只不过它只能使用username和password进行认证,如果你希望使用email,phoneNumber来进行密码认证,则需要另外去开发,下面就是开发的步骤:

  • 添加provider和providerFactory
    你的SelfDirectGrantAuthenticator需要继承ValidatePassword ,当然继承AbstractDirectGrantAuthenticator也是可以的,ValidatePassword是AbstractDirectGrantAuthenticator的一个子类,都是在直接授权时使用的,它的作用是校验用户的密码,KC的这种设计有利于程序的解耦,例如除了ValidatePassword还有ValidateUsername等。
/**
 * 直接继承了验证密码功能的direct grant流.
 */
public class TestDirectGrantAuthenticator extends ValidatePassword {
  KeycloakSession session;

  public V6DirectGrantAuthenticator(KeycloakSession session) {
    this.session = session;
  }

  @Override
  public void authenticate(AuthenticationFlowContext context) {
    String username = Optional.ofNullable(context.getHttpRequest().getDecodedFormParameters().getFirst("username"))
        .orElse(context.getHttpRequest().getDecodedFormParameters().getFirst("username"));
    String password = Optional.ofNullable(context.getHttpRequest().getDecodedFormParameters().getFirst("password"))
        .orElse(context.getHttpRequest().getDecodedFormParameters().getFirst("password"));
    MultivaluedMap<String, String> inputData = new MultivaluedMapImpl<>();
    inputData.add(KeycloakUtil.FIELD_EMAIL_PHONE, username);
    inputData.add(KeycloakUtil.PASSWORD, password);
    if (KeycloakUtil.passwordLogin(this, context, session.getProvider(JpaConnectionProvider.class).getEntityManager(), session, inputData)) {
      super.authenticate(context); //验证密码
    }
  }
  • 注册到org.keycloak.authentication.AuthenticatorFactory

  • keycloak管理平台,添加验证,可以从默认的direct grant上覆制

  • 将直接认证流程改成刚刚建立的

  • 现在就可以在postman里,脱离浏览器,进行认证了,并直接返回access_token

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章