keycloak~自定義directgrant直接認證
direct grant我們把它理解為通過rest接口直接認證,這是oauth2裏的密碼認證方式,即 grant_type=password
,它不需要走授權碼這種複雜的流程,相當於傳統的表單認證;keycloak事實上為我們準備了一個direct grant,只不過它只能使用username和password進行認證,如果你希望使用email,phoneNumber來進行密碼認證,則需要另外去開發,下面就是開發的步驟:
-
添加provider和providerFactory
你的SelfDirectGrantAuthenticator需要繼承AbstractUsernameFormAuthenticator,當然繼承Authenticator也是可以的,因為我們並不需要進行表單交互。
public class V6DirectGrantAuthenticator extends AbstractUsernameFormAuthenticator { 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")); if (用户名密碼==正確) { context.success(); } } @Override public void action(AuthenticationFlowContext context) { authenticate(context); } }
-
註冊到org.keycloak.authentication.AuthenticatorFactory
-
keycloak管理平台,添加驗證,可以從默認的direct grant上覆制
-
將直接認證流程改成剛剛建立的
-
現在就可以在postman裏,脱離瀏覽器,進行認證了,並直接返回access_token
「其他文章」
- java~Optional語法糖
- Java~公用包中如何加載資源文件
- [email protected]一切為了可擴展性
- rsa~對接第三方rsa問題排查
- keycloak~自定義directgrant直接認證
- java~RMI引起的log4j漏洞
- k8s~Endpoints的使用之負載均衡
- keycloak~uma遠程資源授權對接asp.net core
- es~存儲部分字段
- maven編譯後複製到目標位置
- keycloak~緩存的使用
- keycloak~授權功能的使用
- java~並行計算~大集合的並行處理
- keycloak~使用JDBC_PING實現k8s裏的高可用
- keycloak~為認證提供者添加配置項
- skywalking的介紹
- springboot~disruptor異步隊列
- keycloak~自定義rest接口
- springcloud~feign POST form-url-encoded data
- js~ajax獲取後端HTTP狀態的幾種情況