Cómo reemplazar el papel por un punto final específico en la primavera de Seguridad?

Coffemanz:

Soy la creación de la configuración de seguridad con la ayuda de la primavera de Seguridad.

Estoy interesado si es posible anular un rol específico para un punto final más con la ayuda de HttpSecurity objeto.

Digamos que tengo algo como esto:

httpSecurity
               .authenticationProvider(authenticationProvider)
               .authorizeRequests()
               .antMatchers("/api/v1/**").hasRole("ADMIN")

Y yo quiero tener otro papel para un determinado punto final más:

httpSecurity
                .authenticationProvider(authenticationProvider)
                .authorizeRequests()
                .antMatchers("/api/v1/**").hasRole("ADMIN")
                .antMatchers("/api/v1/specific").hasRole("ROLE_ONLY_FOR THIS_ENDPOINT")

Ans no funciona, aún requiere ADMIN papel para el punto final específico.

¿Es posible anular de alguna manera o excluir este punto final de una regla de papel más común?

Actualización 1 :

    @PreAuthorize("hasAuthority('ROLE_SPECIFIC')")
    @GetMapping("/specific")
    public ResponseEntity<TopDamagedComponentsResponse> getTopDamagedComponents(
            @RequestParam(required = true) String manufacturer,
            @RequestParam(required = true) String model,
            @RequestParam(required = true) String vehicleBodyType,
            @RequestParam(required = true) String vehicleType,
            @RequestParam(defaultValue = "25", required = false) Integer limit){
        List<ComponentDto> topDamagedComponents = service.getTopDamagedComponents(manufacturer, model, vehicleBodyType, vehicleType, limit);
        TopDamagedComponentsFilter filter = new TopDamagedComponentsFilter(manufacturer, model, vehicleBodyType, vehicleType);
        return new ResponseEntity<TopDamagedComponentsResponse>(new TopDamagedComponentsResponse(topDamagedComponents, topDamagedComponents.size(), filter), HttpStatus.OK);
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .addFilterBefore(tokenAuthFilter, BasicAuthenticationFilter.class)
                .antMatcher("/**")
                .authenticationProvider(authenticationProvider)
                .authorizeRequests()


                .antMatchers("/api/v1/**").hasRole("ADMIN")

                .antMatchers("/v2/api-docs", "/swagger-resources/configuration/ui", "/swagger-resources", "/swagger-resources/configuration/security", "/swagger-ui.html", "/webjars/**").permitAll()

                .antMatchers("/actuator/**").permitAll()

                .and()
                  .sessionManagement()
                      .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        http.csrf().disable();


    }

He tratado de usar @PreAuthorizepero todavía no funciona.

Coffemanz:

Gracias @RG para darse cuenta de un error tipográfico. Me estaba perdiendo /en el comienzo de mi punto final ( api/v1/specific), era la siguiente:

httpSecurity
                .authenticationProvider(authenticationProvider)
                .authorizeRequests()
                .antMatchers("api/v1/specific").hasRole("ROLE_ONLY_FOR THIS_ENDPOINT")
                .antMatchers("/api/v1/**").hasRole("ADMIN")

Después de añadir /y poner reglas en el orden correcto en la cadena (más los específicos deberían ir primero y más las generales deben estar en el extremo de la cadena), funciona como se esperaba: un parámetro específico requiere sólo un papel específico y todos los demás en virtud de /api/v1/**requerir un rol de administrador.

Por último se ve de esta manera:

httpSecurity
                .authenticationProvider(authenticationProvider)
                .authorizeRequests()
                .antMatchers("/api/v1/specific").hasRole("ROLE_ONLY_FOR THIS_ENDPOINT")
                .antMatchers("/api/v1/**").hasRole("ADMIN")

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=362479&siteId=1
Recomendado
Clasificación