• 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

  • 相关阅读:
    实验十四 线程设计
    实验十三 窗口设计
    实验十二 swing图形界面设计
    实验十一 图形界面二
    实验十 图形用户界面设计
    实验九 异常的抛出,捕获并处理
    实验八 接口与实现接口的类
    实验六 类的封装
    实验五:任意输入10个int类型数据,排序输出,再找出素数
    实验四:采用一维数组输出等腰三角形的杨辉三角
  • 原文地址:https://www.cnblogs.com/lori/p/15798293.html
Copyright © 2020-2023  润新知