在版本3.0.x中禁用Spring方法安全性

时间:2012-11-12 12:09:14

标签: spring security spring-security spring-3

我有一个配置了spring security的Web应用程序,用于限制对URL和方法的访问。我想在默认情况下完全禁用它,并允许我的客户在需要时轻松打开它(他们只能访问“spring-security.xml”)。

我设法关闭了URL拦截,但我的方法安全性仍然启用...

有任何线索吗?

(我不想让客户更改我的web.xml,所以不幸的是每次都不能修改“global-method-security”设置......)

这是我更新的spring-security.xml配置:

<http auto-config='true' use-expressions="true">
    <intercept-url pattern="/**" access="permitAll" />
    <http-basic />
    <anonymous />
</http>

我已经覆盖了DelegatingFilterProxy.doFilter方法,如下所示:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
    final String springSecured = System.getProperty("springSecured");

    if (StringUtils.isNotBlank(springSecured) && springSecured.equalsIgnoreCase("true")) {
        // Call the delegate
        super.doFilter(request, response, filterChain);
    } else {
        // Ignore the DelegatingProxyFilter delegate
        filterChain.doFilter(request, response);
    }
}

这是我所拥有的方法安全性的一个例子:

@RequestMapping(
        value = "applications/{applicationName}/timeout/{timeout}",
        method = RequestMethod.POST)
public
@ResponseBody
@PreAuthorize("isFullyAuthenticated() and hasPermission(#authGroups, 'deploy')")
Object deployApplication() {
    // ...
}

1 个答案:

答案 0 :(得分:4)

如果我是你,我不会使用自定义过滤器链实现,只是开箱即用。您可以使用嵌套元素启用和禁用bean配置的部分(自Spring 3.0起),因此这样的方法可能很方便:

<beans profile="secure">
    <http auto-config='true' use-expressions="true">...</http>
</beans>

您的应用程序现在不受默认配置文件(以及除“安全”配置文件之外的任何其他配置文件)的保护。您可以通过提供系统属性spring.profiles.active = secure或通过在上下文或servlet初始化程序中显式设置来启用安全配置文件。