shiro之web配置

shiro之web配置

1、簡介

        經過配置來學習shiro,或者說經過配置來學習框架和軟件,能夠幫咱們記憶這個框架或軟件的核心功能有哪些,而後是經過什麼模塊來實現這些功能。本篇,咱們學習shiro的web配置,一樣包括了兩個部分:權限(權限鏈)、模塊(session)html

2、權限配置

    a)權限鏈(能夠看做是serlet中的filter):在shiro的web模塊中,都是經過 URI來實現權限驗證。經過servlet的listener,在serlvet啓動的時候,將咱們在配置文件中設置的權限鏈(/index=authc,ssl)加載進SecurityManager中。客戶經過URI(/index)來訪問server端的資源(通常是接口)時,經過字符串匹配來選取權限(authc,ssl)。而後再一個一個驗證,經過的就能夠訪問接口。
java

1.web.xml中
<!--用於加栽攔截鏈--->
<listener>
    <listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class></listener>
<!-- 加入shiro的filter -->
<filter>
    <filter-name>ShiroFilter</filter-name>
    <filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>

2.ini中的配置
#########一個攔截鏈 /URI=權限 #############
[urls]
/index.html = anon
/user/create = anon
/user/** = authc
/admin/** = authc, roles[administrator]
/rest/** = authc, rest/remoting/rpc/** = authc, perms["remote:invoke"]


(
ps:因爲shiro採用的是String.startwith(URI)方式來判斷,並且是按攔截連的配置順序來查找權限攔截鏈。因此,須要將合適的攔截鏈放在前面。
eg:
[urls]
/index = anon
/indexss = authc
若是用戶訪問:/indexss,shiro會先匹配/index在前面,第二條攔截鏈就不會起做用.
)

    b)默認權限
web

Filter Name 功能
anno 不須要受權、登陸就能夠訪問。eg:/index
authc
 須要登陸受權才能訪問。eg:/用戶中心
authcBasic Basic HTTP身份驗證攔截器
logout 退出攔截器。退出成功後,會 redirect到設置的/URI
noSessionCreation 不建立會話鏈接器
perms 受權攔截器:perm['user:create']
port 端口攔截器.eg:port[80]
rest rest風格攔截器
roles 角色攔截器。eg:role[administrator]
ssl
ssl攔截器。經過https協議才能經過
user
用戶攔截器。eg:登陸後(authc),第二次沒登錄可是有記住我(remmbner)均可以訪問。

2、模塊配置

    a)session:和web中的session相似(不建議在servlet中使用shiro來管理session)。
spring

一、默認的3中session:
DefaultSessionManager:DefaultSecurityManager使用的默認實現,用於JavaSE環境;
DefaultWebSessionManager:用於Web環境的實現,能夠替代ServletContainerSessionManager,本身維護着會話,直接廢棄了Servlet容器的會話管理。(其它的容器使用的session,通常咱們就選這個)
(
  若是是servlet容器,則DefaultWebSessionManager將會使用這個:
    ServletContainerSessionManager:DefaultWebSecurityManager使用的默認實現,用於Web環境,其直接使用Servlet容器的會話。
)

2.配置:
[main]
#########默認的session#######################
sessionManager=org.apache.shiro.session.mgt.DefaultWebSessionManager
securityManager.sessionManager=$sessionManager
#########sessionId管理#######################
sessionIdCookie=org.apache.shiro.web.servlet.SimpleCookie
sessionManager=org.apache.shiro.web.session.mgt.DefaultWebSessionManager
sessionIdCookie.name=sid
sessionIdCookie.maxAge=1800
sessionIdCookie.httpOnly=true

sessionManager.sessionIdCookieEnabled=true
sessionManager.sessionIdCookie=$sessionIdCookie
#########session驗證(驗證過時)###############
#### 這裏默認使用的是quartz-1.6.8 須要最新的quartz就須要本身實現######

sessionValidationScheduler=org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler
sessionValidationScheduler.sessionValidationInterval = 3600000
sessionValidationScheduler.sessionManager=$sessionManager&nbsp;

sessionManager.globalSessionTimeout=1800000
sessionManager.sessionValidationSchedulerEnabled=true
sessionManager.sessionValidationScheduler=$sessionValidationScheduler

#########sessionDAO#########################

sessionDAO=org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
sessionDAO.activeSessionsCacheName=shiro-activeSessionCache
sessionManager.sessionDAO=$sessionDAO

######### 開啓cache#####################
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
securityManager.cacheManager = $cacheManager

    b)JSP:頁面標籤能夠幫助咱們給不一樣的權限用戶呈現不一樣的內容。使用方式和jsp的jstl很是相識:
apache

        官網的jsp教材:http://shiro.apache.org/web.html#Web-SessionManagement 安全

        一、在jsp中導入shiro
cookie

<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>

        二、標籤
session

<shiro:guest></shiro:guest>:遊客可查看
<shiro:user></shiro:user>:用戶已經身份驗證/記住我登陸後顯示相應的信息。第一次登陸,第二次若是在cookies中有用戶信息remmeber就能夠查看。
<shiro:authenticated></shiro:authenticated>:用戶已經身份驗證經過,每次都須要登陸
<shiro:notAuthenticated></shiro:notAuthenticated>:沒有登陸,可是上次登陸有點擊記住我。
<shiro:principal property="username"></shiro:principal>:根據登陸時的userName
<shiro:hasRole name="admin"></<shiro:hasRole>:根據角色斷定 
<shiro:hasAnyRoles name="admin,user"></shiro:hasAnyRoles>:多個角色
<shiro:hasPermission name="user:create"></shiro:hasPermission>:資源字符串

        三、註解:@requestRoles和@RequiresPermissions。能夠在接口上使用。app

3、spring整合:

<!--和spring整合就是把前面的Ini配置,用<bean></bean>來從新實現了一次 -->

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util 
       http://www.springframework.org/schema/util/spring-util.xsd">



	<!-- 安全管理器 securityManager start -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="userRealm" />
		<property name="cacheManager" ref="cacheManager" />
		<!-- <property name="sessionManager" ref="sessionManager" /> -->
	</bean>
	<!-- 安全管理器 securityManager end -->


	<!-- realm start -->
	<bean id="userRealm" class="com.javass.model.shiro.ShiroRealm">
		<property name="cachingEnabled" value="true" />
	</bean>
	<!-- realm end -->

	<!-- cache start -->
	<!-- <bean id="cacheManager" class="com.javass.model.shiro.cache.SpringCacheManagerWrapper"> 
		<property name="cacheManager" ref="springCacheManager" /> </bean> -->
	<bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
		<property name="cacheManagerConfigFile" value="classpath:ehcache.xml"></property>
	</bean>
	<!-- cache end -->

	<!-- session start -->
	<!-- 會話ID生成器 -->

	<!-- 會話管理器 -->
	<!-- 
	<bean id="sessionManager"
		class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
		<property name="globalSessionTimeout" value="1800000" />
		<property name="deleteInvalidSessions" value="true" />
		<property name="sessionValidationSchedulerEnabled" value="true" />
		<property name="sessionValidationScheduler" ref="sessionValidationScheduler" />
		<property name="sessionDAO" ref="sessionDAO" />
		<property name="sessionIdCookieEnabled" value="true" />
		<property name="sessionIdCookie" ref="sessionIdCookie" />
	</bean>
	 -->
	 
	<!-- 
	<bean id="sessionIdGenerator"
		class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator" />
	 -->
	 
	<!-- 會話Cookie模板 -->
	<!--  
	<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
		<constructor-arg value="sid" />
		<property name="httpOnly" value="true" />
		<property name="maxAge" value="-1" />
	</bean>
	-->
	
	<!-- 會話驗證調度器 -->
	<!-- 使用quartz來控制 -->
	<!--  
	<bean id="sessionValidationScheduler" class="com.javass.utils.QuartzSessionValidationScheduler">
		<property name="sessionValidationInterval" value="1800000" />
		<property name="sessionManager" ref="sessionManager" />
	</bean> -->
	<!-- 使用的默認的任務器 -->
	<!--
	<bean id="sessionValidationScheduler"
		class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler">
		<property name="interval" value="1800000" />
		<property name="sessionManager" ref="sessionManager" />
	</bean>
	-->

	<!-- session end -->
	<!-- Shiro生命週期處理器 -->
	<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />

	<!-- 自定義的登陸後不能訪問的filter -->
	<bean id="noactionfilter" class="com.javass.model.shiro.NoActionAutenticationFilter">
	</bean>

	<!-- 已經登陸的帳戶不能訪問 -->
	<!-- Shiro shiroFilter start -->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<property name="securityManager" ref="securityManager" />
		<property name="loginUrl" value="/login" />               <!--登陸的跳轉 URL -->
		<property name="successUrl" value="/user/center" />       <!--登陸成功的跳轉 URL -->
		<property name="unauthorizedUrl" value="/index" /> 		  <!--無權訪問的跳轉 URL -->
		<property name="filters">
			<util:map>
				<!--使用咱們自定義的登陸邏輯,爲了支持AJAX請求 -->
				<entry key="noaction" value-ref="noactionfilter" />
			</util:map>
		</property>
		<property name="filterChainDefinitions">
			<value>
				<!-- 權限控制
				 anon: 不須要權限就能訪問 
				 logout: 註銷
				 authc: 須要登陸後才能訪問,但不限制權限 authc, 
				 roles[Admin]: 	必須是 Admin 權限才能訪問 -->
				/index = anon
				/static/** = anon
				/register = anon
				/login = anon,noaction
				/admin/login = anon,noaction
				/logout = logout
				/WEB-INF/views/error.jsp=anon
				/user/**=authc
				/admin/**=authc,roles[admin]
			</value>
		</property>
	</bean>
	<!-- Shiro shiroFilter end -->


	<!-- 使用@requestRoles這種解析annotation start -->
	<bean
		class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"
		depends-on="lifecycleBeanPostProcessor" />
	<bean
		class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
		<property name="securityManager" ref="securityManager" />
	</bean>
	<!-- 使用@requestRoles這種解析annotation end -->
</beans>
相關文章
相關標籤/搜索