![](/img/trans.png)
[英]Spring boot security not authenticating when using @CrossOrigin
[英]Unexpected 403 error when using Spring Security
在這里,我有表格的 html 代碼。 用於創建事件的表單。 它要求用戶提供一些信息,然后他必須按下按鈕創建。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head th:replace="fragments :: head"></head>
<body class="container">
<nav th:replace="fragments :: header"></nav>
<form method="post">
<div class="form-group">
<label>Name
<input th:field="${event.name}" class="form-control">
</label>
<p class="error" th:errors="${event.name}"></p>
</div>
<div class="form-group">
<label>Description
<input th:field="${event.eventDetails.description}" class="form-control">
</label>
<p class="error" th:errors="${event.eventDetails.description}"></p>
</div>
<div class="form-group">
<label>Contact Email
<input th:field="${event.eventDetails.contactEmail}" class="form-control">
</label>
<p class="error" th:errors="${event.eventDetails.contactEmail}"></p>
</div>
<div class="form-group">
<label>Category
<select th:field="${event.eventCategory}">
<option th:each="eventCategory : ${categories}"
th:value="${eventCategory.id}"
th:text="${eventCategory.name}"
></option>
</select>
<p class="error" th:errors="${event.eventCategory}"></p>
</label>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-success">
</div>
</form>
</body>
</html>
在這里,我的表格有 java 代碼。
@GetMapping("create")
public String displayCreateEventForm(Model model){
model.addAttribute("title", "Create Event");
model.addAttribute(new Event());
model.addAttribute("categories", eventCategoryRepository.findAll());
return "events/create";
}
@PostMapping("create")
public String processCreateEventForm(@ModelAttribute @Valid Event newEvent, Errors errors, Model model){
if (errors.hasErrors()){
model.addAttribute("title", "Create Event");
return "events/create";
}
eventRepository.save(newEvent);
return "redirect:";
}
我不知道為什么按下按鈕后它會給我一個錯誤,例如:
白色 label 錯誤頁面。 此應用程序沒有顯式映射 /error,因此您將其視為后備。
2020 年 12 月 29 日星期二 00:24:57 EET 出現意外錯誤(類型=禁止,狀態=403)。 禁止。
配置class
package com.example.demo.config;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private UserService userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers(
"/registration**",
"/js/**",
"/css/**",
"/img/**",
"/webjars/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.permitAll()
.and()
.logout()
.invalidateHttpSession(true)
.clearAuthentication(true)
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/login?logout")
.permitAll();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider auth = new DaoAuthenticationProvider();
auth.setUserDetailsService(userService);
auth.setPasswordEncoder(passwordEncoder());
return auth;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authenticationProvider());
}
}
正如@dan1st在評論部分提到的那樣,使用 SpringSecurity 時,所有表單提交都需要CSRF
(除非您禁用它)。 要在表單中自動添加csrf token
,一種簡單的方法是使用 thymeleaf 標簽。 一旦 thymeleaf 在表單中檢測到其標簽,它就會在丟失時將 csrf 令牌添加到隱藏輸入中。
這是一個例子
<form th:action="@{'your_post_path'}" method="POST" th:object="${yourModelAttributeEntity}">
...
</form>
您可以添加其中之一或兩者
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.