相关疑难解决方法(0)

如何设置Spring Security SecurityContextHolder策略?

我在我的服务中使用异步方法(Spring 3 @Async注释).而且我遇到了问题 - 衍生线程没有安全上下文.原因是Spring Security默认使用SecurityContextHolder.MODE_THREADLOCAL其上下文持有者的策略.但我需要使用SecurityContextHolder.MODE_INHERITABLETHREADLOCAL策略.目前我在AuthenticationSuccessHandler中设置了策略.但在我看来,这不是一个好习惯.

那么如何在上下文配置文件中进行设置呢?
spring security的版本是3.0.0.

spring spring-security

30
推荐指数
4
解决办法
3万
查看次数

Spring MVC:如何在生成的线程中使用请求范围的bean?

在Spring MVC应用程序中,我有一个请求范围的bean.我在某处注入了这个bean.在那里,HTTP请求服务线程可能会产生一个新线程.

但每当我尝试从新生成的线程访问请求范围的bean时,我得到一个org.springframework.beans.factory.BeanCreationException(参见下面的堆栈跟踪).
从HTTP请求线程访问请求范围的bean工作正常.

如何为HTTP请求线程生成的线程提供请求范围的bean?


设置简单

获取以下代码段运行.然后启动服务器,例如http://example.com:8080.
访问http://example.com:8080/scopetestnormal时,每次向此地址发出请求时,counter都会增加1(通过记录器输出会显着).:)超级!

访问http://example.com:8080/scopetestthread时,每次向此地址发出请求时,都会抛出上述异常.:(.无论什么选择ScopedProxyMode,这种情况发生两个基于CGLIB 基于JDK动态代理接口请求范围豆

配置文件

package com.example.config

@Configuration
@ComponentScan(basePackages = { "com.example.scopetest" })
public class ScopeConfig {

    private Integer counter = new Integer(0);

    @Bean
    @Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Number counter() {
        counter = new Integer(counter.intValue() + 1);
        return counter;
    }


    /* Adding a org.springframework.social.facebook.api.Facebook request-scoped bean as a real-world example why all this matters
    @Bean
    @Scope(value = "request", …
Run Code Online (Sandbox Code Playgroud)

spring-mvc spring-social

26
推荐指数
3
解决办法
3万
查看次数

如何克隆Servlet容器提供的分离的HttpServletRequest和HttpServletResponse?

我想实现以下逻辑:当我在主servlet的doService方法(在主Web容器线程中)中收到HttpServletRequeset和HttpServletResponse时,我启动A,B,C三个线程(由我自己的程序管理的线程)来处理其他servlet并行模式,然后在主线程中加入来自这些servlet的每个响应,如果我自己的一个线程(假设一个线程)工作缓慢,主线程将完成,所以主响应将返回给user.A 线程必须继续如果工作正常,我稍后会在浏览器端使用AJAX请求A线程的响应.

因此,我想克隆Servlet容器提供的HttpServlettRequest和HttpServletResponse,并且必须分离克隆的请求和响应(当容器的HttpServletTrequest和HttpServletResponse完成时,克隆的请求和响应仍然可以正常工作).

克隆的请求和响应的行为必须与我的代码视图中的Container相同.它可以被跟踪和包含.

任何的想法?

非常感谢!

所需产物

java servlets

8
推荐指数
2
解决办法
2万
查看次数

Java/Spring MVC:为子线程提供请求上下文

我有问题,我想将我的Spring WebMVC应用程序的一些进程外包到单独的Threads中.这很容易并且有效,直到我想使用一个使用全局请求的类userRightService.这在线程中不可用,我们遇到了一个问题,这几乎是可以理解的.

这是我的错误:

java.lang.RuntimeException:
org.springframework.beans.factory.BeanCreationException: Error creating bean
with name 'scopedTarget.userRightsService': Scope 'request' is not active
for the current thread; consider defining a scoped proxy for this bean if
you intend to refer to it from a singleton; nested exception is 
java.lang.IllegalStateException: Cannot ask for request attribute - 
request is not active anymore!
Run Code Online (Sandbox Code Playgroud)

好的,够清楚了.我试图通过实现此解决方案来保持请求上下文:

如何在异步任务执行程序中启用请求范围

这是我的runnable类:

@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class myThread implements Runnable {

  private RequestAttributes context;

  public DataExportThread(RequestAttributes context) {
    this.context = context;
  }

  public …
Run Code Online (Sandbox Code Playgroud)

java spring multithreading spring-mvc

8
推荐指数
1
解决办法
3040
查看次数

为什么会收到WELD-001303:作用域类型javax.enterprise.context.RequestScoped异常没有活动上下文?

我已经在网站上查看了与该错误有关的其他问题,但其中大多数都是关于SessionScope的,或者没有答案。唯一可能有用的是从线程中调用bean时,作用域类型javax.enterprise.context.RequestScoped没有活动上下文,但它不在我所拥有的上下文中。

我在Wildfly 10.1(Java ee 7)上运行JAX-RS端点。看起来像这样:

@Path("")
public class ServerResource {

    @Inject
    Order order;

    @Resource
    ManagedExecutorService mes;

    @PUT
    @Path("/prepareOrder")
    public void prepareOrder(@Suspended AsyncResponse response) {
        mes.execute(() ->  {
            try {
                Item item = new Item(); // JPA entity
                order.setItem(item); // line 71
                // call a service to save the order data (like item) to the DB
            } catch (Exception e) {
                e.printStackTrace();
                response.resume(false);
            }
            response.resume(true);
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

我添加try-catch仅仅是因为这个问题,它通常不存在。 Order

@Stateful
@RequestScoped
public class Order {
    private Item …
Run Code Online (Sandbox Code Playgroud)

jax-rs cdi wildfly

2
推荐指数
1
解决办法
4355
查看次数