Version: Next

Shrio整合Mybatis

依赖与配置文件

<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>

导入模板引擎——freemarker

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

导入Druid依赖

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>

导入Mysql依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>

导入Mybatis依赖

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>

导入lombok依赖

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
  • application.yaml添加freemarker基本设置、设置数据源设置、配置Mybatis别名和mapper.xml路径
spring:
freemarker:
cache: false # 缓存配置 开发阶段应该配置为false 因为经常会改
suffix: .ftl # 模版后缀名 默认为ftl / 还是用ftl吧,html没freemarker语法提示
charset: UTF-8 # 文件编码
template-loader-path: classpath:/templates/
datasource:
username: root
password: root
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource # 配置使用Druid数据源
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:ttps://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
# 配置Mybatis
# 也可以不配置,使用全限定类名,然后在resources路径下建立和mapper接口一样的包路径
mybatis:
type-aliases-package: com.bsx.shiro.pojo # 别名
mapper-locations: classpath:mapper/*.xml # 扫描mapper.xml文件路径

pojo、mapper、mapper.xml

  • pojo
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String name;
private String password;
}
  • mapper
@Repository
@Mapper
public interface UserMapper {
public User queryUserByName(String name);
}
  • UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bsx.shiro.mapper.UserMapper">
<select id="queryUserByName" resultType="com.bsx.shiro.pojo.User">
SELECT * FROM USER WHERE name = #{name}
</select>
</mapper>

Service、ServiceImpl

  • Service
public interface UserService {
public User queryUserByName(String name);
}
  • ServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User queryUserByName(String name) {
return userMapper.queryUserByName(name);
}
}

测试Mybatis

表中数据:


@SpringBootTest
class Springboot06ShiroApplicationTests {
@Autowired
UserServiceImpl userServiceImpl;
@Test
void contextLoads() {
User user = userServiceImpl.queryUserByName("bb");
System.out.println(user);
}
}

Shiro Realm使用Mybatis

  • UserRealm中使用UserServiceImpl
public class UserRealm extends AuthorizingRealm {
@Autowired
UserServiceImpl userServiceImpl;
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
System.out.println("执行了<<授权>>方法");
return null;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
System.out.println("执行了<<认证>>方法");
//查询数据库获得用户数据
//伪造用户名密码
// String username = "root";
// String password = "123456";
//通过Mybatis查询数据库
// if (!token.getUsername().equals(username)) {
// return null; //Shiro自动抛出 UnknownAccountException
// }
UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
User user = userServiceImpl.queryUserByName(token.getUsername());
if (user == null) {
return null; //Shiro自动抛出 UnknownAccountException
}
//密码认证,shiro会自动做
return new SimpleAuthenticationInfo("", user.getPassword(), "");
}
}