shiro受權的註解和jsp控制

1 在applicationContext-shiro.xml中配置過濾器web

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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">

<!-- 1  配置filter對應的bean -->
 		<!-- shiro的web過濾器 -->
		<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
			<!-- 1.1 配置安全管理器 -->
			<property name="securityManager" ref="securityManager"/>
			<!-- 1.2 loginUrl認證提交地址,若是沒有認證將會請求此地址進行認證,請求此地址將由formAuthenticationFilter進行表單認證-->
			<property name="loginUrl" value="/login.action" />
			<!-- 1.3  unauthorizedUrl指定沒有權限時跳轉頁面-->
			<property name="unauthorizedUrl" value="/refuse.action" />
			
			<!-- 1.5  配置成功頁面 -->
			<property name="successUrl" value="/first.action"/>
			<!-- 1.4  過濾器鏈的定義 -->
			<property name="filterChainDefinitions">
				<value>
					<!-- 對靜態資源進行匿名訪問 -->
					/images/**=anon
					<!-- 請求logout.action地址,shiro去清空session -->
					/logout.action=logout
					<!-- /**=authc 表示全部url都必須認證經過以後開能夠訪問 -->
					
					<!-- 受權的控制  下面經過註解的方式開啓受權-->
					<!-- /items/query.action=perms[items:query]
					/user/query.action=perms[user:query] -->
					
					<!-- 對全部剩下的認證 -->
					/**=authc
					<!-- /**=anon anon全部的url均可以匿名訪問 -->
					<!-- /**=anon -->
				</value>
			</property>
		</bean>
<!-- 2  配置安全管理器  securityManager-->
		<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
			<property name="realm" ref="customRealm"></property>
			
		</bean>
		

<!-- 3  配置realm -->
		<bean id="customRealm" class="com.shi.shiro.CustomRealm">
			<property name="credentialsMatcher" ref="credentialsMatcher"></property>
		</bean>

<!-- 4 配置憑證匹配器 -->
		<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
			<property name="hashAlgorithmName" value="md5"/>
			<property name="hashIterations" value="1"/>
		</bean>
		
</beans>

2 在springmvc.xml文件中配置aopspring

<?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:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.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">
	
	<!-- 配置掃描器 -->
	<context:component-scan base-package="com.shi.controller" >
        <!-- use-default-filters="false"
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/> -->
    </context:component-scan>
    
    <!-- 配置springmvc的映射器和適配器 -->
    <mvc:annotation-driven></mvc:annotation-driven>

	  <!-- 配置映射器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- <property name="prefix" value="/WEB-INF/"></property> -->
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 配置咱們的攔截器 -->
	<!-- <mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.shi.interceptor.LoginInterceptor"></bean>
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/**"/>
			<bean class="com.shi.interceptor.CheckInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors> -->
	
	<!-- 5 開啓aop,對類代理  這是spring的aop方式-->
		<aop:config proxy-target-class="true"></aop:config>
	
	<!-- 6  開啓shiro註解支持 -->	
		<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
			<property name="securityManager" ref="securityManager"></property>
		</bean>
	
</beans>

3 Controller層的代碼express

/**
	 * /items/query.action 查詢商品的action
	 * 執行queryItems方法須要(items:query)權限  是基於aop代理的方式實現的
	 */
	@RequestMapping("/items/query.action")
	@RequiresPermissions("items:query")
	public ModelAndView queryItems()throws Exception{
		ModelAndView mv =new ModelAndView();
		
		mv.setViewName("queryItems");
		return mv;
	}

4 jsp的註解支持apache

輸入圖片說明

<%@ taglib uri="http://shiro.apache.org/tags" prefix="shiro" %>
        <shiro:hasPermission name="items:query">
			用戶具備查詢的權限2
	</shiro:hasPermission>
	<shiro:hasPermission name="items:query">
			用戶具備查詢的權限3
	</shiro:hasPermission>

輸入圖片說明

相關文章
相關標籤/搜索