一.用戶身份認(rèn)證
身份認(rèn)證筏养,就是判斷一個(gè)用戶是否為合法用戶的處理過程沸移。最常用的簡單身份認(rèn)證方式是系統(tǒng)通過核對用戶輸入的用戶名和口令丈牢,看其是否與系統(tǒng)中存儲(chǔ)的該用戶的用戶名和口令一致车柠,來判斷用戶身份是否正確跌宛。對于采用指紋等系統(tǒng)相种,則出示指紋威恼;對于硬件Key等刷卡系統(tǒng),則需要刷卡。
-
用戶名密碼身份認(rèn)證流程
Subject:主體
訪問系統(tǒng)的用戶箫措,主體可以是用戶缭黔、程序等,進(jìn)行認(rèn)證的都稱為主體蒂破;Principal:身份信息
是主體(subject)進(jìn)行身份認(rèn)證的標(biāo)識(shí)馏谨,標(biāo)識(shí)必須具有唯一性,如用戶名附迷、手機(jī)號(hào)惧互、郵箱地址等,一個(gè)主體可以有多個(gè)身份喇伯,但是必須有一個(gè)主身份(Primary Principal)喊儡。credential:憑證信息
是只有主體自己知道的安全信息,如密碼稻据、證書等艾猜。
二.授權(quán)
授權(quán),即訪問控制捻悯,控制誰能訪問哪些資源匆赃。主體進(jìn)行身份認(rèn)證后需要分配權(quán)限方可訪問系統(tǒng)的資源,對于某些資源沒有權(quán)限是無法訪問的今缚。
image.png
三配置步驟:
jar包的配置
shiro-web
的jar
shiro-spring
的jar
shiro-code
的jar
備注:
shiro-all
可以加載所有的shiro包
- pom.xml
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.4.0</version>
</dependency>
- web.xml
.........
<!-- Shiro配置 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
.........
- shiro 過濾器
過濾器簡稱 | 對應(yīng)的java類 | 權(quán)限 |
---|---|---|
anon | org.apache.shiro.web.filter.authc.AnonymousFilter | 可以匿名訪問 |
authc | org.apache.shiro.web.filter.authc.FormAuthenticationFilter | 基于表單的攔截器算柳;如“/**=authc” |
authcBasic | org.apache.shiro.web.filter.authc.BasicHttpAuthenticationFilter | Basic HTTP身份驗(yàn)證攔截器 |
perms | org.apache.shiro.web.filter.authz.PermissionsAuthorizationFilter | 權(quán)限授權(quán)攔截器,驗(yàn)證用戶是否擁有所有權(quán)限姓言; |
port | org.apache.shiro.web.filter.authz.PortFilter | 端口攔截器瞬项,主要屬性:port(80):可以通過的端口 |
rest | org.apache.shiro.web.filter.authz.HttpMethodPermissionFilter | rest風(fēng)格攔截器,自動(dòng)根據(jù)請求方法構(gòu)建權(quán)限字符串 |
roles | org.apache.shiro.web.filter.authz.RolesAuthorizationFilter | 角色授權(quán)攔截器何荚,驗(yàn)證用戶是否擁有所有角色 |
ssl | org.apache.shiro.web.filter.authz.SslFilter | SSL攔截器囱淋,只有請求協(xié)議是https才能通過 |
user | org.apache.shiro.web.filter.authc.UserFilter | 用戶攔截器,用戶已經(jīng)身份驗(yàn)證/記住我登錄的 |
logout | org.apache.shiro.web.filter.authc.LogoutFilter | 退出攔截器餐塘,主要屬性:redirectUrl:退出成功后重定向的地址(/) |
- anon:例子/admins/**=anon 沒有參數(shù)妥衣,表示可以匿名使用。
- authc:例如/admins/user/**=authc表示需要認(rèn)證(登錄)才能使用唠倦,F(xiàn)ormAuthenticationFilter是表單認(rèn)證称鳞,沒有參數(shù)
- perms:例子/admins/user/=perms[user:add:],參數(shù)可以寫多個(gè),多個(gè)時(shí)必須加上引號(hào)稠鼻,并且參數(shù)之間用逗號(hào)分割冈止,例如/admins/user/=perms["user:add:,user:modify:*"],當(dāng)有多個(gè)參數(shù)時(shí)必須每個(gè)參數(shù)都通過才通過候齿,想當(dāng)于isPermitedAll()方法熙暴。
- user:例如/admins/user/**=user沒有參數(shù)表示必須存在用戶, 身份認(rèn)證通過或通過記住我認(rèn)證通過的可以訪問闺属,當(dāng)?shù)侨氩僮鲿r(shí)不做檢查
- spring-shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!-- 配置shiro的過濾器工廠類,id- shiroFilter要和我們在web.xml中配置的過濾器一致 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<!-- 調(diào)用我們配置的權(quán)限管理器 -->
<property name="securityManager" ref="securityManager" />
<!-- 配置我們的登錄請求地址 -->
<property name="loginUrl" value="/login.jsp" />
<!-- 配置我們在登錄頁登錄成功后的跳轉(zhuǎn)地址周霉,如果你訪問的是非/login地址掂器,則跳到您訪問的地址 -->
<property name="successUrl" value="/index.jsp" />
<!-- 如果您請求的資源不再您的權(quán)限范圍,則跳轉(zhuǎn)到/403請求地址 -->
<property name="unauthorizedUrl" value="/unauthorized" />
<!-- 權(quán)限配置 -->
<property name="filterChainDefinitions">
<value>
<!-- anon表示此地址不需要任何權(quán)限即可訪問 -->
/login=anon
/icon/**=anon
/js/**=anon
/logout=logout
<!--所有的請求(除去配置的靜態(tài)資源請求或請求地址為anon的請求)都要通過登錄驗(yàn)證,如果未登錄則跳到/login -->
/** = authc
</value>
</property>
</bean>
<!-- ==============================================2俱箱、安全管理器============================================== -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="realm" ref="userRealm" />
</bean>
<!-- 會(huì)話管理器 -->
<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<!-- session的失效時(shí)長国瓮,單位毫秒 -->
<property name="globalSessionTimeout" value="600000"/>
<!-- 刪除失效的session -->
<property name="deleteInvalidSessions" value="true"/>
</bean>
<!--==============================================3、realm===================================================-->
<!-- Shiro配置,繼承自AuthorizingRealm的自定義Realm (解決初始化時(shí)的依賴循環(huán)問題狞谱,通過這里向realm中注入userservice實(shí)現(xiàn))-->
<bean id="userRealm" class="com.thoughtWorks.shiro.CustomRealm" >
</bean>
<!--4 憑證匹配器 -->
<bean id="credentialsMatcher"
class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="md5" />
<property name="hashIterations" value="1" />
</bean>
<!--==============================================rememberMeManager管理器========================================-->
<!-- rememberMeManager管理器 -->
<bean id="rememberMeManager" class="org.apache.shiro.web.mgt.CookieRememberMeManager">
<property name="cookie" ref="rememberMeCookie" />
</bean>
<!-- 記住我cookie -->
<bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
<constructor-arg value="rememberMe" />
<!-- 記住我cookie生效時(shí)間30天 -->
<property name="maxAge" value="2592000" />
</bean>
</beans>
- spring-web.xml
.....
<!-- 開啟shiro注解支持 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
<property name="securityManager" ref="securityManager" />
</bean>