Skip to content
Snippets Groups Projects
Select Git revision
  • e770ea4a9755d9c715e9eb08f0005b0947d4ec37
  • main default protected
  • release
  • OZG-8252-gitlab-pipeline
  • OZG-7774-E2E
  • OZG-5120-PoC-Native-Image
  • 1.11.0
  • 1.10.0
  • 1.9.0
  • 1.8.0
  • 1.7.0
  • 1.6.0
  • 1.5.0
  • 1.4.0
  • 1.3.0
  • 1.2.1
  • 1.2.0
  • 1.1.1
  • 1.1.0
  • 1.0.0
  • 0.8.0
  • 0.7.0
  • 0.6.0
  • 0.5.0
  • 0.4.0
  • 0.3.0
26 results

SecurityConfiguration.java

Blame
  • SecurityConfiguration.java 2.96 KiB
    /*
     * Copyright (C) 2024 Das Land Schleswig-Holstein vertreten durch das
     * Ministerium für Energiewende, Klimaschutz, Umwelt und Natur Zentrales
     * IT-Management
     * 
     * Lizenziert unter der EUPL, Version 1.2 oder - sobald diese von der
     * Europäischen Kommission genehmigt wurden - Folgeversionen der EUPL
     * ("Lizenz"); Sie dürfen dieses Werk ausschließlich gemäß dieser Lizenz nutzen.
     * Eine Kopie der Lizenz finden Sie hier:
     * 
     * https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
     * 
     * Sofern nicht durch anwendbare Rechtsvorschriften gefordert oder in
     * schriftlicher Form vereinbart, wird die unter der Lizenz verbreitete Software
     * "so wie sie ist", OHNE JEGLICHE GEWÄHRLEISTUNG ODER BEDINGUNGEN -
     * ausdrücklich oder stillschweigend - verbreitet. Die sprachspezifischen
     * Genehmigungen und Beschränkungen unter der Lizenz sind dem Lizenztext zu
     * entnehmen.
     */
    package de.ozgcloud.admin.security;
    
    import org.apache.http.HttpHeaders;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.HttpMethod;
    import org.springframework.http.HttpStatus;
    import org.springframework.security.config.Customizer;
    import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
    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;
    
    @Configuration
    @EnableMethodSecurity(securedEnabled = true)
    @EnableWebSecurity
    public class SecurityConfiguration {
    
    	@Bean
    	SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
    
    		// Configure a resource server with JWT decoder
    		http.oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()));
    		// State-less session (state in access-token only)
    		http.sessionManagement(sm -> sm.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
    
    		// Disable CSRF because of state-less session-management
    		http.csrf(csrf -> csrf.disable());
    
    		// Return 401 (unauthorized) instead of 302 (redirect to login) when
    		// authorization is missing or invalid
    		http.exceptionHandling(eh -> eh.authenticationEntryPoint((request, response, authException) -> {
    			response.addHeader(HttpHeaders.WWW_AUTHENTICATE, "Bearer realm=\"Restricted Content\"");
    			response.sendError(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase());
    		}));
    
    		http.authorizeHttpRequests(requests -> requests
    				.requestMatchers(HttpMethod.GET, "/api/environment").permitAll()
    				.requestMatchers("/api").authenticated()
    				.requestMatchers("/api/**").authenticated()
    				.requestMatchers("/actuator").permitAll()
    				.requestMatchers("/actuator/**").permitAll()
    				.requestMatchers("/configserver/**").permitAll()
    				.anyRequest().denyAll());
    
    		return http.build();
    	}
    
    }