【发布时间】: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