/*
Navicat Premium Data Transfer
Source Server : localhost_3305
Source Server Type : MySQL
Source Server Version : 80030
Source Host : 127.0.0.1:3305
Source Schema : db0618
Target Server Type : MySQL
Target Server Version : 80030
File Encoding : 65001
Date: 15/01/2023 22:21:15
*/SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS =0;-- ------------------------------ Table structure for sys_user-- ----------------------------DROPTABLEIFEXISTS`sys_user`;CREATETABLE`sys_user`(`id`intNOTNULL,`username`varchar(100)CHARACTERSET utf8mb3 COLLATE utf8mb3_general_ci NULLDEFAULTNULL,`password`varchar(100)CHARACTERSET utf8mb3 COLLATE utf8mb3_general_ci NULLDEFAULTNULL,`salt`varchar(100)CHARACTERSET utf8mb3 COLLATE utf8mb3_general_ci NULLDEFAULTNULL,`suo`intNULLDEFAULT0,PRIMARYKEY(`id`)USINGBTREE)ENGINE=InnoDBCHARACTERSET= utf8mb3 COLLATE= utf8mb3_general_ci ROW_FORMAT = COMPACT;-- ------------------------------ Records of sys_user-- ----------------------------INSERTINTO`sys_user`VALUES(1,'admin','7e84f2fbdc9de493dc1e17c44b163ebc9168bc472f26db231f472f1012e62d87','q1',0);INSERTINTO`sys_user`VALUES(2,'aaa','18ae76b69e6b7b2ae78100013442beafb692bbbad663b1ff5845f0036b446ad7','q2',0);INSERTINTO`sys_user`VALUES(3,'bbb','7c8425aa02dfdfc973257f3b2a4ded786eadee830d7a29a900669727fa7a5966','q3',1);SET FOREIGN_KEY_CHECKS =1;
2 在config包下创建域(MysqlRealm类)
packagecom.zlz.config;importcom.zlz.entity.SysUser;importcom.zlz.mapper.SysUserMapper;importorg.apache.shiro.authc.*;importorg.apache.shiro.authz.AuthorizationInfo;importorg.apache.shiro.realm.AuthorizingRealm;importorg.apache.shiro.subject.PrincipalCollection;importorg.apache.shiro.util.ByteSource;importorg.springframework.beans.factory.annotation.Autowired;publicclassMysqlRealmextendsAuthorizingRealm{//授权方法@OverrideprotectedAuthorizationInfodoGetAuthorizationInfo(PrincipalCollection pc){returnnull;}@AutowiredSysUserMapper sysUserMapper;//认证方法@OverrideprotectedAuthenticationInfodoGetAuthenticationInfo(AuthenticationToken at)throwsAuthenticationException{//①在认证方法里面获取输入的用户名String username =(String) at.getPrincipal();SysUser user = sysUserMapper.findUserByUsername(username);if(user ==null){//②如果账户不存在,就抛出账户不存在异常thrownewUnknownAccountException();}//③检查账户是否锁定 如果锁定的话,那就会抛出锁定异常if(user.getSuo()==1){thrownewLockedAccountException();}//密码错误,shiro会自动帮你抛出密码错误这个异常的// System.out.println("实例对象名称:");//④拿到盐(放在第三个形参的位置)-----变动的地方ByteSource salt =ByteSource.Util.bytes(user.getSalt());//⑤把对应的参数设置进去SimpleAuthenticationInfo s=newSimpleAuthenticationInfo(user.getUsername(),user.getPassword(),salt,getName());return s;}}