使用shiro的方法 注解方式,页面标签方式 权限控制

(一)注解方式

1.spring配置文件中开启shiro注解支持

        <!-- 开启shiro框架注解支持 -->
	<bean id="defaultAdvisorAutoProxyCreator" 
		class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
			<!-- 配置为true,必须使用cglib方式为Action对象创建代理对象 -->
		<property name="proxyTargetClass" value="true"/>
	</bean>
	
	<!-- 配置shiro框架提供的切面类,用于创建代理对象 -->
	<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"/>

2.在Action的方法上使用shiro注解

        @RequiresPermissions("staff-delete")//调用这个方法需要staff-delete权限
	public String deleteBatch(){
		staffService.deleteBatch(ids);
		return LIST;
	}

3.配置struts.xml,当权限不足时,跳转

                <!-- 全局结果集定义 -->
		<global-results>
			<result name="unauthorized">/unauthorized.jsp</result>
		</global-results>
		
		<global-exception-mappings>
			<exception-mapping result="unauthorized" exception="org.apache.shiro.authz.UnauthorizedException"></exception-mapping>
		</global-exception-mappings>

(二)页面标签方式

1.在jsp页面中引入shiro的标签库

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

2.使用shiro的标签控制页面元素展示

例子:当前user有 staff-list 权限的时候会展示 button,反之就没有

<shiro:hasPermission name="staff-list">
	<input type="button" value="submit">
</shiro:hasPermission>

 

猜你喜欢

转载自blog.csdn.net/qq_41566772/article/details/88085915