![](/img/trans.png)
[英]Spring security in angularjs returns 403 forbidden for login
[英]Spring Security CSRF 403 Forbidden on successful login
I'm using Spring Security on a basic Thymeleaf setup with index.html and login.html, however the default login page always returns 403 Forbidden when the credentials are valid. (如預期的那樣,當憑據不匹配時,它會給出 UI 錯誤)。
我相信這是由於 CSRF 令牌已經作為 cookie (XSRF-TOKEN) 包含在每個后端請求中。 我寧願不簡單地禁用 CSRF,所以我嘗試以我可以在網上找到的幾乎所有方式將此令牌包含到 POST 請求中:
/login?_csrf=token
<input type="hidden" name="_csrf"...
到 Thymeleaf 表單中(這是默認行為,我檢查它確實發送,但后端拒絕它??) 有任何想法嗎? 默認的 Spring Security /login
POST 端點在請求中期望什么? 它對 CSRF 代幣有何期待? 身份驗證似乎有效,只是 CSRF 在成功登錄時失敗。 還是我完全想念的其他東西給了我403 Forbidden?
提前致謝!!
我的設置
Spring 引導版本: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>
WebSecurityConfigurerAdapter
@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());
}
}
WebMvcConfigurer
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
}
Controller
@Controller
@RequestMapping("/")
public class WebController {
@GetMapping
public String index() {
return "index";
}
}
問題是您的安全規則。
.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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.