[spring-boot3 | WIP]: change cors policy with filter.
This commit is contained in:
parent
d0e8e7ea3f
commit
f7598b418c
|
@ -1,27 +1,10 @@
|
||||||
package eu.dnetlib.uoaauthorizationlibrary.configuration;
|
package eu.dnetlib.uoaauthorizationlibrary.configuration;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableConfigurationProperties({SecurityConfig.class, GlobalVars.class})
|
@EnableConfigurationProperties({SecurityConfig.class, GlobalVars.class})
|
||||||
@ComponentScan(basePackages = { "eu.dnetlib.uoaauthorizationlibrary" })
|
@ComponentScan(basePackages = { "eu.dnetlib.uoaauthorizationlibrary" })
|
||||||
public class AuthorizationConfiguration {
|
public class AuthorizationConfiguration { }
|
||||||
|
|
||||||
@Bean
|
|
||||||
public WebMvcConfigurer corsConfigurer() {
|
|
||||||
return new WebMvcConfigurer() {
|
|
||||||
@Override
|
|
||||||
public void addCorsMappings(CorsRegistry registry) {
|
|
||||||
registry.addMapping("/**")
|
|
||||||
.allowedOriginPatterns("*")
|
|
||||||
.allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS")
|
|
||||||
.allowCredentials(true);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
package eu.dnetlib.uoaauthorizationlibrary.configuration;
|
||||||
|
|
||||||
|
import jakarta.servlet.FilterChain;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.web.filter.OncePerRequestFilter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class CorsAllowFilter extends OncePerRequestFilter {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||||
|
FilterChain filterChain) throws ServletException, IOException {
|
||||||
|
response.addHeader("Access-Control-Allow-Origin", "*");
|
||||||
|
response.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
|
||||||
|
response.addHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");
|
||||||
|
response.addHeader("Access-Control-Expose-Headers", "Access-Control-Allow-Origin, Access-Control-Allow-Credentials");
|
||||||
|
response.addHeader("Access-Control-Allow-Credentials", "true");
|
||||||
|
response.addIntHeader("Access-Control-Max-Age", 10);
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
package eu.dnetlib.uoaauthorizationlibrary.stateless.security;
|
package eu.dnetlib.uoaauthorizationlibrary.stateless.security;
|
||||||
|
|
||||||
|
import eu.dnetlib.uoaauthorizationlibrary.configuration.CorsAllowFilter;
|
||||||
import eu.dnetlib.uoaauthorizationlibrary.security.EntryPoint;
|
import eu.dnetlib.uoaauthorizationlibrary.security.EntryPoint;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
@ -18,10 +19,12 @@ import org.springframework.security.web.authentication.www.BasicAuthenticationFi
|
||||||
public class WebSecurityConfig {
|
public class WebSecurityConfig {
|
||||||
|
|
||||||
private final AuthorizationFilter filter;
|
private final AuthorizationFilter filter;
|
||||||
|
private final CorsAllowFilter corsFilter;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public WebSecurityConfig(AuthorizationFilter filter) {
|
public WebSecurityConfig(AuthorizationFilter filter, CorsAllowFilter corsFilter) {
|
||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
|
this.corsFilter = corsFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -32,6 +35,7 @@ public class WebSecurityConfig {
|
||||||
@Bean
|
@Bean
|
||||||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
|
||||||
http.csrf(AbstractHttpConfigurer::disable);
|
http.csrf(AbstractHttpConfigurer::disable);
|
||||||
|
http.addFilter(corsFilter);
|
||||||
http.addFilterBefore(filter, BasicAuthenticationFilter.class);
|
http.addFilterBefore(filter, BasicAuthenticationFilter.class);
|
||||||
http.exceptionHandling(handler -> handler.authenticationEntryPoint(this.entryPoint()));
|
http.exceptionHandling(handler -> handler.authenticationEntryPoint(this.entryPoint()));
|
||||||
http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
|
http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());
|
||||||
|
|
Loading…
Reference in New Issue