How to Allow Anonymous Access to All URLs Except One in Spring Security

Question

How can I configure Spring Security to allow anonymous access to all URLs except for a specific one?

   @Override
   protected void configure(HttpSecurity http) throws Exception {
       http.authorizeRequests()
           .antMatchers("/**").permitAll() // Allow access to all URLs
           .antMatchers("/employee/me").authenticated() // Protect this URL
           .and()
           .csrf().disable();
   }

Answer

Configuring Spring Security to allow access to all URLs except one specific URL can simplify your security management significantly. This configuration enables anonymous access across your application while securing only the URL you wish to protect.

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/**").permitAll() // Allow access to all URLs
            .antMatchers("/employee/me").authenticated() // Protect this URL
            .and()
            .csrf().disable();
    }
}

Causes

  • Complex code when manually specifying permissions for every URL.
  • The need for frequent modifications whenever new URLs are added.

Solutions

  • Use the 'antMatchers' method to specify the protected URL while allowing all others to be accessed anonymously.
  • Implement a simpler Spring Security configuration that achieves the desired outcome without overwhelming complexity.

Common Mistakes

Mistake: Not using antMatchers correctly for the specific secured URL.

Solution: Ensure you specify the URL correctly in antMatchers to protect it, while permitting all others.

Mistake: Forgetting to disable CSRF when doing public APIs.

Solution: When working with APIs, consider using csrf().disable() if CSRF protection is not needed.

Helpers

  • Spring Security
  • allow all URLs except one
  • Spring Security configuration
  • anonymous access
  • Java Spring Security

Related Questions

⦿How to Enumerate IP Addresses of All Enabled Network Interface Cards (NICs) in Java?

Discover how to list all enabled NICs and their IP addresses in Java without external dependencies. Learn with code examples.

⦿How to Convert a Checkstyle Configuration to an Eclipse Formatter Configuration?

Learn how to convert Checkstyle XML configuration files into Eclipse formatter settings including tools and techniques for seamless integration.

⦿How to Read Response Headers Using RestTemplate in Java

Learn how to access response headers when using RestTemplate in Java with detailed steps and code examples.

⦿How to Fix the 'Unable to Compute Hash of classes.jar' Error in Android Release Build?

Learn how to resolve the Unable to compute hash of classes.jar error when building an Android app including troubleshooting tips and Gradle configurations.

⦿How to Overlay Text on BufferedImage Using Graphics2D in Java?

Learn how to overlay text on a BufferedImage using Graphics2D in Java. Stepbystep guide with code snippets and common mistakes.

⦿How to Integrate an Angular 2 Frontend with a Java Maven Backend into a Single WAR File?

Learn how to seamlessly integrate an Angular 2 application with a Java Maven Web Application to produce a single WAR file for deployment.

⦿What Is the Difference Between Java System Properties and Environment Variables?

Explore the differences between Java system properties and environment variables including usage behavior in different environments and how to access them.

⦿How to Implement a Java Constructor for Mathematical Functions

Learn how to design a Java constructor to return the square root and inverse of a number using objectoriented principles.

⦿How to Locate the @Inject Annotation Jar for MVC Unit Testing

Discover where to find the Inject jar for MVC unit testing along with installation instructions and troubleshooting tips.

⦿A Simple Java Thread Example Demonstrating Concurrent Execution

Learn how to create and run multiple threads in Java with a clear example demonstrating concurrent execution of threads.

© Copyright 2025 - CodingTechRoom.com