Не работает Set-Cookie после ответа
Не могу понять как заставить браузер сохранять куки, отправляю запрос на сервер и в случае успеха получаю ответ с куки(в куки сохранён ключ авторизации).
Использую 2 локальных сервера, localhost:8081 - фронт, localhost:8080 - бэк.
Клиент
methods: {
send() {
const FormData = require('form-data');
const form = new FormData();
form.append("login", this.login);
form.append("password", this.password);
fetch('http://localhost:8080/api/web/request/profile/auth', {
method: 'POST',
headers: {
'Allow': 'POST',
'Access-Control-Allow-Origin': '*',
Accept: 'application/json',
'Content-Language': 'en'
},
body: form
});
}
}
Сервер
if(userEntity.getPassword().equals(passwordHash)) {
sessionKey = sessionService.buildSessionIDFactory(httpServletRequest, login);
userSessionKey = new Cookie(USER_SESSION_KEY, sessionKey);
userSessionKey.setPath("/auth");
log.info(String.format("user is auth true login %s, password %s", login, password));
log.info(String.format("decode after auth %s", new String(Base64.decodeBase64(sessionKey))));
httpServletResponse.addCookie(userSessionKey);
log(login, remoteAddress, AuthStatus.SUCCESS, httpServletRequest.getHeader(USER_AGENT));
} else {
log.info(String.format("user is auth fail login %s, password %s", login, password));
log(login, remoteAddress, AuthStatus.FAILURE, httpServletRequest.getHeader(USER_AGENT));
throw new ResponseStatusException(HttpStatus.FORBIDDEN, "incorrect login or password");
}
ServerConfig
@Override
public void addCorsMappings(CorsRegistry corsRegistry) {
corsRegistry.addMapping("/**")
.allowedMethods("GET","POST")
.allowedOrigins("*")
;
}
@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
return (web) -> web.ignoring()
.antMatchers("/**");
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean Date date() {
return new Date();
}
@Bean Utils utils() { return new Utils(); }
@Bean
public SecurityFilterChain configure(HttpSecurity httpSecurity) throws Exception {
// httpSecurity
// .addFilterAfter(new SessionFilter(), BasicAuthenticationFilter.class)
// .antMatcher("/api/web/request/profile/logout");
return httpSecurity.build();
}
