之五 2流高手速成記:Springboot整合Shiro實現安全管理

廢話不多說 , 咱們直接接上回
上一篇我們講了如何使用Springboot框架整合Nosql,并于文章最后部分引入了服務端Session的概念
而早在上上一篇中 , 我們則已經講到了如何使用Springboot框架整合Mybatis/MybatisPlus實現業務數據的持久化(寫入數據庫)
本篇我們把關注點放在一個于這兩部分有共同交集的內容——安全管理,并且引入我們今天的主角——Shiro框架

Apache Shiro是一個強大且易用的Java安全框架,執行身份驗證、授權、密碼和會話管理 。使用Shiro的易于理解的API,您可以快速、輕松地獲得任何應用程序,從最小的移動應用程序到最大的網絡和企業應用程序 。
—— 來自百度百科
Shiro框架包含三個核心組件:
Subject —— 泛指當前與Shiro交互中的實體,可以是用戶或者某后臺進程
SecurityManager —— Shiro的核心組件,對內管理各種組件實例,對外提供各種安全服務
Realm —— Shiro與安全數據之間的橋接器
【之五 2流高手速成記:Springboot整合Shiro實現安全管理】Shiro框架還包含有其他諸多概念 , 為降低大家的心智負擔,這些我們暫且不談 , 文末會給大家推薦延展閱讀的相關文章
還是老規矩直接上干貨,以完整的實例讓大家對【如何基于Shiro實現權限的細粒度控制】有一個整體上的認知
之五 2流高手速成記:Springboot整合Shiro實現安全管理

文章插圖
 
之五 2流高手速成記:Springboot整合Shiro實現安全管理

文章插圖
不知道大家會不會覺得項目結構突然變復雜?別擔心,接下來我會給大家逐一拆解
1. 創建數據表首先是角色表——role
之五 2流高手速成記:Springboot整合Shiro實現安全管理

文章插圖

之五 2流高手速成記:Springboot整合Shiro實現安全管理

文章插圖
然后是用戶表——user
之五 2流高手速成記:Springboot整合Shiro實現安全管理

文章插圖

之五 2流高手速成記:Springboot整合Shiro實現安全管理

文章插圖
最后是權限表——permission
之五 2流高手速成記:Springboot整合Shiro實現安全管理

文章插圖

之五 2流高手速成記:Springboot整合Shiro實現安全管理

文章插圖
2. 創建三個對應的Mapperpackage com.example.hellospringboot.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.example.hellospringboot.model.Role;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Repository;@Mapper@Repositorypublic interface RoleMapper extends BaseMapper<Role> {}package com.example.hellospringboot.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.example.hellospringboot.model.User;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Repository;@Mapper@Repositorypublic interface UserMapper extends BaseMapper<User> {}package com.example.hellospringboot.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.example.hellospringboot.model.Permission;import org.apache.ibatis.annotations.Mapper;import org.springframework.stereotype.Repository;@Mapper@Repositorypublic interface PermissionMapper extends BaseMapper<Permission> {}
這里我們用到了上上一節講到的內容
這里的Mapper會輔助于后續的安全數據讀取
3. 接下來是Service及其實現類package com.example.hellospringboot.service;import com.example.hellospringboot.model.Role;public interface RoleService {Role findRoleById(int id);}package com.example.hellospringboot.service.impl;import com.example.hellospringboot.mapper.RoleMapper;import com.example.hellospringboot.model.Role;import com.example.hellospringboot.service.RoleService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;@Servicepublic class RoleServiceImpl implements RoleService {@AutowiredRoleMapper mapper;public Role findRoleById(int id){Role role = mapper.selectById(id);return role;}}package com.example.hellospringboot.service;import com.example.hellospringboot.model.User;public interface UserService {boolean checkUserByUsernameAndPassword(String userName, String passWord);User findUserByUserName(String userName);}package com.example.hellospringboot.service.impl;import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.example.hellospringboot.mapper.UserMapper;import com.example.hellospringboot.model.User;import com.example.hellospringboot.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserServiceImpl implements UserService {@AutowiredUserMapper mapper;public boolean checkUserByUsernameAndPassword(String userName, String passWord){QueryWrapper<User> wrapper = new QueryWrapper<User>();wrapper = wrapper.eq("user_name", userName).eq("pass_word",passWord);List<User> userList = mapper.selectList(wrapper);return userList.size() > 0;}public User findUserByUserName(String userName){QueryWrapper<User> wrapper = new QueryWrapper<User>();wrapper = wrapper.eq("user_name", userName);User user = mapper.selectOne(wrapper);return user;}}

推薦閱讀