我正在学习Spring boot,以及图书馆商店系统的编码。现在我正在使用 Spring Security 6 来实现登录功能。当我在 swagger 3 中检查我的 api 时,我的登录页面一直显示 403。我不知道为什么,有人可以帮我解决这个问题吗? (ps. 英语不是我的母语,所以我不太擅长。)
帮助我解决登录问题,使我的身份验证工作正常 非常感谢!
您可以尝试很多可能性,我建议这 2 个选项。
@CrossOrigin // <- Only add this
@RestController
@RequestMapping("/Book")
public class BooksController {
...
}
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// Other configuration...
.authorizeRequests()
.antMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll() // Allow Swagger UI and API Docs
.anyRequest().authenticated()
// Other configuration...
}
}