В настоящее время я разрабатываю REST API с Spring Boot для внешнего приложения Angular2.
Я использую Spring Security для управления аутентификацией пользователей, но мне нужно сохранить некоторую информацию в сеансе браузера. Проблема в том, что при каждом запросе создается новый JSESSIONID
.
Пример:
- Аутентификация
POST
ВозвращаетSet-Cookie:JSESSIONID=C367245309E4E80606066FDCFBE0EE43
в заголовке ответа. Новый сеанс создается с информацией о пользователе
- Защищенный ресурс REST
GET
: сеанс пуст, аJSESSIONID
файл cookie отсутствует в заголовке запроса. Он возвращаетSet-Cookie:JSESSIONID=163B28B7AC2042F9EFF1046F9E14A600
Моя конфигурация безопасности Spring:
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
// Unable x-frame-options from same origin
httpSecurity.headers().frameOptions().sameOrigin();
/*
* the secret key used to signe the JWT token is known exclusively by
* the server. With Nimbus JOSE implementation, it must be at least 256
* characters longs.
*/
String secret = IOUtils.toString(getClass().getClassLoader().getResourceAsStream("secret.key"),
Charset.defaultCharset());
httpSecurity.addFilterAfter(jwtTokenAuthenticationFilter("/**", secret), ExceptionTranslationFilter.class)
.addFilterBefore(new SimpleCORSFilter(), CorsFilter.class)
/*
* Exception management is handled by the
* authenticationEntryPoint (for exceptions related to
* authentications) and by the AccessDeniedHandler (for
* exceptions related to access rights)
*/
.exceptionHandling().authenticationEntryPoint(new SecurityAuthenticationEntryPoint())
.accessDeniedHandler(new RestAccessDeniedHandler()).and()
/*
* anonymous() consider no authentication as being anonymous
* instead of null in the security context.
*/
.anonymous().and()
/* No Http session is used to get the security context */
//
.sessionManagement().maximumSessions(1).and().sessionFixation().none()
.sessionCreationPolicy(SessionCreationPolicy.ALWAYS).and().authorizeRequests()
/*
* All access to the authentication service are permitted
* without authentication (actually as anonymous)
*/
.antMatchers("/auth/**").permitAll().antMatchers("/css/**").permitAll().antMatchers("/js/**")
.permitAll().antMatchers("/accueil").permitAll()
// .antMatchers("/**").permitAll()
/*
* All the other requests need an authentication. Role access is
* done on Methods using annotations like @PreAuthorize
*/
.anyRequest().authenticated().and().addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class).csrf()
.csrfTokenRepository(csrfTokenRepository()).disable();
}
Можете ли вы помочь мне исправить мою проблему с сеансом, пожалуйста?