0

I'm having problems establishing session between Angular 9 and Spring Boot.

When call localhost:8080/test I always get same SessionId from the server.

However, when I call the same endpoint via Angular 9,

 this.http.get('localhost:8080/test').subscribe()

I get each call different SessionId. Why?


WebSecurityConfig.java

@Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                .and()
                    .csrf()
                    .and()
                    .cors().disable()
                .authorizeRequests()
                .antMatchers("/test").permitAll()
                .anyRequest().authenticated()
}

TestEndpoint.java

@RestController
@Slf4j
public class TestEndpoint {

    @Autowired
    HttpSession session;

    @GetMapping("/test")
    public void test(){
        log.info(session.getId());
    }
}
kgf3JfUtW
  • 13,702
  • 10
  • 57
  • 80
  • Most likely you're not passing along the cookie holding your SessionID back to the server. XHR requests typically don't add cookies. Maybe take a look at https://stackoverflow.com/questions/35602866/how-to-send-cookie-in-request-header-for-all-the-requests-in-angular2 – Simon Mar 20 '20 at 16:17
  • Awesome! It works :) Thanks @Simon – Aleksander Rydzewski Mar 20 '20 at 16:45

0 Answers0