数据未保留在Spring会话bean中

时间:2012-11-09 01:46:57

标签: spring session javabeans

我在Spring 3.1中创建了一个简单的会话范围bean。它应该让我可以轻松访问当前登录用户的公司。

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionData {

    private Company company;

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }
}

我在自定义身份验证提供程序的authenticate()方法中使用公司填充此bean。

@Component(value = "authenticationProvider")
public class ProduxAuthenticationProvider implements AuthenticationProvider {

    private SessionData sessionData;
    private CompanyService companyService;

    @Autowired
    public ProduxAuthenticationProvider(SessionData sessionData, CompanyService companyService) {
        this.sessionData = sessionData;
        this.companyService = companyService;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        // Authentication logic (token creation) removed for readability....

        Company company = companyService.findByUserProfile(profile);

        // Set company on session bean
        sessionData.setCompany(company);

        return token;
    }
}

然后我尝试访问ManagementHomeController中的公司字段,该字段在身份验证成功完成后运行。

@Controller
@RequestMapping(value = "/manage/home")
public class ManagementHomeController {

    private CourseService userService;
    private CompanyService companyService;
    private SessionData sessionData;

    @Autowired
    public ManagementHomeController(CourseService userService, CompanyService companyService, SessionData sessionData) {
        this.userService = userService;
        this.companyService = companyService;
        this.sessionData = sessionData;
    }

    @RequestMapping(method = RequestMethod.GET)
    public String get(Model model) {
        model.addAttribute("mainContent", "content/managementHome");

        // sessionData.company is null here! Object id is same as in ProduxAuthenticationProvider

        return "main";
    }
}

在ManagementHomeController中,SessionData的公司字段为null,但它是同一个对象。我可以看到,因为sessionData的对象ID在ProduxAuthenticationProvider和ManagementHomeController中是相同的。

任何想法为什么SessionData都在失去它的公司?

1 个答案:

答案 0 :(得分:2)

不完全确定这是否可行,但是从我理解,因为您正在将一个会话范围的bean注入到一个过滤器中,该过滤器在调用Spring Dispatcher servlet之前被调用,但是作用域代理正常工作所需的某些请求属性可能不会已正确设置。

修复方法是将此逻辑移动到Spring Interceptor或在web.xml中注册RequestContextListener

相关问题