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
最近整理了一系列的Java面试题,包含了Java基础、进阶、多线程、并发编程、数据库(MySQL、Redis、MongoDB、Memcachedd等等)、Spring全家桶、MyBatis、ZooKeeper、Dubbo、Elasticsearch、RabbitMQ、Kafka、Linux、Netty、Tomcat以及阿里、腾讯、字节、百度等大厂的面试真题,另外可以微信搜索539413949获取我为大家准备的资料