需求:
在公司新的系统里面博主我使用的是ApachShiro 作为安全框架、作为后端的鉴权以及登录、分配权限等操作 管理员的信息都是存储在管理员表
前台App 用户也需要校验用户名和密码进行登录、但是用户的信息却是存在另一张表里面、如何给这两个不同的数据表进行登录?鉴权呢?
当然 按照Shiro的强大,我们完全可以用一个接口作为登录的验证、不同的Realm 来执行不同的逻辑即可
相关知识储备 Realm
用最简单的话来说 一个Realm就是一个检验用户身份的组件,但这里这个组件需要我们继承去重写,因为每个系统有各自不同的业务逻辑,这些事情是Shiro所不能了解的,我们得通过这个
Realm 告诉Shiro 我们的密码是怎么加密得到的,还有用户名是哪个,以及加密的方式是啥
————————————————————————————————————————————
了解这些需要了解的东西之后,我们模仿现有的Realm,照猫画虎的来一个
@Component public class WeChatRealm extends AuthorizingRealm { @Autowired private VehicleOwnerService vehicleOwnerService; @Autowired private SysUserService sysUserService; /** * 授权 微信接口没有权限 * * @param principals * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { //没有权限机制返回Null即可 return null; } /* * @Author MRC * @Description 认证 * @Date 11:36 2019/9/11 * @Param [token] * @return org.apache.shiro.authc.AuthenticationInfo **/ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { System.out.println("微信登录认证"); //登录用户名 String username = (String) token.getPrincipal(); Wrapper<VehicleOwner> vehicleOwnerWrapper = new EntityWrapper<>(); vehicleOwnerWrapper.eq("phone",username); VehicleOwner vehicleOwner = vehicleOwnerService.selectOne(vehicleOwnerWrapper); if (vehicleOwner == null) { //找不到这个用户直接返回null return null; } //