spring-boot-learning
spring-boot-learning copied to clipboard
Disable session on JWT demo
In your demo of JWT hibernate, It is necessary to disable the session, so that API can be truthly stateless. Otherwise, the cookies will be use to authenticate. That it can get the resource without using the token after login https://github.com/loda-kun/spring-boot-learning/blob/master/spring-security-hibernate-jwt/src/main/java/me/loda/springsecurityhibernatejwt/WebSecurityConfig.java @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .cors() // Ngăn chặn request từ một domain khác .and() .authorizeRequests() .antMatchers("/api/login").permitAll() // Cho phép tất cả mọi người truy cập vào địa chỉ này .anyRequest().authenticated() .and() .sessionManagement() .sessionCreationPolicy(SessionCreationPolicy.STATELESS);; // Tất cả các request khác đều cần phải xác thực mới được truy cập
// Thêm một lớp Filter kiểm tra jwt
http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
}