求教Spring-Shiro导致@Async注解失效的问题

一休00173 2019-01-26 01:43:29
公司要求做文件上传、解析、比对的功能。由于Excel文件内容较多,采用@Async实现异步解析比对。
未使用Shiro前,异步方法正常执行。但是配置了Shiro之后,异步方法失效,变为同步执行,求教大佬这是什么原因。

注:
调用异步方法的类 与 异步方法所在类 不同。
Spring配置文件不存在包的重复扫描。
配置 Shiro /**=anon 可以按预期正常运行,配置Shiro /**=authc 会导致@Async注解失效。
...全文
215 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
一休00173 2019-01-26
  • 打赏
  • 举报
回复
引用 1 楼 亲爱的Joe 的回复:
重点看下是不是因为线程问题吧。 看你描述,你的异步上传接口在配置了需要权限验证的时候会失效。 shiro权限验证实在主线程内执行的,到了子线程内就无法正确执行了。 你可以试试下面这个: SecurityUtils.getSubject().execute(Callable callable); 把你上传操作放到Callable中去处理,应该会解决你的问题。

@Async
@Requestmapping
public void  upload(File file){
  Callable uploadCallable=new Callable(){
     @Override
     public void run(){
          service.uploadFile(file);
     }
   
  }
SecurityUtils.getSubject().execute(uploadCallable);
}
谢谢大佬,问题已解决,解决方案如下: 将spring-dao.xml中的 Async注解启用移动到spring-shiro.xml即可,暂不清楚原理。 配置文件 spring-dao.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:websocket="http://www.springframework.org/schema/websocket"
	xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd 
		http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

	<!-- 注解扫描 -->
	<context:component-scan base-package="xx.xxx" />

	<!-- 启用Async注解 -->
<!-- 	<task:annotation-driven executor="annotationExecutor" /> -->
	<!-- 支持 @Async 注解 -->
<!-- 	<task:executor id="annotationExecutor" pool-size="20" /> -->

        <!-- 部分配置省略 -->
</beans>
配置文件: spring-shiro.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:task="http://www.springframework.org/schema/task"
	xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">

	<!-- 启用Async注解 -->
	<task:annotation-driven executor="annotationExecutor" />
	<!-- 支持 @Async 注解 -->
	<task:executor id="annotationExecutor" pool-size="20" />

        <!-- 部分配置省略 -->

	<!-- 配置shiro过滤器 1. bean名称必须和web.xml中fileter名称一致 2. 过滤所有用户的请求 -->
	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<property name="securityManager" ref="securityManager" />
		<!-- 过滤链的定义 -->
		<property name="filterChainDefinitions">
			<value>

				<!-- 认证后可访问页面 -->
				/** = authc
			</value>
		</property>
	</bean>

</beans>
一休00173 2019-01-26
  • 打赏
  • 举报
回复
引用 楼主 一休00173 的回复:
公司要求做文件上传、解析、比对的功能。由于Excel文件内容较多,采用@Async实现异步解析比对。 未使用Shiro前,异步方法正常执行。但是配置了Shiro之后,异步方法失效,变为同步执行,求教大佬这是什么原因。 注: 调用异步方法的类 与 异步方法所在类 不同。 Spring配置文件不存在包的重复扫描。 配置 Shiro /**=anon 可以按预期正常运行,配置Shiro /**=authc 会导致@Async注解失效。
谢谢大佬,问题解决了,但是暂不清楚是什么原因。
亲爱的Joe 2019-01-26
  • 打赏
  • 举报
回复
重点看下是不是因为线程问题吧。

看你描述,你的异步上传接口在配置了需要权限验证的时候会失效。
shiro权限验证实在主线程内执行的,到了子线程内就无法正确执行了。
你可以试试下面这个:
SecurityUtils.getSubject().execute(Callable callable);

把你上传操作放到Callable中去处理,应该会解决你的问题。


@Async
@Requestmapping
public void upload(File file){
Callable uploadCallable=new Callable(){
@Override
public void run(){
service.uploadFile(file);
}

}
SecurityUtils.getSubject().execute(uploadCallable);
}

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧