【问题标题】:Spring Security httpBasic 404 for correct credentialsSpring Security httpBasic 404 以获得正确的凭据
【发布时间】:2021-01-18 23:02:53
【问题描述】:

在我的 react 应用中,我调用 spring security 基本登录:

        e.preventDefault();
        const username = states.username;
        const password = states.password;

        fetch('/api/login', {
            method: 'post',
            headers: {
                Authorization: 'Basic ' + window.btoa(username + ':' + password),
            },
        }).then((resp) => {
            console.log(resp);

            return resp.text();
        });

SecurityConfig.java:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/api/login").authenticated()
                .and()
                .httpBasic();
    }
}

问题是,在登录页面上输入正确的凭据时(spring security 的默认值:用户名“user”和生成的密码),我在控制台中得到的响应是:

POST https://localhost:3000/api/login 404 (Not Found)
Response {type: "basic", url: "https://localhost:3000/api/login", redirected: false, status: 404, ok: false, …}
body: (...)
bodyUsed: true
headers: Headers {}
ok: false
redirected: false
status: 404
statusText: "Not Found"
type: "basic"
url: "https://localhost:3000/api/login"
__proto__: Response

当输入不正确的凭据时,基本身份验证弹出窗口显示

并且没有任何凭据被视为有效 - 登录尝试后弹出窗口不断重新出现。

我错过了什么?

【问题讨论】:

  • https://localhost:3000/api/login 上是否有任何东西在运行以处理请求?
  • 是的,后端已配置并运行,而且它接收请求
  • 显示/api/login的控制器方法。

标签: java reactjs spring authentication spring-security


【解决方案1】:

我认为这是因为您正在保护登录端点,所以您创建了一个恶性循环 - 您必须通过身份验证才能进行身份验证。

将您的 antMatchers 行更改为:

.antMatchers("/api/login").permitAll()
.and()
.formLogin()
.loginProcessingUrl("/api/login")

【讨论】:

    猜你喜欢
    • 2015-08-31
    • 2013-02-05
    • 1970-01-01
    • 2018-11-10
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    相关资源
    最近更新 更多