【spring系列】- Springmvc整合apache shiro安全框架
shiro:
shiro是apache開源的安全框架,可以方便用來處理使用者資訊認證、使用者授權控制、資訊加密等功能,因為其小巧而功能強大在很多SSM架構的web應用系統中被廣泛採用,shiro的架構設計個人覺得結構很清晰,對於想向更高層次發展的開發人員來說,shiro原始碼是非常好的學習材料,下面介紹springmvc整合shiro的步驟:
①:為servlet容器提供訪問的入口(web.xml):DelegatingFilterProxy
<filter> <filter-name>shiroFilter</filter-name> <filter-class> org.springframework.web.filter.DelegatingFilterProxy </filter-class> <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>
工作原理:在web.xml檔案中配置DelegatingFilterProxy的Servlet物件,其代理的目標Serlvet其實是org.apache.shiro.spring.web.ShiroFilterFactoryBean,為了建立兩個DelegatingFilterProxy和ShiroFilterFactoryBean的關係,主要是通過Filter名稱進行繫結的,當客戶端傳送請求時,DelegatingFilterProxy進行攔截,然後根據獲取Filter的名稱,最後呼叫IOC容器的getBean的方法獲取和Filter名稱匹配的Bean,因為ShiroFilterFactoryBean是FactoryBean因為實際上處理客戶端請求的是shiro在IOC容器的Filter的實現類
②:配置shiro的ehcache快取管理器:快取認證等資訊:(可選)
<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
<!--Ehcache快取配置檔案為空則使用預設配置-->
<property name="cacheManagerConfigFile" value="classpath:ehcache-shiro.xml"/>
</bean>
③:配置shiro的會話管理器(可選:預設ServletContainerSessionManager)
<bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="globalSessionTimeout" value="600000"/> <!-- session 有效時間為半小時 (毫秒單位)-->
<property name="sessionListeners">
<list>
<bean class="com.zhiwei.shiro.listener.sessionLisenter"/>
</list>
</property>
</bean>
④:配置shiro的realm物件:提供shiro認證/授權資料的資料域物件
<bean id="myRealm" class="com.zhiwei.shiro.realm.MyRealm" init-method="setCredentialMatcher"/>
⑤:配置shiro的安全管理器
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="cacheManager" ref="cacheManager"/>
<property name="sessionManager" ref="shiroSessionManager"/>
<property name="realm" ref="myRealm"/>
<!-- AuthenticationListener在認證器裡面維護 -->
<property name="authenticator.authenticationListeners">
<set>
<bean class="com.zhiwei.shiro.listener.DefineAuthenticationListener"/>
</set>
</property>
</bean>
⑥:配置shiro的ShiroFilterFactoryBean(真正處理客戶端請求的類)
securityManager:安全管理器
loginUrl:登陸頁面
successUrl:登陸成功頁面
unauthorizedUrl:授權失敗跳轉的頁面
filterChainDefinitions:過濾器鏈
意:注意過濾器的工作順序:shiro採取第1次匹配優先,第一次匹配後後面的過濾器鏈不會匹配,順序不當可能出現"302 not found"錯誤
常用過濾器通俗解釋:(過濾路徑支援ant風格)
logout :退出登陸過濾器:預設跳轉專案根路徑訪問地址(可自定義)
anon:匿名過濾器:不需要認證就可以進行訪問:例如公司的首頁
authc:認證過濾器:客戶端只有認證通過之後才能訪問(例如個人資訊)
roles:許可權過濾器:客戶端訪問制定路徑,只有滿足指定的角色才能訪問
千萬注意:名稱必須與web.xml配置的Filter名稱一致,否則IOC找不到對應的Bean出現異常
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
<property name="securityManager" ref="securityManager"/>
<property name="loginUrl" value="/toLoginPage"/>
<property name="successUrl" value="/toSuccessfulPage"/>
<property name="unauthorizedUrl" value="/toUnauthorizedPage"/>
<property name="filterChainDefinitions">
<value>
/shiroHandler/shiro-logout = logout
/shiroHandler/shiro-login = anon
/toUserPage = authc,roles[user]
/toAdminPage = authc,roles[admin]
/** = authc
</value>
</property>
</bean>
⑦:配置shiro元件在具備IOC Bean的生命週期活動
(傳統的Filter的生命週期是由servlet容器進行統一管理,因為DelegatingFilterProxy的緣故,實際處理客戶端請求是IOC容器裡面的Filter,因此shiro的實際處理客戶端請求的過濾器需要屬於IOC的Bean元件,因此最好將其生命週期活動交給IOC容器同一管理)
<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
補充:
配置檔案中介紹了AuthenticationListener/SessionListener的配置
AuthenticationListener:認證監聽器:會負責監聽整個shiro認證過程的情況,對於一些專案想統計使用者登陸的請求,可以使用該介面,該介面在AuthenticatingSecurityManager的Authenticator中的authenticationListeners維護,DefaultWebSecurityManager因為繼承了AuthenticatingSecurityManager:使用級聯屬性配置authenticator.authenticationListeners即可:
AuthenticationListener介面:
public interface AuthenticationListener {
void onSuccess(AuthenticationToken token, AuthenticationInfo info);
void onLogout(PrincipalCollection principals);
}
spring配置AuthenticationListener:authenticator.authenticationListeners
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<property name="authenticator.authenticationListeners">
<set>
<bean class="com.zhiwei.shiro.listener.DefineAuthenticationListener"/>
</set>
</property>
</bean>
SessionListener:會話監聽器:顧名思義就是監聽整個會話的操作過程在SessionManager中的sessionListeners維護:
SessionListener 介面:
public interface SessionListener {
void onStart(Session session);
void onStop(Session session);
void onExpiration(Session session);
}
spring配置SessionListener :sessionListeners
<bean id="shiroSessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
<property name="sessionListeners">
<list>
<bean class="com.zhiwei.shiro.listener.sessionLisenter"/>
</list>
</property>
</bean>
相關推薦
【spring系列】- Springmvc整合apache shiro安全框架
shiro: shiro是apache開源的安全框架,可以方便用來處理使用者資訊認證、使用者授權控制、資訊加密等功能,因為其小巧而功能強大在很多SSM架構的web應用系統中被廣泛採用,shiro的架構設計個人覺得結構很清晰,對於想向更高層次發展的開發人員來說,
【spring系列】- springmvc國際化的工作原理
國際化:頁面響應式佈局一個頁面針對不同的裝置進行相應的顯示,國際化則是也個頁面針對不同的地區/語言進行相應的自適應的顯示 SpringMVC支援的國際化的三個類: ResourceBundleMessageSource: 國際化訊息資源的封裝類,主要配置不同地
【原創】基於Spring-SpringMVC-Mybatis 的 Shiro 安全框架使用教程--轉載請註明出處
Shiro使用說明文件 宣告: 我們所使用的框架為SSM框架+Shiro許可權控制框架,在以下部分中,將會描述如何使用一個Shiro框架。 框架使用概述: Shiro安全框架為我們提供了一個較為完善的許可權管理系統。我們將使用該框架提供的使用者登入,登出,使用者許可權
【Spring 系列】1. 搭建和配置Spring與jdbc整合的環境
配置資料來源 <!-- 配置結點,可以使用佔位符 --> <context:property-placeholder location=“classpath:jdbc.properties”/> <bean id="dataSource" class="org.apach
【spring系列】之6:bean的初始化和銷燬方法
bean的生命週期:bean建立---初始化----銷燬的過程 容器管理bean的生命週期:我們可以自定義初始化和銷燬方法;容器在bean進行到當前生命週期的時候來呼叫我們自定義的初始化和銷燬方法 構造(物件建立): 單例項:在容器啟動的時候建立物件 多例項:在每次獲
【SpringSecurity系列】SpringBoot整合SpringSecurity新增驗證碼登入
上一篇博文已經介紹過了SpringSecurity的表單登入,這裡我們基於上一篇的基礎上,新增一個驗證碼進行登入,登入頁面效果圖,如圖所示: 首先我們需要建立驗證碼的生成規則,首先建立一個驗證碼的實體: public class ImageCode { /** 驗證碼 */
【SpringSecurity系列】SpringBoot整合SpringSecurity的表單登入
在如今的專案開發中,登入和許可權管理是一個專案最基本的需求,對於這個需求SpringSecurity一個很好的架構,這一系列博文是我自己學習SpringSecurity的一些總結和自己踩過的坑和從零開始搭接一個完整的基於SpringSecurity的登入(包括第三方的登入)和許可權管理的專案。
【spring系列】之8:屬性賦值
一:使用@Value賦值 基本數值 可以寫SpEL; #{} 可以寫${};取出配置檔案【properties】中的值(在執行環境變數裡面的值) demo:set,get省略 @Value("張三") private String na
【spring系列】之14:spring宣告式事務實現原理剖析
通過上一節事務環境搭建,我們知道,在搭建的5個步驟中,有兩個是spring為我們提供底層去稍作配置,然後使用的, 這兩個操作涉及的便是: @EnableTransactionManagement PlatformTransactionManager 其中,Platfor
【spring系列】之7:生命週期-BeanPostProcessor原理以及在spring底層中的運用
本章節重點說一下我們自定義的BeanPostProcessor的執行機制和呼叫棧。 事前準備: 1.自定義beanPostProcessor 2.註冊自定義的beanPostProcessor到容器中 1.自定義beanPostProcessor @Componen
【Spring 系列】一、如何從spring中獲取bean
導包 spring要管理的類資訊 package com.liuyong666.service.impl; import com.liuyong666.service.PersonService; public class PersonServiceBean implements Person
【spring Mvc】SpringMVC 檔案上傳配置,多檔案上傳,使用的MultipartFile
基本的SpringMVC的搭建在我的上一篇文章裡已經寫過了,這篇文章主要說明一下如何使用SpringMVC進行表單上的檔案上傳以及多個檔案同時上傳的步驟 SpringMVC 基礎教程 框架分析:ht
【spring系列】- IOC容器Bean生命週期
Spring IOCBean的生命週期: bean物件例項化—>屬性注入—>beanfactory—>ApplicationContext---->postProcessBeforeInitialization—> Initi
【Spring系列】——Spring Framework簡介(二)
目錄 Core Container Core Container由spring-core,spring-beans,spring-context,spring-context-support,and spring-expression這些模組組成。
【spring系列】- Spring的FactoryBean的基本認識
FactoryBean:工廠Bean IOC容器內部一種特殊的Bean,主要作用就是管理內部的Bean例項物件(單例/原型物件),扮演類似工廠的角色 特點: 通過Bean名稱只能獲取其生產的物件,
【spring 國際化】springMVC、springboot國際化處理詳解
在web開發中我們常常會遇到國際化語言處理問題,那麼如何來做到國際化呢? 你能get的知識點? 使用springgmvc與thymeleaf進行國際化處理。 使用springgmvc與jsp進行國際化處理。 使用springboot與thymeleaf進行國際化處理。 目錄你能get的知識點?你必須要
在Spring MVC中使用Apache Shiro安全框架
修改 ctype var format href 文件的 來看 one name 我們在這裏將對一個集成了Spring MVC+Hibernate+Apache Shiro的項目進行了一個簡單說明。這個項目將展示如何在Spring MVC 中使用Apache Shiro來構
【Shiro】- springmvc 整合 shiro
浪費了“黃金五年”的Java程式設計師,還有救嗎? >>>
【Spring Boot】(23)、Spring Boot整合Mybatis
首先新增mybatis依賴: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</
【Spring Boot】(30)、SpringBoot整合RabbitMQ
1、安裝 1.1、Erlang: Erlang下載地址,下載後安裝即可。 1.2、RabbitMQ安裝 RabbitMQ下載地址,下載後安裝即可。 注意:Erlang的版本要與RabbitMQ版本需要匹配才行。 RabbitMQ Mini