web.xml 关键配置, 可以确定的是配置顺序没有问题
<!-- 安全过滤 --> <filter> <filter-name>shiroFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <async-supported>true</async-supported> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>*.do</url-pattern> <url-pattern>*.json</url-pattern> <url-pattern>*.xhtml</url-pattern> </filter-mapping> <!-- SpringMVC功能 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/META-INF/spring/appconf-*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> <url-pattern>*.json</url-pattern> <url-pattern>*.xhtml</url-pattern> </servlet-mapping>
控制器代码1, 像这样加入异步处理,正常响应,但是后台报错
/** * 分页查询 * * @param queryVo * @return Pagination<DynamicformVo> */ @RequestMapping("queryPage") @ResponseBody public Callable<Pagination<DynamicformVo>> getGrid(@ModelAttribute("DynamicformQueryVo") final DynamicformQueryVo queryVo) { return new Callable<Pagination<DynamicformVo>>() { @Override public Pagination<DynamicformVo> call() throws Exception { return dynamicformService.queryForPage(queryVo); } }; }
控制器代码2, 像这样写,正常处理没有报错,可惜不支持异步
/** * 分页查询 * * @param queryVo * @return Pagination<DynamicformVo> */ @RequestMapping("queryPage") @ResponseBody public Pagination<DynamicformVo> getGrid(@ModelAttribute("DynamicformQueryVo") final DynamicformQueryVo queryVo) { return dynamicformService.queryForPage(queryVo); }
报错如下:
org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code, either bound to the org.apache.shiro.util.ThreadContext or as a vm static singleton. This is an invalid application configuration. at org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) at org.apache.shiro.subject.Subject$Builder.<init>(Subject.java:627) at org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56) at org.apache.shiro.web.servlet.ShiroHttpServletRequest.getSubject(ShiroHttpServletRequest.java:88) at org.apache.shiro.web.servlet.ShiroHttpServletRequest.getSubjectPrincipal(ShiroHttpServletRequest.java:93) at org.apache.shiro.web.servlet.ShiroHttpServletRequest.getUserPrincipal(ShiroHttpServletRequest.java:111) at org.springframework.web.servlet.FrameworkServlet.getUsernameForRequest(FrameworkServlet.java:1079) at org.springframework.web.servlet.FrameworkServlet.publishRequestHandledEvent(FrameworkServlet.java:1065) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:996) at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863) at javax.servlet.http.HttpServlet.service(HttpServlet.java:646) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) at javax.servlet.http.HttpServlet.service(HttpServlet.java:727) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:748) at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:659) at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:625) at org.apache.catalina.core.AsyncContextImpl$1.run(AsyncContextImpl.java:237) at org.apache.catalina.core.AsyncContextImpl.doInternalDispatch(AsyncContextImpl.java:380) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:215) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:299) at org.apache.coyote.http11.AbstractHttp11Processor.asyncDispatch(AbstractHttp11Processor.java:1632) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:599) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619)
如上所述,Shiro 在处理Servlet3异步请求时会出现异常?要怎么处理呢?可以通过log4j配置把该错误屏蔽掉,但这不是我想要的效果!
也遇到这个问题,还没查到什么资料能解决
在mapping中增加dispatcher项
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
楼上回答的正确,加上就不报错了,受教了
SpringBoot的配置
https://blog.csdn.net/ucicno000/article/details/105065103
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。