第一步:在spring配置文件中开启shiro注解支持
<!-- 开启shiro框架注解支持 -->
<bean name="" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
<!-- 必须使用cglib方式为Action对象创建代理对象
如果这里设置为false的话,那么将会使用jdk进行代理,针对接口,但是接口中没有Action中的XX方法,所以会抛出
java.lang.NoSuchMethodException: com.sun.proxy.$proxyXX.XX(),所以使用jdk代理不好使
如果配置的是true的话就会使用cglib进行代理,是通过继承来实现代理的,子类继承的里面当然会有XX方法,所以要配置true -->
<property name="proxyTargetClass" value="true"/>
</bean>
<!-- 配置好shiro框架提供的切面类,用于创建代理对象 -->
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"/>
第二步:在Action的方法上使用shiro注解
/**
* 删除取派员
* @return
* @throws Exception
*/
@RequiresPermissions(value="staff-delete")
/*
* 如果没有staff-delete权限的话将无法执行此方法
* 并抛出org.apache.shiro.authz.UnauthorizedException,权限不足异常
*/
public String deleteBatch() throws Exception {
staffService.deleteBatch(ids);
return "staffList";// 跳回取派员显示列
}
/**
* 修改取派员--->其原理是通过id从数据库中取出取派员,然后将客户端传过来的参数进行赋值给从数据库中取出来的取派员进行覆盖。
* 为何不直接用客户端传过来的对象(参数封装到Bean里了)直接持久化到数据库?因为页面传过来的数据不是很完整,有的参数是空的。
* @return
* @throws Exception
*/
@RequiresPermissions(value="staff-edit")
public String editStaff() throws Exception {
// 根据id获得数据库中的对象
BcStaff staff = staffService.findStaffById(this.model.getId());
staff.setName(model.getName());
staff.setHaspda(model.getHaspda() == null ? "0" : "1");
staff.setId(model.getId());
staff.setStandard(model.getStandard());
staff.setStation(model.getStation());
staff.setTelephone(model.getTelephone());
staffService.editStaff(staff);
return "staffList";// 跳回取派员显示列
}
执行到相应的方法将会出现异常,顾名思义就是权限不足的意思
第三步:在struts.xml中配置全局异常捕获,当shiro框架抛出权限不足异常时,跳转到权限不足提示页面
<!-- 定义全局结果集,比如拦截器根本就不知道从哪个action中拦截到的页面 -->
<global-results>
<result name="login">/login.jsp</result>
<result name="unauthorized">/unauthorized.jsp</result>
</global-results>
<global-exception-mappings> <!-- 定义全局异常,如果捕捉到权限不足异常就跳转到全局结果集为unauthorized的result去 -->
<exception-mapping result="unauthorized" exception="org.apache.shiro.authz.UnauthorizedException"/>
</global-exception-mappings>
版权声明:本文为qq_36138324原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。