登录 Api 显示 403 错误。 - 春季安全 6

问题描述 投票:0回答:1

我正在学习Spring boot,以及图书馆商店系统的编码。现在我正在使用 Spring Security 6 来实现登录功能。当我在 swagger 3 中检查我的 api 时,我的登录页面一直显示 403。我不知道为什么,有人可以帮我解决这个问题吗? (ps. 英语不是我的母语,所以我不太擅长。)

这是我的项目:https://github.com/kylehuang640/libBEM02

帮助我解决登录问题,使我的身份验证工作正常 非常感谢!

authentication spring-security jwt backend
1个回答
0
投票

您可以尝试很多可能性,我建议这 2 个选项。

1. CORS 配置

@CrossOrigin // <- Only add this
@RestController
@RequestMapping("/Book")
public class BooksController {
...
}

2.创建 Spring Security 配置

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...
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.