我在帶有 index.html 和 login.html 的基本 Thymeleaf 設定上使用 Spring Security,但是當憑據有效時,默認登錄頁面總是回傳 403 Forbidden。(正如預期的那樣,當憑據不匹配時,它會出現 UI 錯誤)。
我相信這是由于 CSRF 令牌已經作為 cookie (XSRF-TOKEN) 包含在每個后端請求中。我不想簡單地禁用 CSRF,所以我嘗試以幾乎所有我可以在網上找到的方式將此令牌包含到 POST 請求中:
- 改變目標
/login?_csrf=token - 插入
<input type="hidden" name="_csrf" ...Thymeleaf 表單(這是默認行為,我檢查它確實發送,但后端拒絕它??) - 從普通表單提交交換到 AJAX/fetch 并插入 X-XSRF-TOKEN 標頭。也不起作用,包括 JSON 和 x-www-form-urlencoded 編碼請求。
有任何想法嗎?默認的 Spring Security /loginPOST 端點在請求中期望什么?它如何期望 CSRF 令牌?身份驗證似乎有效,只是 CSRF 在成功登錄時失敗。或者是我完全遺漏了其他什么東西,給了我一個 403 Forbidden?
提前致謝!!
我的設定
Spring Boot 版本:2.6.2
pom.xml 依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.zsoltfabok</groupId>
<artifactId>sqlite-dialect</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
索引.html
<form th:action="@{/login}" method="post">
<label for="username">Username</label>
<input type="text" name="username" />
<label for="password">Password</label>
<input type="password" name="password" />
<button type="submit">Submit</button>
<!-- there's also a hidden CSRF token generated automatically -->
</form>
網路安全配置器配接器
@Configuration
@EnableGlobalAuthentication
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// ...
@Override
public void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/favicon.ico").anonymous()
.antMatchers("/static/**").anonymous()
.mvcMatchers("/", "/login", "/signup").anonymous()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login")
.defaultSuccessUrl("/")
.and()
.httpBasic()
.and()
.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
}
}
WebMvc配置器
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
控制器
@Controller
@RequestMapping("/")
public class WebController {
@GetMapping
public String index() {
return "index";
}
}
uj5u.com熱心網友回復:
問題是您的安全規則。
.antMatchers("/favicon.ico").anonymous()
.antMatchers("/static/**").anonymous()
.mvcMatchers("/", "/login", "/signup").anonymous()
您將它們配置為匿名訪問。這意味著尚未通過身份驗證的人。它將阻止經過身份驗證的用戶訪問任何這些 URL(或者如果您禁用匿名訪問則未經身份驗證)。
修復允許任何人使用permitAll()而不是訪問的問題anonymous()。
.antMatchers("/favicon.ico").permitAll()
.antMatchers("/static/**").permitAll()
.mvcMatchers("/", "/login", "/signup").permitAll()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/404710.html
標籤:
上一篇:如何修復未能延遲初始化集合
下一篇:嘗試以angular呼叫SpringREST端點時,Originhttp://localhost:4200已被瀏覽器中的CORS策略錯誤阻止
