SpringSecurity 中 ,then 过期了然后页面就显示403
时间: 2024-02-11 12:08:05 浏览: 21
Spring Security 中的 `then` 是一个授权规则的关键字,用于在授权规则中指定授权通过后的操作。如果 `then` 过期,那么授权通过后就没有指定的操作,所以会显示 403 错误。
通常情况下,我们可以使用 `hasRole` 或 `hasAuthority` 等授权规则来指定用户的角色或权限,然后使用 `access` 关键字来指定授权通过后的操作。例如:
```java
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasAnyRole("ADMIN", "USER")
.antMatchers("/public/**").permitAll()
.anyRequest().authenticated()
.and().formLogin().loginPage("/login").permitAll()
.and().logout().permitAll()
.and().csrf().disable();
```
在上面的代码中,`hasRole` 和 `hasAnyRole` 规则用于指定用户的角色,`permitAll` 规则用于指定公共访问的路径,`authenticated` 规则用于指定需要认证的路径,而 `loginPage` 和 `logout` 则用于指定登录和注销的路径。如果授权通过,用户就可以访问对应的路径,否则就会重定向到登录页面或显示 403 错误。