Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!