系统开发接入shiro权限后,项目启动,进入项目的某一个功能时报以下错误:(这个错误在业务日志里是看不到的,得去localhost.log里查看)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 [http-nio-8301-exec-5] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [AppServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class 2 HandlerMethod details: 3 Controller [com.sun.proxy.$Proxy44] 4 Method [public java.lang.String com.crm.web.controller.payplat.PayPlatController.payPlatList()] 5 Resolved arguments: 6 ] with root cause 7 java.lang.IllegalArgumentException: object is not an instance of declaring class 8 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 9 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 10 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 11 at java.lang.reflect.Method.invoke(Method.java:498) 12 at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:214) 13 at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132) 14 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104) 15 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:748) 16 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:689) 17 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83) 18 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945) 19 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876) 20 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:931) 21 at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822) 22 at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) 23 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807) 24 at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) 25 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 26 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 27 at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) 28 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 29 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 30 at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61) 31 at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) 32 at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) 33 at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) 34 at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) 35 at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) 36 at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) 37 at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) 38 at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) 39 at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) 40 at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) 41 at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) 42 at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344) 43 at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261) 44 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 45 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 46 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 47 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108) 48 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 49 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 50 at com.crm.web.filter.CrossDomainFilter.doFilter(CrossDomainFilter.java:38) 51 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 52 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 53 at org.apache.logging.log4j.web.Log4jServletFilter.doFilter(Log4jServletFilter.java:67) 54 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 55 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 56 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) 57 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 58 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475) 59 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 60 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80) 61 at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651) 62 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 63 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) 64 at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498) 65 at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 66 at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:796) 67 at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1374) 68 at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 69 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 70 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 71 at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 72 at java.lang.Thread.run(Thread.java:748)
可是接入方式和配置方式和以前的一模一样,以前系统跑的好好的,看到这个错误一脸懵逼。
于是找度娘帮忙,可搜出来的都只是说反射出错了,没有实际可用的解决方案。还是得靠自己,开始一句一句跟代码。发现在用到操作权限的地方没有调用shiro的doGetAuthorizationInfo方法,
这很不正常啊,然后度娘了一下。找到如下方案:
1、在shiro.xml配置文件里添加这个配置
<aop:config proxy-target-class="true"></aop:config>
经过实践,此方法不管用
2、在shiro.xml配置文件里的这个bean下定义proxyTargetClass此属性的值
<!-- AOP式方法级权限检查 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor">
<!--指定使用动态代理机制true表示使用CGLib代理-->
<property name="proxyTargetClass" value="true"/>
</bean>
启动项目,发现居然不报错了,一切正常了。
费了大半天功夫,终于不报错,项目正常了,非常欣喜。可是对于为什么添加这个属性就不报错了还是一脸懵逼。于是搜了一下这个属性。说是AOP式方法级权限检查,突然想到点什么:这个项目和其他项目不同的是在这个项目里添加了一个记录系统日志的aop,这个aop的切点是@annotation(org.springframework.web.bind.annotation.RequestMapping),报错的那个方法既有这个注解也有shiro操作权限的@RequiresPermissions注解。通过实践也证明了我的猜想。RequestMapping不是一个实现类,所以需要用CGLIB动态代理。
如果要被代理的对象是个实现类,那么Spring会使用JDK动态代理来完成操作(Spirng默认采用JDK动态代理实现机制)
如果要被代理的对象不是个实现类,那么,Spring需要使用CGLib来实现动态代理。