這幾天因為項目需要,學習了下shiro,由此留下一些記錄,也希望對初學shiro的朋友有幫助。
springboot 是這兩年新興起來的一個項目,它的出現是為了減少springmvc開發過程中需要引入各種的jar包,各種xml配置文件,它充分利用了JavaConfig的配置模式以及“約定優於配置”的理念,幫開發者配置大部分需要的東西,在github上的springboot項目里面,提供了很多列子,
而apache shiro 是一個輕量級的身份驗證與授權框架,與spring security 相比較,簡單易用,靈活性高,springboot本身是提供了對security的支持,畢竟是自家的東西。springboot暫時沒有集成shiro,這得自己配。
網上找了一些資料,配置shiro的,有很多需要在web.xml、application.xml里面各種配置,然而springboot 並沒有這些,而且springboot提倡無xml,使用javaconfig的配置方式,對這個不是很熟悉,但有人使用javaconfig的方式配置了shiro,參見這位csdn里面一位同學的博客spring boot 集成shiro的配置,下載了demo,然后模仿着成功配置了下。但習慣了xml的配置方式,感覺javaconfig的方式並不是很直觀,於是自己又把它換成了xml的方式。以下是主要的配置過程
首先是spring-shiro.xml
<?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.xml文件。
ehcache是一個純Java的進程內緩存框架,相關介紹可以看這里
<ehcache updateCheck="false" name="shiroCache">
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
</ehcache>
springboot加載xml配置文件
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的方法上面使用注解的的方式,就可以進行權限控制。
這里沒有提供MonitorRealm類,里面要實現doGetAuthorizationInfo(授權)和doGetAuthenticationInfo(認證)兩個方法,還有就是loginController里面要做一些改動,有需要的朋友可以參考這篇SpringMVC整合Shiro博文。
