我正在尝试将csrf标签添加到表单中,但它似乎与mvc中的工作方式不同。
所以我做的是添加
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
登录表单,但即使存在这些注释,_csrf属性也不存在
@EnableWebFluxSecurity
@EnableReactiveMethodSecurity
这是我的SecurityWebFilterChain:
http
.authorizeExchange().pathMatchers(
"/landing",
"/",
"/register",
"/login",
"/favicon.ico",
"/js/**",
"/fonts/**",
"/assets/**",
"/css/**",
"/webjars/**").permitAll()
.anyExchange().authenticated()
.and()
.httpBasic()
.and()
.formLogin().loginPage("/login")
.and().logout()
我错过了什么?
更新: 添加了我正在使用的相关依赖项。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.0</thymeleaf-layout-dialect.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity4</artifactId>
<version>3.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>io.github.jpenren</groupId>
<artifactId>thymeleaf-spring-data-dialect</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
</dependencies>
UPDATE 当我将带有csrf的隐藏输入标记包含在登录表单中时:
<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" />
我收到此错误:
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "_csrf.parameterName" (template: "public/login" - line 75, col 17)
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'parameterName' cannot be found on null
因为_csrf因某种原因为空,即使注释已到位。
登录控制器:
@GetMapping("/login")
public String login(Model model) {
return "public/login";
}
还尝试添加如下控制器建议:
@ControllerAdvice
public class SecurityAdvice {
@ModelAttribute("_csrf")
Mono<CsrfToken> csrfToken(final ServerWebExchange exchange) {
final Mono<CsrfToken> csrfToken = exchange.getAttribute(CsrfToken.class.getName());
return csrfToken.doOnSuccess(token -> exchange.getAttributes()
.put(DEFAULT_CSRF_ATTR_NAME, token)).log();
}
}
与此处使用的方法类似:https://github.com/daggerok/csrf-spring-webflux-mustache
然而,这会导致
java.lang.NullPointerException: null
at com.a.Config.SecurityAdvice.csrfToken(SecurityAdvice.java:23) ~[classes/:na]
这一行是最后一个片段的返回部分。
答案 0 :(得分:0)
这就是我要使其正常工作的方法。
@GetMapping("/login")
public Mono<String> login(ServerWebExchange exchange, Model model) {
Mono<CsrfToken> token = exchange.getAttributeOrDefault(CsrfToken.class.getName(), Mono.empty());
return token.map(t -> {
model.addAttribute("_csrf", t);
return "login";
});
}
答案 1 :(得分:0)
我必须加入thymeleaf-extras-springsecurity5
(而不是springsecurity4
)才能使它正常工作。
我找不到任何CsrfToken,这意味着@ mk88的回答也没有帮助,this guide也没有帮助。
一旦我包含以下依赖项,它就会按设计的那样完美地工作:
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
<version>3.0.4.RELEASE</version>
</dependency>
Thymeleaf的输出:
<form action="/members" method="POST" ><input type="hidden" name="_csrf" value="432033fd-1076-4620-9f99-d0220b6d3071"/>
答案 2 :(得分:0)
在所有控制器上均可使用的最干净的解决方案是使用in the documentation(详细信息{{3}}):
@ControllerAdvice
答案 3 :(得分:0)
默认情况下,WebFlux中似乎没有将CsrfToken信息实际添加到模型中。 (请参阅https://github.com/spring-projects/spring-security/issues/6046)
解决方法是添加订阅令牌信息的ControllerAdvice,并通过ComponentScan引用它,或将其声明为显式bean,如下所示:
(我正在使用Kotlin,但这在Java中应该以相同的方式工作)
import org.springframework.security.web.reactive.result.view.CsrfRequestDataValueProcessor.DEFAULT_CSRF_ATTR_NAME
import org.springframework.security.web.server.csrf.CsrfToken
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ModelAttribute
import org.springframework.web.server.ServerWebExchange
import reactor.core.publisher.Mono
@ControllerAdvice
class CsrfControllerAdvice {
@ModelAttribute(value = DEFAULT_CSRF_ATTR_NAME)
fun csrfToken(exchange: ServerWebExchange): Mono<CsrfToken> {
return exchange.getAttributeOrDefault(CsrfToken::class.java.name, Mono.empty())
}
}