【问题标题】:How can I protect already built spring web application against XSS and CSRF attacks?如何保护已经构建的 Spring Web 应用程序免受 XSS 和 CSRF 攻击?
【发布时间】:2016-01-06 23:28:21
【问题描述】:

我已经使用 spring 4.2.0.RELEASE 完全构建了我的 Web 应用程序。现在在测试过程中,我发现应用程序容易受到 XSS 和 CSRF 攻击。我需要知道如何以最少的努力保护它(/更改现有代码)。

我已经参考了这个spring 文档:它说,

从 Spring Security 4.0 开始,CSRF 保护默认启用 XML 配置。

所以我认为我需要使用弹簧安全性。所以我添加了 pom 依赖项:

    <!-- Spring Security -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

但是我提到的所有指南和教程都要求配置 Spring 安全性,以及登录功能的更改。基本上弹簧安全负责登录。但我不想改变它,我只需要使用 Spring Security 的一些特性。基本上是 CSRF 令牌和 XSS 相关的功能,我可以这样做吗?

我添加了 pom 依赖,仅此而已。我还需要做什么?有没有我找不到的好参考。我正在为项目使用 STS 我提到了this:但它也要求弹簧认证。

【问题讨论】:

  • 除非您在security.xml 中指定csrf = "disabled",否则它将从v4.x 自动启用..
  • 我没有 security.xml 文件

标签: java spring spring-mvc spring-security csrf


【解决方案1】:

首先,您需要将 Spring Security 集成到您的 Web 应用程序中。有很多教程。然后你可以启用像

<http>
  <!-- ... -->
  <csrf disabled="true"/>
</http>

在spring安全文档中,http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/#csrf-configure,是这样说的;

As of Spring Security 4.0, CSRF protection is enabled by default with XML 
configuration. If you would like to disable CSRF protection, the corresponding 
XML configuration can be seen below.

<http>
    <!-- ... -->
    <csrf disabled="true"/>
</http>

CSRF protection is enabled by default with Java configuration. If you would like to disable 
CSRF, the corresponding Java configuration can be seen below. Refer to the Javadoc of csrf() 
for additional customizations in how CSRF protection is configured.

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable();
    }
}

【讨论】:

  • 好吧,我知道我需要使用 Spring Security,但正如我所说的,我发现的所有教程都要求我更改我现阶段不想做的登录功能。你能给出一些好的教程的链接吗?我已经浏览了您在回答中提到的那个文件。
  • 我不知道您当前的登录功能。如果您使用 ajax 登录,您可以按照本教程进行操作 raibledesigns.com/rd/entry/… 或者如果您发布您当前的登录页面,我们可以找到一种使用 Spring Security 更改它的简单方法。
  • 我的登录是正常的。来自 ldap 服务器的 AD 身份验证。我不希望 spring 提供基于角色的功能,因为我已经处理了角色访问。我不想要那个&lt;authentication-manager&gt; &lt;authentication-provider&gt;
猜你喜欢
  • 2019-09-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 1970-01-01
  • 1970-01-01
  • 2014-08-13
  • 2017-02-07
相关资源
最近更新 更多