欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于SSM+Shiro+Bootstrap實現(xiàn)用戶權(quán)限管理系統(tǒng)

 更新時間:2021年12月21日 10:18:22   作者:Bug終結(jié)者.  
這篇文章主要介紹了基于SSM+Shiro實現(xiàn)一個用戶權(quán)限管理系統(tǒng),每位用戶只可訪問指定的頁面,文中的示例代碼講解詳細,對我們學(xué)習(xí)或工作有一定幫助,快跟隨小編一起學(xué)習(xí)吧

引言

本篇博文基于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相關(guān)面試題匯總詳解

    springboot相關(guān)面試題匯總詳解

    這篇文章主要介紹了springboot相關(guān)面試題匯總詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-08-08
  • Springboot全局異常捕獲及try catch區(qū)別解析

    Springboot全局異常捕獲及try catch區(qū)別解析

    這篇文章主要介紹了Springboot全局異常捕獲及try catch區(qū)別解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06
  • logback高效狀態(tài)管理器StatusManager源碼解析

    logback高效狀態(tài)管理器StatusManager源碼解析

    這篇文章主要為大家介紹了logback高效狀態(tài)管理器StatusManager源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • springboot中poi使用操作方法

    springboot中poi使用操作方法

    在項目中,有很多對excel的操作,大都數(shù)時候我們都會使用poi工具類,本文將介紹poi的一些使用方法,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • lombok注解介紹小結(jié)

    lombok注解介紹小結(jié)

    lombok是一個可以幫助我們簡化java代碼編寫的工具類,這篇文章主要介紹了lombok注解介紹小結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • SpringBoot啟動過程的實現(xiàn)

    SpringBoot啟動過程的實現(xiàn)

    這篇文章主要介紹了SpringBoot啟動過程的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java實現(xiàn)Json字符串與Object對象相互轉(zhuǎn)換的方式總結(jié)

    Java實現(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-03
  • SpringBoot中的@RestControllerAdvice注解詳解

    SpringBoot中的@RestControllerAdvice注解詳解

    這篇文章主要介紹了SpringBoot中的@RestControllerAdvice注解詳解,RestControllerAdvice注解用于創(chuàng)建全局異常處理類,用于捕獲和處理整個應(yīng)用程序中的異常,需要的朋友可以參考下
    2024-01-01
  • Java流形式返回前端的實現(xiàn)示例

    Java流形式返回前端的實現(xiàn)示例

    ? Java后端開發(fā)項目時,需要給前端傳一些數(shù)據(jù),本文主要介紹了Java流形式返回前端的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Spring boot + mybatis + Vue.js + ElementUI 實現(xiàn)數(shù)據(jù)的增刪改查實例代碼(二)

    Spring boot + mybatis + Vue.js 

    這篇文章主要介紹了Spring boot + mybatis + Vue.js + ElementUI 實現(xiàn)數(shù)據(jù)的增刪改查實例代碼(二),非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-05-05

最新評論