gpt4 book ai didi

java - 如何在 spring security/thymeleaf 中自动生成 csrf token 而不使用 @EnableWebMvcSecurity

转载 作者:行者123 更新时间:2023-11-30 03:19:54 25 4
gpt4 key购买 nike

我一直试图找出为什么隐藏的 csrf 字段没有自动添加到我的登录表单中。我将 Spring 4.1.1 与 Spring Security 4.0.1 和 Thymeleaf 2.1.4 结合使用。

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

我当前的配置是:

<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>

我的表单 thymeleaf 模板是:

<!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>

预先感谢您提供正确方向的任何指示:)

问候,

扫描仪

最佳答案

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

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

关于java - 如何在 spring security/thymeleaf 中自动生成 csrf token 而不使用 @EnableWebMvcSecurity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31575091/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com