【问题标题】:An Authentication object was not found in the SecurityContext - Servlet 3 with Async Support在 SecurityContext 中找不到身份验证对象 - 具有异步支持的 Servlet 3
【发布时间】:2013-10-01 02:56:17
【问题描述】:

使用带有异步支持的 Spring 3.2。在匿名 Callable 方法中,安全上下文会丢失一次

@RequestMapping(value = "/home", method = RequestMethod.GET)
public Callable<String> home(final Model model) {
    return new Callable<String>() {
        @Override
        public String call() throws Exception {
            model.addAttribute("homeService", homeService.findId(1));
            return "home";
        }
    };
}

这是应用于 servlet-context.xml 内的 bean 的安全装饰器

<beans:bean id="homeService" class="example.service.HomeServiceImpl" scope="request">
    <security:intercept-methods>
        <security:protect access="ROLE_USER" method="find*"/>
    </security:intercept-methods>
</beans:bean>

这是错误,因为安全上下文不存在: org.springframework.security.authentication.AuthenticationCredentialsNotFoundException: An Authentication object was not found in the SecurityContext

【问题讨论】:

    标签: spring spring-mvc asynchronous spring-security servlet-3.0


    【解决方案1】:

    升级到Spring Security 3.2.0 RC1 是解决方案。

    This article 提到 Spring Security 3.2 与 Servlet 3 异步支持兼容。

    将 SecurityContext 与 Callable 相关联
    从技术上讲, Spring Security 与 WebAsyncManager 集成。安全上下文 用于处理 Callable 的是 SecurityContext 当时存在于 SecurityContextHolder 调用 startCallableProcessing。

    Maven 依赖:

    <dependencies>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.2.0.RC1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.2.0.RC1</version>
        </dependency>
    </dependencies>
    
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 2017-09-01
      • 2017-08-03
      • 2020-03-04
      • 1970-01-01
      • 1970-01-01
      • 2017-01-30
      • 2017-08-17
      • 1970-01-01
      相关资源
      最近更新 更多