springmvc集成shiro注解权限

springmvc集成shiro注解权限

源代码下载:http://download.csdn.net/detail/u013147600/9066923

java.lang.ClassNotFoundException: org.aspectj.lang.annotation.Around错误解决方法http://blog.csdn.net/u013147600/article/details/48132947

配置aop错误:

org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 16 in XML document from class path resource [springmvc.xml] is invalid; nested exception is org.xml.sax.SAXParseException: The prefix "aop" for element "aop:aspectj-autoproxy" is not bound.

添加这些有关AOP的配置:

添加后如下面所示:
<beans xmlns=" http://www.springframework.org/schema/beans"
xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p=" http://www.springframework.org/schema/p"
xmlns:context=" http://www.springframework.org/schema/context"
xmlns:mvc=" http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd 
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
">

在springmvc.xml中的配置:

<aop:aspectj-autoproxy proxy-target-class="true"/> 

记得在shiro.xml中配置:

<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>

如果没有配置上面这两个bean的话,访问时就不会进行权限管理,(也就是配置的权限无效)。

controller控制层的方法:

@Controller
@RequestMapping("/admin")
public class AdminController {
 
 private UserService userService =new UserServiceImpl();
 
/* 
加上这个后这个方法只有当用户的角色为admin时才可以访问,不然会出现UnauthorizedException异常
如:严重: Servlet.service() for servlet [SpringMVC] in context with path [/authc] threw exception [Request processing failed; nested exception is org.apache.shiro.authz.UnauthorizedException: Subject does not have role [admin]] with root cause
org.apache.shiro.authz.AuthorizationException: Not authorized to invoke method: public java.lang.String com.authc.controller.AdminController.queryAllUserInfo(javax.servlet.http.HttpServletRequest)
*/
 @RequiresRoles("admin")
 @RequestMapping("/queryAllUserInfo")
 public String queryAllUserInfo(HttpServletRequest request)
 {
  List<User> userList = userService.queryAllUserInfo();
  request.setAttribute("userList", userList);
  return "/admin";
 }
}
注:Shiro权限注释和shiro.xml中权限的配置(形如:/member/queryMyUserInfo=authc)可以结合使用,但是不要产生冲突。


对异常的拦截:

配置成shiro权限注解后,下面的配置没有效果,就是当用户没有权限的时候不会运行"/member/login"路径,而是直接在页面显示出UnauthorizedException错误信息。
 <!-- 用户访问未对其授权的资源时,所显示的连接 -->  
  <property name="unauthorizedUrl" value="/member/login"></property>

解决方法:
在 springmvc中加入如下配置:
<!-- shiro为集成springMvc 拦截异常-->
 <bean
  class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
  <property name="exceptionMappings">
   <props>
    <!-- 这里你可以根据需要定义N多个错误异常转发 -->
    <prop key="org.apache.shiro.authz.UnauthorizedException">redirect:/member/login</prop>
    <prop key="org.apache.shiro.authz.UnauthenticatedException">redirect:/member/login</prop>
    <prop key="java.lang.IllegalArgumentException">/error.jsp</prop>  <!-- 参数错误(bizError.jsp) -->
    <prop key="java.lang.Exception">/error.jsp</prop>  <!-- 其他错误为'未定义错误'(unknowError.jsp) -->
   </props>
  </property>
 </bean>


  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值