package com.thediff.api.config; import org.springframework.context.annotation.Bean;…
- User
package com.thediff.api.config;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration public class WebConfig {
@Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**"); } }; } } package com.thediff.api.config;
import com.google.common.collect.ImmutableList; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.data.web.config.EnableSpringDataWebSupport; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; import org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.CorsConfigurationSource; import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import javax.sql.DataSource; import java.util.Collections;
@EnableSpringDataWebSupport @EnableWebSecurity @EnableGlobalMethodSecurity(securedEnabled = true) @EnableJdbcHttpSession(maxInactiveIntervalInSeconds=604800) public class WebSecurityConfig {
@Configuration @Order(1) public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
protected void configure(HttpSecurity http) throws Exception {
http .csrf().disable() .antMatcher("/api/**") .authorizeRequests() .anyRequest().hasAnyRole("ADMIN", "API", "USER") .and() .httpBasic(); http.cors(); } }
@Configuration @Order(2) public static class SwaggerSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception {
http .csrf().disable() .antMatcher("/swagger*") .authorizeRequests() .anyRequest().hasAnyRole("ADMIN", "API") .and() .httpBasic(); http.cors(); } }
@Configuration @Order(3) public static class RecorderApiSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {
- Assistant
It's possible that the reason a new session is created in the "spring_session" table every time you do a curl request is because the requests are not including the necessary session cookie. When a client sends a request to a server, it includes a session cookie if it has one. If the server doesn't receive a session cookie with the request, it assumes that it's a new client and creates a new session.
To solve this issue, you can make sure that your curl requests include the session cookie by using the
-boption with curl. For example:javascriptcurl -b "SESSION=<session-id>" http://example.comReplace
<session-id>with the actual session ID value that you want to use. You can find the session ID value by looking at the "spring_session" table in your database.Alternatively, you can also try setting the
withCredentialsoption totruein your client-side code if you're making requests from a web application. This will ensure that the client includes the session cookie with each request.javascriptaxios.defaults.withCredentials = true; - User
I send the requests from java resttemplate
- Assistant
If you're using Java
RestTemplateto make API requests, you can set the session cookie manually in the request headers. Here's an example:javaRestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.set("Cookie", "SESSION=<session-id>"); HttpEntity<String> entity = new HttpEntity<>("body", headers); ResponseEntity<String> response = restTemplate.exchange( "http://example.com", HttpMethod.GET, entity, String.class);Replace
<session-id>with the actual session ID value that you want to use. You can find the session ID value by looking at the "spring_session" table in your database.This should ensure that the session cookie is included in the API requests and prevent a new session from being created in the "spring_session" table every time you make a request.