基于SSM+Shiro+Bootstrap實現(xiàn)用戶權(quán)限管理系統(tǒng)
引言
本篇博文基于SSM+Shiro實現(xiàn)用戶權(quán)限管理系統(tǒng),每位用戶只可訪問指定的頁面,具體需求如下
需求
用戶賬號的增刪改查功能
權(quán)限包括: 系統(tǒng)模塊操作權(quán)限(system),財務(wù)模塊操作權(quán)限(finance),考勤模塊操作權(quán)限(checkon),
每個用戶都可能擁有0或多個權(quán)限,在新增和編輯用戶的時候,可以多選權(quán)限。
系統(tǒng)模塊:包括用戶賬號管理功能。
財務(wù)模塊:為了開發(fā)簡化,只要做出靜態(tài)的頁面即可,不要真的有財務(wù)模塊。
考勤模塊:同財務(wù)模塊。
效果圖
功能細節(jié)
- 技術(shù)棧: Maven+SSM+Shiro + Bootstarp
- 操作者必須登錄,且擁有system權(quán)限才可以訪問system/* 下的所有頁面和功能
- 操作者必須登錄,且擁有 finance 權(quán)限才可以訪問 finance/* 下的所有頁面和功能
- 操作者必須登錄,且擁有 checkon 權(quán)限才可以訪問 checkon/* 下的所有頁面和功能
- 用戶的新增、編輯、刪除、列表功能,都屬于 system/* 下面的頁面和功能。
- 操作者如果未登錄,會被踢到登錄頁面
- 操作者如果訪問需要權(quán)限的頁面,但沒有權(quán)限,會被踢到 noauthor 頁面
- 用戶新增和編輯頁面,必須驗證登錄密碼和二次驗證密碼相同
- 用戶名必須是唯一的
- 用戶新增的時候必須輸入用戶名
- 用戶編輯的時候,用戶名為只讀的
- 用戶編輯的時候,可以不輸入密碼
- 用戶編輯頁面打開的時候,不要回顯密碼
- 用戶新增的時候必須輸入登錄密碼
- 用戶編輯回顯的時候必須回顯昵稱和擁有權(quán)限
分頁采用PageHelper插件
數(shù)據(jù)表準備
用戶表:t_shiro_user
CREATE TABLE `t_shiro_user` ( `noid` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(32) NOT NULL, `userpwd` varchar(32) NOT NULL, `nickname` varchar(32) DEFAULT NULL, PRIMARY KEY (`noid`), UNIQUE KEY `uniq_username` (`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
權(quán)限表:t_shiro_permission
CREATE TABLE `t_shiro_permission` ( `noid` int(11) NOT NULL AUTO_INCREMENT, `permission_code` varchar(32) NOT NULL COMMENT '權(quán)限代號', `permission_describe` varchar(32) NOT NULL COMMENT '權(quán)限描述', PRIMARY KEY (`noid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
權(quán)限代號是開發(fā)的時候需要用的字符串,一般都是英文字符串,比如“system”。
權(quán)限描述是給最終軟件使用方(操作者)看的,比如 “系統(tǒng)模塊操作權(quán)限”
權(quán)限數(shù)據(jù)是固定的,這些數(shù)據(jù)是設(shè)計時的,即在開發(fā)前就確定的東西,不會在項目運行階段再變化;如果需要變化就需要重新開發(fā)相關(guān)的代碼。
權(quán)限表的數(shù)據(jù)如下:
INSERT INTO `t_shiro_permission` (`permission_code`, `permission_describe`) VALUES ('system', '系統(tǒng)模塊操作權(quán)限'); INSERT INTO `t_shiro_permission` (`permission_code`, `permission_describe`) VALUES ('finance', '財務(wù)模塊操作權(quán)限'); INSERT INTO `t_shiro_permission` (`permission_code`, `permission_describe`) VALUES ('checkon', '考勤模塊操作權(quán)限');
pom文件
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.13</version> </dependency> <!-- mybatis 相關(guān)的依賴 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.13</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- shiro依賴--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.8.0</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.8.0</version> </dependency> <!-- 分頁插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <maven.compiler.encoding>UTF-8</maven.compiler.encoding> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8087</port> <path>/ssm_shiro</path> <uriEncoding>UTF-8</uriEncoding> </configuration> </plugin> </plugins> </build>
項目結(jié)構(gòu)
核心源碼
MyRealm自定義權(quán)限類,該類實現(xiàn)了用戶認證與授權(quán)
@Component public class MyRealm extends AuthorizingRealm { @Autowired private ShiroUserMapper shiroUserMapper; @Autowired private UserPermissionMapper userPermissionMapper; /** * 自定義授權(quán)方法 * 思路:根據(jù)PrincipalCollection對象獲取用戶名,根據(jù)用戶名查詢對象, * 查詢該用戶在數(shù)據(jù)表中所對應(yīng)的權(quán)限,并轉(zhuǎn)換為set集合,存入SimpleAuthorizationInfo對象并返回 * @param principalCollection * @return */ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { //獲取用戶名 String username = (String) principalCollection.getPrimaryPrincipal(); //根據(jù)用戶名獲取用戶對象 ShiroUser shiroUser = new ShiroUser(); shiroUser.setUsername(username); ShiroUser user = shiroUserMapper.get(shiroUser); //用戶非空判斷 if (user != null) { //獲取用戶所對應(yīng)的權(quán)限 UserPermission userPermission = new UserPermission(); userPermission.setUser_id(user.getNoid()); List<String> list = userPermissionMapper.list(userPermission); //List集合轉(zhuǎn)為Set集合放入授權(quán)權(quán)限實例對象中 Set<String> perms = new HashSet<>(); for (String s : list) { perms.add(s); } SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo(); authorizationInfo.addStringPermissions(perms); return authorizationInfo; } return null; } /** * 自定義認證方法,根據(jù)AuthenticationToken對象獲取用戶名, * 查詢用戶名對應(yīng)的都系,并進行驗證是否正確,最后返回AuthenticationInfo對象 * @param authenticationToken * @return * @throws AuthenticationException */ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { String username = (String) authenticationToken.getPrincipal(); ShiroUser shiroUser = new ShiroUser(); shiroUser.setUsername(username); ShiroUser user = shiroUserMapper.get(shiroUser); if (user != null) { // 這一步就執(zhí)行了對密碼的驗證 AuthenticationInfo authcInfo=new SimpleAuthenticationInfo(user.getUsername(),user.getUserpwd() , getName()); return authcInfo; } else { return null; } } }
核心配置文件applicationContent.xml
<!-- 安全管理,將 myRealm嵌入 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="myRealm"/> </bean> <!-- Shiro過濾器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- Shiro的核心安全接口,這個屬性是必須的 --> <property name="securityManager" ref="securityManager"/> <!-- 身份認證失敗,則跳轉(zhuǎn)到登錄頁面的配置 --> <property name="loginUrl" value="/user/login"/> <!-- 權(quán)限認證失敗,則跳轉(zhuǎn)到指定頁面 --> <property name="unauthorizedUrl" value="/system/noauthor"/> <!-- Shiro連接約束配置,即過濾鏈的定義 --> <property name="filterChainDefinitions"> <value> <!-- 游客身份,表示登錄就可以訪問--> /loginPost=anon <!-- user賦值的不登錄,無無法訪問,直接就是認證失敗。--> /system/*=user <!-- 訪問以finance打頭的接口必須擁有finance權(quán)限--> /finance/*=perms[finance] <!-- 訪問以checkon打頭的接口必須擁有checkon權(quán)限--> /checkon/*=perms[checkon] <!-- 退出登錄,無需寫controller,只寫配置及寫對應(yīng)的跳轉(zhuǎn)即可實現(xiàn)退出登錄--> /logout.action=logout </value> </property> </bean> <!-- 保證實現(xiàn)了Shiro內(nèi)部lifecycle函數(shù)的bean執(zhí)行 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/> <!-- 開啟Shiro注解 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager"/> </bean>
Shiro退出登錄
applicationContent.xml
<!-- 退出登錄,無需寫controller,只寫配置及寫對應(yīng)的跳轉(zhuǎn)即可實現(xiàn)退出登錄--> /logout.action=logout
通用jsp,inc.jsp
<%--點擊該鏈接,applicationContent.xml中配置的退出登錄進行攔截, shiro內(nèi)部實現(xiàn)退出登錄并清空緩存 --%> <a href="${APP_PATH}/logout.action" rel="external nofollow" ${user == null ? 'style="display: none"':'style="display: inline-block"'} class="btn btn-danger">退出登錄</a>
啟動項目命令
mvn clean tomcat7:run
IDEA配置如圖
建議采用DeBug方式啟動?
到此這篇關(guān)于基于SSM+Shiro+Bootstrap實現(xiàn)用戶權(quán)限管理系統(tǒng)的文章就介紹到這了,更多相關(guān)SSM+Shiro+Bootstrap用戶權(quán)限管理系統(tǒng)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot全局異常捕獲及try catch區(qū)別解析
這篇文章主要介紹了Springboot全局異常捕獲及try catch區(qū)別解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06logback高效狀態(tài)管理器StatusManager源碼解析
這篇文章主要為大家介紹了logback高效狀態(tài)管理器StatusManager源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11Java實現(xiàn)Json字符串與Object對象相互轉(zhuǎn)換的方式總結(jié)
這篇文章主要介紹了Java實現(xiàn)Json字符串與Object對象相互轉(zhuǎn)換的方式,結(jié)合實例形式總結(jié)分析了java基于Json-Lib、Org.Json、Jackson、Gson、FastJson五種方式轉(zhuǎn)換json類型相關(guān)操作技巧,需要的朋友可以參考下2019-03-03SpringBoot中的@RestControllerAdvice注解詳解
這篇文章主要介紹了SpringBoot中的@RestControllerAdvice注解詳解,RestControllerAdvice注解用于創(chuàng)建全局異常處理類,用于捕獲和處理整個應(yīng)用程序中的異常,需要的朋友可以參考下2024-01-01Spring boot + mybatis + Vue.js 
這篇文章主要介紹了Spring boot + mybatis + Vue.js + ElementUI 實現(xiàn)數(shù)據(jù)的增刪改查實例代碼(二),非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05