Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

162
Vistas
Authorize requests for multiple roles for different URLs

I'm trying to implement different roles in my application and I cannot achieve to include/exclude URLs for different roles.

For example, I want that users with role ROLE_USER and anonymous users can access to the following endpoints:

 String[] publiclyAccessibleUris = new String[]{ "/home", "/user", "/profile/*", "location/**", "/products/*" };

And I want that users with role ROLE_DENIED can only access to the following endpoints:

 String[] publiclyAccessibleUrisForDenied = new String[]{ "/home", "/profile/*" };

Here is my code:

 http
     .sessionManagement()
         .sessionCreationPolicy(STATELESS)
         .and()
     .anonymous()
         .and()
     .authorizeRequests()
         .antMatchers(publiclyAccessibleUris).hasRole("USER")
         .anyRequest().permitAll()
         .antMatchers(publiclyAccessibleUrisForDenied).hasRole("DENIED")
         .anyRequest().permitAll()
         .antMatchers("/**").hasRole("DENIED")
         .anyRequest().denyAll();

How can I achieve this?

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Finally i solved like the following way in order to allow all urls for all users with anon o ROLE_USER, and for ROLE_SUSPENDED i set wich one i want to.

    http
            .sessionManagement().sessionCreationPolicy(STATELESS)
    .and()
            .anonymous()
    .and()
            .authorizeRequests()
                    .antMatchers(publiclyAccessibleUris).permitAll()
                    .antMatchers(publiclyAccessibleUrisForDenied).access("hasRole('ROLE_DENIED') or hasAuthority('DENIED') OR hasRole('ROLE_USER') or hasAuthority('USER')")
                    .anyRequest().access("hasRole('ROLE_USER') or hasAuthority('USER')");
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda