Home > Java > javaTutorial > How to Correctly Order Request Matchers in Spring Security for Role-Based Access Control?

How to Correctly Order Request Matchers in Spring Security for Role-Based Access Control?

Patricia Arquette
Release: 2024-12-02 04:16:10
Original
250 people have browsed it

How to Correctly Order Request Matchers in Spring Security for Role-Based Access Control?

Fixing Role Management in Spring Security

Your issue with role-based access control in Spring Security stems from the order of your request matchers. The matcher for any request should come after specific role-based matchers.

To resolve this and restrict admin access, modify your configuration as follows:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .csrf().disable()      
        .httpBasic()
            .and()
        .authorizeRequests()
            .antMatchers("/users/all").hasRole("admin")
            .anyRequest().authenticated() // Moved after role-based matcher
            .and()
        .formLogin()
            .and()
        .exceptionHandling().accessDeniedPage("/403");
}
Copy after login

With this configuration, requests to /users/all will require the admin role, while all other requests will require any authenticated user.

The above is the detailed content of How to Correctly Order Request Matchers in Spring Security for Role-Based Access Control?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template