keycloak~自定义SPI的注入与扩展
项目结构
自定义SPI注册
1. 直接复制文件方式
docker cp keycloak-service-self-spi.jar keycloak:/opt/
docker cp module-one-add.cli keycloak:/opt/
docker exec -it keycloak bash
$JBOSS_HOME/bin/jboss-cli.sh --file=/opt/module-one-add.cli
使用docker commit保存你当前容器为一个新的镜像,然后启动它即可
2. 打包镜像方式
Dockerfile
FROM jboss/keycloak:11.0.3
USER root
#self provider
WORKDIR /opt
COPY target/keycloak-service-self-spi.jar .
RUN mkdir cli
COPY module-one-add.cli cli/
RUN $JBOSS_HOME/bin/jboss-cli.sh --file=cli/module-one-add.cli
RUN rm -rf $JBOSS_HOME/standalone/configuration/standalone_xml_history/current
打包
docker build -t self/keycloak .
后续的扩展
当有了自己的SPI接口之后,它也有了对应的ProviderFactory和Provider,然后你就可以在扩展它了,一般的步骤是:
- 建立新项目
- 依赖上面的SPI项目(它已经被注入到keycloak里,就像keycloak系统的spi)
- 将新的项目打包上传到
/opt/jboss/keycloak/standalone/deployments
目录完成自动化部署 - 在resources/META-INF/jboss-deployment-structure.xml里添加上面项目spi module
- 注册成功之后,在keycloak管理平台的server-info/providers里会有体现
module-one-add.cli
# main provider
module add --name=keycloak-service-self-spi --resources=/opt/keycloak-service-self-spi.jar --dependencies=org.keycloak.keycloak-core,org.keycloak.keycloak-common,org.hibernate,org.keycloak.keycloak-server-spi,org.keycloak.keycloak-server-spi-private,org.keycloak.keycloak-services,org.keycloak.keycloak-model-jpa,org.jboss.logging,javax.api,javax.ws.rs.api,javax.transaction.api,javax.persistence.api,org.jboss.resteasy.resteasy-jaxrs,org.apache.httpcomponents,org.apache.commons.lang,javax.xml.bind.api,com.squareup.okhttp3
embed-server --server-config=standalone-ha.xml --std-out=echo
batch
# main provider
/subsystem=keycloak-server:list-add(name=providers,value=module:keycloak-service-self-spi)
/subsystem=keycloak-server/spi=SelfProviderFactory/:add
/subsystem=keycloak-server/spi=SelfProvider/provider=defaultselfprovider/:add(enabled=true)
run-batch
stop-embedded-server
embed-server --server-config=standalone.xml --std-out=echo
batch
# main provider
/subsystem=keycloak-server:list-add(name=providers,value=module:keycloak-phone-provider)
/subsystem=keycloak-server/spi=SelfProviderFactory/:add
/subsystem=keycloak-server/spi=SelfProvider/provider=defaultselfprovider/:add(enabled=true)
run-batch
stop-embedded-server