Shiro教程7(整合SSM項目-授權)

首先授權必須是在認證通過之後才會執行的操作,之前我們在Shiro教程4(授權操作)該教程中講過,獲取權限我們是通過如下方法實現的

Shiro教程7(整合SSM項目-授權)

那麼在自定義Realm中授權是怎麼實現的呢?
我們跟蹤代碼來看
首先從 hasRole()方法來看。

Shiro教程7(整合SSM項目-授權)

Shiro教程7(整合SSM項目-授權)

Shiro教程7(整合SSM項目-授權)

Shiro教程7(整合SSM項目-授權)

Shiro教程7(整合SSM項目-授權)

Shiro教程7(整合SSM項目-授權)

到此我們發現要對用戶授權的話,我們只需要在定義Realm的doGetAuthorizationInfo方法中來授權。

/**
* 授權方法
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
\tString userName = (String) principals.getPrimaryPrincipal();
\tSystem.out.println("登錄的賬號是:"+userName);
\t// ---- 根據賬號去數據庫中查詢對應的角色和權限
\t// 此處我們將權限寫死來模擬

\tSimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
\t// 固定設置一個角色
\tinfo.addRole("role1");
\t// 固定設置一個權限
\tinfo.addStringPermission("user:query");
\treturn info;
}

在SpringMVC的配置文件中開啟shiro註解

<dependency>
\t<groupid>org.springframework/<groupid>
\t<artifactid>spring-aop/<artifactid>
\t<version>4.3.21.RELEASE/<version>
/<dependency>
<dependency>
\t<groupid>org.springframework/<groupid>
\t<artifactid>spring-aspects/<artifactid>
\t<version>4.3.21.RELEASE/<version>
/<dependency>

添加依賴

<dependency>
\t<groupid>org.springframework/<groupid>
\t<artifactid>spring-aop/<artifactid>
\t<version>4.3.21.RELEASE/<version>
/<dependency>
<dependency>
\t<groupid>org.springframework/<groupid>
\t<artifactid>spring-aspects/<artifactid>
\t<version>4.3.21.RELEASE/<version>
/<dependency>

springMVC配置文件


<beans>\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
\txmlns:context="http://www.springframework.org/schema/context"
\txmlns:aop="http://www.springframework.org/schema/aop"
\txmlns:mvc="http://www.springframework.org/schema/mvc"
\txsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
\t\thttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
\t\thttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
\t\thttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
\t\t

\t
\t<component-scan>\t
\t
\t<annotation-driven>
\t
\t
\t<config>
\t<bean>
\t\t<property>
\t/<bean>
/<beans>

正常訪問

Shiro教程7(整合SSM項目-授權)

登錄成功後可以正常訪問

Shiro教程7(整合SSM項目-授權)

註解權限驗證

 @RequiresRoles("role2") // 驗證角色
//@RequiresPermissions("user:delete") // 驗證權限
@RequestMapping("/query")
public String query(){
\tSystem.out.println("查詢用戶的方法");
\treturn "/user.jsp";
}

測試

Shiro教程7(整合SSM項目-授權)

權限驗證生效。

指定沒有權限訪問的跳轉地址

Shiro教程7(整合SSM項目-授權)

注意雖然配置了此設置,但是在使用註解的形式下,未授權的情況下還是不會跳轉,此時我們需要在SpringMVC中添加對應的攔截器處理


<beans>\txmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
\txmlns:aop="http://www.springframework.org/schema/aop" xmlns:mvc="http://www.springframework.org/schema/mvc"
\txsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

\t\thttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
\t\thttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
\t\thttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

\t
\t<component-scan>
\t
\t<annotation-driven>

\t
\t<config>
\t<bean>\t\tclass="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
\t\t<property>
\t/<bean>

\t
\t<bean>\t\tclass="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
\t\t<property>
\t\t\t<props>
\t\t\t\t
\t\t\t\t<prop>redirect:/jsp/refuse.jsp/<prop>
\t\t\t\t<prop>redirect:/login.jsp/<prop>
\t\t\t\t
\t\t\t\t
\t\t\t/<props>
\t\t/<property>
\t/<bean>
/<beans>

再測試

當我們登錄成功後訪問http://localhost:8082/query後被重定向到了此地址:

Shiro教程7(整合SSM項目-授權)

jsp標籤驗證

引入標籤庫

標籤驗證

Shiro教程7(整合SSM項目-授權)

用戶管理:


<hasrole>
\t
/<hasrole>
<hasrole>
\t
/<hasrole>
<haspermission>

\t
/<haspermission>
<haspermission>
\t
/<haspermission>

測試

用戶具有的角色和權限

Shiro教程7(整合SSM項目-授權)

訪問方法權限放開

Shiro教程7(整合SSM項目-授權)

登錄訪問:

Shiro教程7(整合SSM項目-授權)

shiro標籤說明

shiro:authenticated

表示已認證通過

<authenticated>
<label>用戶身份驗證已通過 /<label>
/<authenticated>

說明:只有已通過用戶認證,但不是通過記住我(remember me)瀏覽才會看到標籤內的內容

shiro:guest

<guest>
<label>您當前是遊客,/<label>
/<guest>

說明:只有是沒有登錄過,以遊客的身份瀏覽才會看到標籤內的內容

shiro:hashRole

表示擁有某一角色

<hasrole>
<label>這個用戶擁有的角色是admin/<label>
/<hasrole>

說明:只有成功登錄後,且具有admin角色的用戶才可以看到標籤內的容,name屬性中只能填寫一個角色的名稱

shiro:hasAnyRoles

表示擁有多個角色中的一個即可

<hasanyroles>
<label>這是擁有admin或者是user角色的用戶/<label>
/<hasanyroles>

說明:只有成功登錄後,且具有admin或者user角色的用戶才會看到標籤內的內容;name屬性中可以填寫多個角色名稱,以逗號(,)分隔

shiro:hasPermission

表示擁有某一權限

<haspermission>
<label>這個用戶擁有admin:add的權限/<label>
/<haspermission>

說明:只有成功登錄後,且具有admin:add權限的用戶才可以看到標籤內的內容,name屬性中只能填寫一個權限的名稱

shiro:lacksRole

表示不擁有某一角色

<lacksrole>
<label>這個用戶不擁有admin的角色/<label>
/<lacksrole>

說明:只有成功登錄後,且不具有admin角色的用戶才可以看到標籤內的內容,name屬性中只能填寫一個角色的名稱

shiro:lacksPermission

表示不擁有某一權限

<lackspermission>
<label>這個用戶不擁有admin:delete的權限/<label>
/<lackspermission>

說明:只有成功登錄後,且不具有admin:delete權限的用戶才可以看到標籤內的內容,name屬性中只能填寫一個權限的名稱

shiro:notAuthenticated

表示沒有通過驗證

<notauthenticated>
<label>用戶身份驗證沒有通過(包括通過記住我(remember me)登錄的) /<label>
/<notauthenticated>

說明:只有沒有通過驗證的才可以看到標籤內的內容,包括通過記住我(remember me)登錄的

shiro:principal

表示用戶的身份

取值取的是你登錄的時候,在Realm 實現類中的new SimpleAuthenticationInfo(第一個參數,…) 放的第一個參數

Shiro教程7(整合SSM項目-授權)

1.如果第一個放的是username或者是一個值 ,那麼就可以直接用。

 
<shiro>

2.如果第一個參數放的是對象,比如放User 對象。那麼如果要取其中某一個值,可以通過property屬性來指定。

 
<principal>

shiro:user

表示已登錄

<user>
<label>歡迎[<principal>],/<label>
/<user>
Shiro教程7(整合SSM項目-授權)



分享到:


相關文章: