All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.infusers.core.security.common.WebSecurityConfiguration Maven / Gradle / Ivy

There is a newer version: 2024.12.0008
Show newest version

package com.infusers.core.security.common;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import com.infusers.core.stats.users.ActiveUserService;
import com.infusers.core.util.InfusersUtility;

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration {	
	
    private final AuthenticationConfiguration authenticationConfiguration;
    private final ApplicationContext applicationContext;
    private final ActiveUserService activeUserService;    
    @Autowired
    private InfusersUtility infusersUtility;    
		
	private static final String[] AUTH_WHITELIST = { 
			"/v2/api-docs", 
			"/api/v2/api-docs", 
			"/swagger-resources", 
			"/swagger-resources/**",
			"/configuration/ui", 
			"/configuration/security", 
			"/v3/api-docs/**", 
			"/v3/api-docs/", 
			"/v3/api-docs", 
			"/swagger-ui/**",
			"/swagger-ui/",
			"/swagger-ui",
			"/swagger-ui.html",
			"/infusers/ws/hello",
			"/infusers/ws/topic/greetings",
			"/ws",
			"/ws/**",
			"/h2-console",
			"/infusers/user/login/social",
			"/oauth2/**",
			"/verify/**",
			"/infusers/user/verify",
			"/api/auth/**",
			"/infusers/api/version",
			"/infusers/quotes/**",
			"/infusers/platform/**",
			"/infusers/api/cloud-provider",
			"/actuator/**",
			"/actuator/",
			"/actuator",
			"/infusers/api/audit-spring-boot-dependencies",
			"/infusers/eng/insight-3p-lib",
			"/infusers/eng/insight-pom-file",
			"/infusers/sse/active-stats-stream",
			"/infusers/sse/active-requests-count",
			"/infusers/user/signup",
			"/webjars/**" };
	
	
    public WebSecurityConfiguration(AuthenticationConfiguration authenticationConfiguration, ApplicationContext ctx, InfusersUtility infusersUtility, ActiveUserService activeUserService) {
        this.authenticationConfiguration = authenticationConfiguration;
        this.applicationContext = ctx;
        this.infusersUtility = infusersUtility;     
        this.activeUserService = activeUserService;        
    }
    
	@Bean
	 public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Exception {
		
		httpSecurity
			.cors()
			.and()
			.csrf().disable()
			.authorizeHttpRequests()
            .requestMatchers(
                    Arrays.stream(AUTH_WHITELIST)
                        .map(AntPathRequestMatcher::new)
                        .toArray(AntPathRequestMatcher[]::new)
                ).permitAll()
				//.requestMatchers(AUTH_WHITELIST).permitAll()
				.anyRequest()
				.authenticated()
			.and()
			.addFilter(new AuthenticationFilter(authenticationManager(authenticationConfiguration), this.applicationContext, this.infusersUtility, this.activeUserService))
			.addFilter(new AuthorizationFilter(authenticationManager(authenticationConfiguration), infusersUtility, this.activeUserService))
			.sessionManagement()
			.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
			.and()
			.exceptionHandling().authenticationEntryPoint(new InfusersAuthenticationEntryPoint());
		    
		    return httpSecurity.build();
		    
	  }
	  
	  @Bean
	  public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfiguration) throws Exception {
	    return authConfiguration.getAuthenticationManager();
	  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy