Skip to content
Snippets Groups Projects
Select Git revision
  • 084b767070cdc1deff5d681c69e63c8a85bd100e
  • main default protected
  • release
  • OZG-7856_schadcode_scanner
  • ci-pipeline
  • OZG-7526-signatur-nicht-uebernommen
  • OZG-6223-zip-download-bug
  • OZG-7367-tooltip-extension
  • OZG-7023-OZG-6956-E2E-externe-Stellen
  • OZG-6238-npm-durch-pnpm-ersetzen
  • release-admin
  • release-info
  • OZG-6700-admin-feature-toggle
  • E2E-Updates
  • OZG-7047-tooltips
  • OZG-6957-e2e-fachstellen-oe-daten
  • OZG-7006-ZuarbeitAnfragen
  • temp_OZG-7027
  • unit-tests-hotfix
  • OZG-6731-POC-keycloakResourceService-with-multiple-stateResources
  • e2e-add-zufi-version
  • 2.26.0
  • 2.25.0
  • 2.24.2
  • 2.24.1
  • 2.24.0
  • 2.23.0
  • 2.22.0
  • 2.21.0
  • 2.20.0
  • 2.21.0-SNAPSHOT
  • 2.19.0
  • 2.18.0
  • 2.17.1
  • 1.3.0
  • release-admin-1.3.0
  • release-info-1.3.0
  • 2.17.0
  • 2.16.0
  • 2.15.0
  • release-admin-1.1.0
41 results

button-with-spinner.e2e.component.ts

Blame
  • SecurityConfiguration.java 4.21 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 java.net.URI;
    import java.util.List;
    
    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.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    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.annotation.web.configurers.AbstractHttpConfigurer;
    import org.springframework.security.config.http.SessionCreationPolicy;
    import org.springframework.security.core.AuthenticationException;
    import org.springframework.security.oauth2.core.oidc.StandardClaimNames;
    import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter;
    import org.springframework.security.web.SecurityFilterChain;
    import org.springframework.web.ErrorResponse;
    import org.springframework.web.servlet.function.ServerResponse;
    
    import lombok.RequiredArgsConstructor;
    
    @Configuration
    @EnableMethodSecurity(securedEnabled = true)
    @EnableWebSecurity
    @RequiredArgsConstructor
    public class SecurityConfiguration {
    
    	private final List<HttpMessageConverter<?>> converters;
    
    	@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(AbstractHttpConfigurer::disable);
    
    		http.exceptionHandling(eh -> eh.authenticationEntryPoint((request, response, authException) ->
    				errorServerResponse(authException, request.getRequestURI())
    						.writeTo(request, response, () -> converters)));
    
    		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();
    	}
    
    	private ServerResponse errorServerResponse(AuthenticationException ex, String requestUri) {
    		return ServerResponse
    				.from(ErrorResponse
    						.builder(ex, HttpStatus.UNAUTHORIZED, ex.getLocalizedMessage())
    						.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_PROBLEM_JSON_VALUE)
    						.instance(URI.create(requestUri))
    						.header(HttpHeaders.WWW_AUTHENTICATE, "Bearer realm=\"Restricted Content\"")
    						.build());
    	}
    
    	@Bean
    	JwtAuthenticationConverter jwtAuthenticationConverter() {
    		var jwtConverter = new JwtAuthenticationConverter();
    		jwtConverter.setJwtGrantedAuthoritiesConverter(jwt -> List.of(() -> "ROLE_USER"));
    		jwtConverter.setPrincipalClaimName(StandardClaimNames.PREFERRED_USERNAME);
    		return jwtConverter;
    	}
    
    }