开发者社区> 问答> 正文

Shiro 异步报错?报错

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配置把该错误屏蔽掉,但这不是我想要的效果!

展开
收起
爱吃鱼的程序员 2020-06-22 10:53:34 693 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    也遇到这个问题,还没查到什么资料能解决

    在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

    2020-06-22 10:53:51
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载