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>

输入图片说明

相关文章
相关标签/搜索