如何通过subject.associateWith(work)将Spring延迟结果产生的线程与Apache Shiro Subject相关联;
对于可调用或可运行的内容很明显,但对于异步Spring对象则不然。
@RequestMapping(value = "etc/async/{atbId}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public DeferredResult<ResponseEntity<String>> getEtcById(HttpServletResponse response,
@PathVariable("atbId") long atbId,
HttpSession session) throws IOException {
log.debug("Enter async getEtcById with ListenableFuture");
final String userName = (String) session.getAttribute(SessionKeys.USER_ID);
final AuthenticatedAsyncRestTemplate restTemplate = new AuthenticatedAsyncRestTemplate();
final DeferredResult<ResponseEntity<String>> result = new DeferredResult<>(100000);
ListenableFuture<ResponseEntity<String>> future =
restTemplate.getForEntity("http://localhost:8079/ts.download/download/etc/{atbId}?userName={userName}", String.class, atbId, userName);
future.addCallback(
new ListenableFutureCallback<ResponseEntity<String>>() {
@Override
public void onSuccess(ResponseEntity<String> response) {
// Will be called in HttpClient thread
log.debug("ListenableFuture onSuccess: " + response.getBody());
result.setResult(response);
}
@Override
public void onFailure(Throwable t) {
log.error("ListenableFuture onFailure", t);
result.setErrorResult(t.getMessage());
}
});
log.debug("Leaving async getEtcById");
return result;
}
其余的调用有效,但我收到此错误消息:
用于servlet [spring]的Servlet.service()与path的上下文 [/ mycontext]抛出异常[java.lang.RuntimeException: org.apache.shiro.UnavailableSecurityManagerException:没有 SecurityManager可以访问调用代码,或者绑定到 org.apache.shiro.util.ThreadContext或作为vm静态单例。这个 是一个无效的应用程序配置。]有根本原因 org.apache.shiro.UnavailableSecurityManagerException:没有 SecurityManager可以访问调用代码,或者绑定到 org.apache.shiro.util.ThreadContext或作为vm静态单例。这个 是无效的应用程序配置。在 org.apache.shiro.SecurityUtils.getSecurityManager(SecurityUtils.java:123) 在org.apache.shiro.subject.Subject $ Builder。(Subject.java:627) 在org.apache.shiro.SecurityUtils.getSubject(SecurityUtils.java:56) 在 org.apache.shiro.web.servlet.ShiroHttpServletRequest.getSubject(ShiroHttpServletRequest.java:88) 在 org.apache.shiro.web.servlet.ShiroHttpServletRequest.getSubjectPrincipal(ShiroHttpServletRequest.java:93) 在 org.apache.shiro.web.servlet.ShiroHttpServletRequest.getUserPrincipal(ShiroHttpServletRequest.java:111) 在 org.springframework.web.servlet.FrameworkServlet.getUsernameForRequest(FrameworkServlet.java:1079) 在 org.springframework.web.servlet.FrameworkServlet.publishRequestHandledEvent(FrameworkServlet.java:1065) 在 org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:996) 在 org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:621)at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:728)at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 在 org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749) 在 org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:660) 在 org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:626) 在 org.apache.catalina.core.AsyncContextImpl $ 1.run(AsyncContextImpl.java:225) 在 org.apache.catalina.core.AsyncContextImpl.doInternalDispatch(AsyncContextImpl.java:368) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 在 org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 在 org.apache.catalina.connector.CoyoteAdapter.asyncDispatch(CoyoteAdapter.java:300) 在 org.apache.coyote.http11.AbstractHttp11Processor.asyncDispatch(AbstractHttp11Processor.java:1636) 在 org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:599) 在 org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1721) 在 org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1679) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615) 在java.lang.Thread.run(Thread.java:724)