Correct implementation

Report a typo

There is a program with three endpoints:

  • GET /get available to everyone.
  • POST /add available only to authenticated users.
  • PUT /update only available to authenticated users with the ROLE_MANAGER role.

Select the appropriate code snippet that strictly enforces such access rules.

Java
  1. @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(matcherRegistry -> matcherRegistry
                        .requestMatchers("/get").permitAll()
                        .requestMatchers("/add").authenticated()
                        .requestMatchers("/update").hasAuthority("ROLE_MANAGER")
                        .anyRequest().denyAll()
                )
                .httpBasic(Customizer.withDefaults())
                .build();
    }
  2. @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(matcherRegistry -> matcherRegistry
                        .requestMatchers(HttpMethod.GET, "/get").permitAll()
                        .requestMatchers(HttpMethod.POST, "/add").authenticated()
                        .requestMatchers(HttpMethod.PUT, "/update").hasAuthority("ROLE_MANAGER")
                        .anyRequest().denyAll()
                )
                .httpBasic(Customizer.withDefaults())
                .build();
    }
  3. @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(matcherRegistry -> matcherRegistry
                        .requestMatchers(HttpMethod.GET, "/get").permitAll()
                        .requestMatchers(HttpMethod.POST, "/add").authenticated()
                        .requestMatchers(HttpMethod.PUT, "/update").hasAuthority("MANAGER")
                        .anyRequest().denyAll()
                )
                .httpBasic(Customizer.withDefaults())
                .build();
    }
  4. @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests(matcherRegistry -> matcherRegistry
                        .requestMatchers("/get").permitAll()
                        .requestMatchers("/add").authenticated()
                        .requestMatchers("/update").hasRole("MANAGER")
                        .anyRequest().denyAll()
                )
                .httpBasic(Customizer.withDefaults())
                .build();
    }
Kotlin
  1. @Bean
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain =
        http
            .authorizeHttpRequests { matcherRegistry -> matcherRegistry
                .requestMatchers("/get").permitAll()
                .requestMatchers("/add").authenticated()
                .requestMatchers("/update").hasAuthority("ROLE_MANAGER")
                .anyRequest().denyAll()
            }
            .httpBasic(Customizer.withDefaults())
            .build()
    }
  2. @Bean
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain =
        http
            .authorizeHttpRequests { matcherRegistry -> matcherRegistry
                .requestMatchers(HttpMethod.GET, "/get").permitAll()
                .requestMatchers(HttpMethod.POST, "/add").authenticated()
                .requestMatchers(HttpMethod.PUT, "/update").hasAuthority("ROLE_MANAGER")
                .anyRequest().denyAll()
            }
            .httpBasic(Customizer.withDefaults())
            .build()
    }
  3. @Bean
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain =
        http
            .authorizeHttpRequests { matcherRegistry -> matcherRegistry
                .requestMatchers(HttpMethod.GET, "/get").permitAll()
                .requestMatchers(HttpMethod.POST, "/add").authenticated()
                .requestMatchers(HttpMethod.PUT, "/update").hasAuthority("MANAGER")
                .anyRequest().denyAll()
            }
            .httpBasic(Customizer.withDefaults())
            .build()
    }
  4. @Bean
    fun securityFilterChain(http: HttpSecurity): SecurityFilterChain =
        http
            .authorizeHttpRequests { matcherRegistry -> matcherRegistry
                .requestMatchers("/get").permitAll()
                .requestMatchers("/add").authenticated()
                .requestMatchers("/update").hasRole("MANAGER")
                .anyRequest().denyAll()
            }
            .httpBasic(Customizer.withDefaults())
            .build()
    }
Select one option from the list
___

Create a free account to access the full topic