Spring Security httpBasic在使用正确凭据时返回404。

huangapple go评论73阅读模式
英文:

Spring Security httpBasic 404 for correct credentials

问题

在我的 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

当输入错误的凭据时,会显示基本认证弹出框,并且没有凭据被视为有效 - 在登录尝试后,弹出框会继续出现。

我漏掉了什么?

英文:

In my react app I call spring security basic login:

        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();
    }
}

The problem is, when entering correct credentials on login page (spring security's default: username "user" and generated password), the response i get in console is:

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

when entering incorrect credentials basic authentication pop-up shows, Spring Security httpBasic在使用正确凭据时返回404。

and no credentials are treated as valid – the pop-up keeps reappearing after login attempts.

What am I missing?

答案1

得分: 1

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

将您的 antMatchers 行更改为:

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

I think it's because you're securing the login endpoint, so you've created a ficious circle - you have to be authenticated to authenticate.

Change your antMatchers line to:

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

</details>



huangapple
  • 本文由 发表于 2020年10月3日 23:00:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定