由于在实际项目中 , 登录模块使用的shiro进行登录认证, 首先会根据用户名进行查询. 但是实际出现的bug是,无论用户名是大写还是小写都能登录成功, 最后查找到的原因是mysql在查询时, 没有进行大小写的区分.
解决方法:
- 查询时指定大小写敏感 即使用
collate utf8_bin
指定大小写, 如下
select id, account, name, birthday,password, sex, email, avatar,
phone, roleid,salt,
deptid, status,
createtime, version
from sys_user where account = "admin" collate utf8_bin and status != 3
- 建表时指定大小敏感 例如在建表语句的后面加上指定大小写,
COLLATE utf8_bin
-- 大小写敏感
CREATE TABLE `test1` (
`str` char(255) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 大小写不敏感
CREATE TABLE `test2` (
`str` char(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;