【问题标题】:Spring webflow unit test with spring security onSpring webflow单元测试与spring security on
【发布时间】:2013-03-09 04:21:02
【问题描述】:

我正在尝试使用 Spring Security 测试一个 webflow 控制器:

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

我正在使用AbstractXmlFlowExecutionTests 子类。

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

【问题讨论】:

  • 你能发布一个带有“安全”标签的sn-p吗?
  • 嗯,它在消息中......我在 webflow 的动作状态中使用它。当我实际运行我的应用程序时它工作正常,但在测试中失败。

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


【解决方案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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-21
    • 2018-12-13
    • 2010-09-26
    • 2019-01-20
    • 1970-01-01
    • 2013-01-11
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多