這幾天由於項目須要,學習了下shiro,由此留下一些記錄,也但願對初學shiro的朋友有幫助。css
springboot 是這兩年新興起來的一個項目,它的出現是爲了減小springmvc開發過程當中須要引入各類的jar包,各類xml配置文件,它充分利用了JavaConfig的配置模式以及「約定優於配置」的理念,幫開發者配置大部分須要的東西,在github上的springboot項目裏面,提供了不少列子,html
而apache shiro 是一個輕量級的身份驗證與受權框架,與spring security 相比較,簡單易用,靈活性高,springboot自己是提供了對security的支持,畢竟是自家的東西。springboot暫時沒有集成shiro,這得本身配。java
網上找了一些資料,配置shiro的,有不少須要在web.xml、application.xml裏面各類配置,然而springboot 並無這些,並且springboot提倡無xml,使用javaconfig的配置方式,對這個不是很熟悉,但有人使用javaconfig的方式配置了shiro,參見這位csdn裏面一位同窗的博客spring boot 集成shiro的配置,下載了demo,而後模仿着成功配置了下。但習慣了xml的配置方式,感受javaconfig的方式並非很直觀,因而本身又把它換成了xml的方式。如下是主要的配置過程git
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- ========================================================= Shiro Components ========================================================= --> <!-- 緩存管理器 使用Ehcache實現 --> <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:app/config/ehcache-shiro.xml" /> </bean> <!-- 虛須要本身寫的realm實現類 充當shiro和應用的安全數據的橋樑 --> <bean id="MonitorRealm" class="com.test.MonitorRealm"></bean> <!-- 安全管理器 --> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realms"> <list> <ref bean="MonitorRealm" /> </list> </property> <property name="cacheManager" ref="cacheManager" /> </bean> <!-- Shiro生命週期處理器 --> <!-- 官方對其的解釋是 http://shiro.apache.org/static/1.2.1/apidocs/org/apache/shiro/spring/LifecycleBeanPostProcessor.html This post processor makes it easier to configure Shiro beans in Spring, since the user never has to worry about whether or not if they have to specify init-method and destroy-method bean attributes. 大意是使shiro bena 注入更加方便 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <!-- Shiro的Web過濾器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <property name="securityManager" ref="securityManager" /> <property name="loginUrl" value="/user-web/login" /> <property name="unauthorizedUrl" value="/unauthorized " /> <property name="filters"> <util:map> <entry key="authc"> <!-- 身份驗證攔截器,默認爲FormAuthenticationFilter,但 PassThruAuthenticationFilter功能相對強大,詳情見 https://shiro.apache.org/static/1.2.1/apidocs/org/apache/shiro/web/filter/authc/PassThruAuthenticationFilter.html--> <bean class="org.apache.shiro.web.filter.authc.PassThruAuthenticationFilter" /> </entry> </util:map> </property> <!-- shiro的強大的攔截器鏈,能夠匹配所有的url,並根據配置進行攔截--> <property name="filterChainDefinitions"> <value> # 無需認證即可以訪問的的文件放在前面 /js/* = anon /css/* = anon /img/* = anon /images/* = anon /user-web/login = anon /logout = logout /user-web/* = authc /backend-web/* = authc </value> </property> </bean> <!-- 開啓Shiro的註解(如@RequiresRoles,@RequiresPermissions),需藉助SpringAOP掃描使用Shiro註解的類,並在必要時進行安全邏輯驗證 --> <!-- 這裏要配置如下兩個bean,在這以前要配置好lifecycleBeanPostProcessor--> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <!-- 加上下面這一句是爲了解決If the controller requires proxying (e.g. due to @Transactional), please use class-based proxying 的報錯--> <!-- 參考http://www.cnblogs.com/digdeep/p/4624998.html 會發現上面錯誤是 Spring AOP 不一樣配置方式產生的衝突問題 --> <property name="proxyTargetClass" value="true"/> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean> <!-- 異常攔截 --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="org.apache.shiro.authz.UnauthorizedException"> /unauthorized <!-- 未受權處理頁 --> </prop> <prop key="org.apache.shiro.authz.UnauthenticatedException"> /user-web/login <!-- 身份沒有驗證 --> </prop> </props> </property> </bean> </beans>
ehcache是一個純Java的進程內緩存框架,相關介紹能夠看這裏github
<ehcache updateCheck="false" name="shiroCache"> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> </ehcache>
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @SpringBootApplication @ComponentScan @EnableAutoConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(new String[] { "classpath*:app/config/spring-*.xml", "classpath*:app/config/spring-session-redis.xml", "classpath*:/user/captcha.xml" //.... }, args); } }
這樣。spingboot以xml形式配置shiro就完成了,後面在controller的方法上面使用註解的的方式,就能夠進行權限控制。web
這裏沒有提供MonitorRealm類,裏面要實現doGetAuthorizationInfo(受權)和doGetAuthenticationInfo(認證)兩個方法,還有就是loginController裏面要作一些改動,有須要的朋友能夠參考這篇SpringMVC整合Shiro博文。redis