Java 如何在spring security/thymeleaf中自动生成csrf令牌而不使用@EnableWebMvcSecurity

Java 如何在spring security/thymeleaf中自动生成csrf令牌而不使用@EnableWebMvcSecurity,java,spring-mvc,spring-security,csrf,thymeleaf,Java,Spring Mvc,Spring Security,Csrf,Thymeleaf,我一直在想为什么隐藏的csrf字段不会自动添加到我的登录表单中。 我将Spring4.1.1与SpringSecurity 4.0.1和Thymeleaf 2.1.4一起使用 我所能找到的解决问题的方法是在thymeleaf中使用_csrf变量手动添加字段(尽管_csrf对我来说为null),或者在Java配置中使用@EnableWebMvcSecurity。 但是,我使用xml来配置安全性,并希望保持这种方式。 这一切归结起来就是:我可以向安全xml中添加什么来让thymeleaf生成csrf

我一直在想为什么隐藏的csrf字段不会自动添加到我的登录表单中。 我将Spring4.1.1与SpringSecurity 4.0.1和Thymeleaf 2.1.4一起使用

我所能找到的解决问题的方法是在thymeleaf中使用_csrf变量手动添加字段(尽管_csrf对我来说为null),或者在Java配置中使用@EnableWebMvcSecurity。 但是,我使用xml来配置安全性,并希望保持这种方式。 这一切归结起来就是:我可以向安全xml中添加什么来让thymeleaf生成csrf令牌字段

我当前的配置是:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:security="http://www.springframework.org/schema/security"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <!-- **************************************************************** -->
    <!--  RESOURCE FOLDERS CONFIGURATION                                  -->
    <!--  Dispatcher configuration for serving static resources           -->
    <!-- **************************************************************** -->
    <mvc:resources mapping="/dist/**" location="file:/var/www/meubelplan/dist/"/>
    <mvc:resources mapping="/css/**" location="file:/var/www/meubelplan/css/"/>
    <mvc:resources mapping="/js/**" location="file:/var/www/meubelplan/js/"/>
    <mvc:resources mapping="/images/**" location="file:/var/www/meubelplan/images/"/>

    <!-- **************************************************************** -->
    <!--  SPRING ANNOTATION PROCESSING                                    -->
    <!-- **************************************************************** -->
    <mvc:annotation-driven/>
    <context:component-scan base-package="com.wwk.meubelplan"/>

    <!-- **************************************************************** -->
    <!--  SPRING SECURITY                                                 -->
    <!-- **************************************************************** -->

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="sem" password="rtyfghvbn" authorities="ROLE_USER" />
                <security:user name="winnie" password="ikbenwinnie" authorities="ROLE_USER" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

    <security:http pattern="/account/login" security="none"/>
    <security:http pattern="/account/logout" security="none"/>
    <security:http pattern="/account/create" security="none"/>
    <security:http use-expressions="false">
        <security:csrf/>
        <security:intercept-url pattern='/account/**' access='ROLE_USER' />
        <security:form-login login-page='/account/login' default-target-url='/account' always-use-default-target='true'/>
        <security:logout logout-url="/account/logout" delete-cookies="JSESSIONID" logout-success-url="/account/login"/>
    </security:http>

    <!-- **************************************************************** -->
    <!--  THYMELEAF-SPECIFIC ARTIFACTS                                    -->
    <!--  TemplateResolver <- TemplateEngine <- ViewResolver              -->
    <!-- **************************************************************** -->

    <bean id="templateResolver"
          class="org.thymeleaf.templateresolver.FileTemplateResolver">
        <property name="prefix" value="/var/www/meubelplan/" />
        <property name="suffix" value=".html" />
        <property name="templateMode" value="HTML5" />
        <property name="characterEncoding" value="UTF-8" />
        <property name="cacheable" value="false"/>
    </bean>

    <bean id="templateEngine"
          class="org.thymeleaf.spring4.SpringTemplateEngine">
        <property name="templateResolver" ref="templateResolver" />
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="templateEngine" ref="templateEngine" />
        <property name="characterEncoding" value="UTF-8" />
    </bean>

</beans>

我的表单模板是:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:include="partials/general/head"></head>
<body>

    <div class="container">

        <nav th:replace="partials/general/navbar"></nav>

        <div th:replace="partials/general/logobar"></div>

        <div class="row">
            <div class="col-md-6 col-md-offset-3">

                <br/><br/>

                <div class="panel panel-default">
                    <div class="panel-heading">Login om uw account gegevens te bekijken</div>
                    <div class="panel-body">
                        <form name="loginForm" method="POST" th:action="@{'~/account'}">
                            <div class="form-group">
                                <label for="username">Email address</label>
                                <input type="text" class="form-control" id="username" name="username" placeholder="Gebruikersnaam"/>
                            </div>
                            <div class="form-group">
                                <label for="password">Password</label>
                                <input type="password" class="form-control" id="password" name="password" placeholder="Password"/>
                            </div>
                            <button type="submit" class="btn btn-default" value="Submit">Inloggen</button>
                        </form>
                    </div>
                </div>

            </div>
        </div>

    </div><!-- /.container -->

    <span th:replace="partials/general/scripts"></span>

</body>
</html>



登录uw帐户gegevens te bekijken 电子邮件地址 密码 因洛根
提前感谢您向正确方向提供的任何提示:)

问候,


Sem

如果它没有自动插入CSRF令牌,您可以强制它通过以下行:

<input type="hidden"
    name="${_csrf.parameterName}"
    value="${_csrf.token}"/>

我在另一个问题中找到了我需要的信息:

答案是:

将以下xml添加到xml配置将使thymeleaf能够自动将csrf令牌输入添加到表单中

security.xml

<bean id="requestDataValueProcessor" class="org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor"/>

<bean id="csrfFilter" class="org.springframework.security.web.csrf.CsrfFilter">
    <constructor-arg>
        <bean class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository">
            <property name="headerName" value="X-SECURITY" />
        </bean>
    </constructor-arg>
</bean>

web.xml

<filter>
    <filter-name>csrfFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <async-supported>true</async-supported>
  </filter>

<filter-mapping>
    <filter-name>csrfFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

csrfFilter
org.springframework.web.filter.DelegatingFilterProxy
真的
csrfFilter
/*

您能否尝试声明一个类型为
org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor
的bean?感谢您的评论,尽管这一操作最初并不奏效,但它确实将我推向了正确的方向。经过一番搜索后,我遇到了这样一个问题:虽然模板引擎不同,但问题是相同的,我让它处理该问题的解决方案。@semvdwal请您分享解决方案。我一直在努力工作。谢谢你advance@RachitAgrawal不久前我回答了我的问题,请看这个问题的第二个答案,我现在也标记了它,以便您可以轻松地看到它是哪一个。但是,您的情况可能不同,因此如果提供的解决方案没有帮助,请随时向我提供一些详细信息,以便我可以帮助您解决(希望如此)。谢谢你的回答,不过,我想让ThymalEleaf自动生成字段。ThymalEleaf会自动生成参数。您能检查生成的html页面源代码并确认是否有隐藏的输入吗