keycloak-mybatis整合
keycloak-整合mybatis
为方便一些不会jpa的同学,特意整理了一下与mybatis的结合使用方式。
放码过来。
依赖\配置
数据源配置参考: keycloak-多数据源配置
maven 依赖和打包配置。pom.xml
<!--...-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--...-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<webResources>
<resource>
<directory>${project.build.directory}/classes/META-INF/services</directory>
<targetPath>META-INF/services</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<targetPath>WEB-INF/classes</targetPath>
<includes>
<include>**/*Mapper.xml</include><!--关键点-->
</includes>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
</plugin>
<!--...-->
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
resources/META-INF
加入mybatisconfig.xml
配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="MANAGED"/>
<dataSource type="JNDI">
<property name="data_source" value="java:jboss/datasources/DapengXADS"/><!--自定义数据源名称要一致-->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dapeng/cloud/models/mybatis/UserMapper.xml"/>
</mappers>
</configuration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
UserStorageProviderFactory
@JBossLog
@AutoService(MybatisStorageProviderFactory.class)
public class MybatisStorageProviderFactory implements UserStorageProviderFactory<DapengUserStorageProvider>{
private static final String PROVIDER_NAME="dapeng-jdbc";
private static final String MYBATIS_CONFIG="META-INF/mybatis-config.xml";
private SqlSessionFactory sqlSessionFactory;
@Override
public void init(Scope config) {
try {
InputStream in = Resources.getResourceAsStream(MYBATIS_CONFIG);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
log.error("创建sqlSessionFactory失败",e);
e.printStackTrace();
}
}
public String getId() {
return PROVIDER_NAME;
}
public MybatisUserStorageProvider create(KeycloakSession keycloakSession, ComponentModel componentModel) {
return new MybatisUserStorageProvider(keycloakSession,componentModel,sqlSessionFactory.openSession());
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
MybatisUserStorageProvider
@JBossLog
@Data
@AllArgsConstructor
public class MybatisUserStorageProvider implements UserStorageProvider, UserLookupProvider,
CredentialInputValidator {
private KeycloakSession session;
private ComponentModel componentModel;
private SqlSession sqlSession;
@Override
public UserModel getUserById(String id,
RealmModel realmModel) {
User user = (User)sqlSession.selectOne("com.dapeng.cloud.models.mybatis.UserMapper.findById", id);
log.debugv("用户:{0}",user);
return new UserAdapter(session,realmModel,componentModel,user);
}
@Override
public UserModel getUserByUsername(String username, RealmModel realmModel) {
log.debugv("获取用户信息 用户名:{0},域:{1}",username,realmModel.getName());
User user =(User)sqlSession.selectOne("com.dapeng.cloud.models.mybatis.UserMapper.findByMobile", username);
return new UserAdapter(session,realmModel, componentModel,user);
}
@Override
public UserModel getUserByEmail(String s, RealmModel realmModel) {
return null;
}
@Override
public boolean supportsCredentialType(String credentialType) {
return PasswordCredentialModel.TYPE.equals(credentialType);
}
@Override
public boolean isConfiguredFor(RealmModel realm, UserModel user, String credentialType) {
return PasswordCredentialModel.TYPE.equals(credentialType)&&user instanceof UserAdapter;
}
@Override
public boolean isValid(RealmModel realm, UserModel user,
CredentialInput credentialInput) {
if (!supportsCredentialType(credentialInput.getType())) return false;
String in = credentialInput.getChallengeResponse();
log.infof("input password: %s ,", in);
return "123456".equals(in);
}
@Override
public void close() {
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
剩下的就是mybatis的常规操作了,这里不在赘述。
有问题请留言。
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wzslw@163.com
文章标题:keycloak-mybatis整合
文章字数:675
本文作者:武继明
发布时间:2020-06-08, 14:39:23
最后更新:2020-08-21, 06:30:57
原始链接:https://www.omingo.com/2020/06/08/keycloak-mybatis整合/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。
来做第一个留言的人吧!