弹簧安全的弹簧webflow单元测试

时间:2013-02-27 17:14:28

标签: spring spring-security spring-webflow spring-webflow-2

我正在尝试使用弹簧安全性测试webflow控制器:

<action-state id="search">
    <secured attributes="ROLE_ADMIN"/>
...
</action-state>

我正在使用AbstractXmlFlowExecutionTests子类。

现在,测试在没有“安全”标签的情况下运行正常(我没有为安全性做任何模拟),但是一旦我添加安全标记,测试就会成功完成,尽管我预计安全例外被抛出 任何想法为什么它不起作用,我该如何配置它? 提前致谢! 伊戈尔

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案:我需要手动添加securityListener。 在startFlow之前:

setFlowExecutionListener(getSecurityListener(new String[] {"ROLE_ADMIN_FAKE"}));

其中

private FlowExecutionListener getSecurityListener(String[] roles) {
    List<GrantedAuthority> result = new ArrayList<>();
    for (String role: roles) {
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority(role);
        result.add(authority);
    }
    Authentication auth = new PreAuthenticatedAuthenticationToken("Igor", "", result);
    SecurityContextHolder.getContext().setAuthentication(auth);
    return new SecurityFlowExecutionListener();
}