Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring security multiple hasIPAddress antMatchers

I have the following spring security configuration snippet:

http
   .authorizeRequests()
   .antMatchers("/tokens").hasIpAddress("10.0.0.0/16")
   ....

This works, but I would also like to grant access to "/tokens" from 127.0.0.1. I was hoping something along the lines of the following would work, but it doesn't:

http
   .authorizeRequests()
   .antMatchers("/tokens").hasIpAddress("10.0.0.0/16").hasIpAddress("127.0.0.1/32")
   ....
like image 896
MarcF Avatar asked Feb 03 '15 15:02

MarcF


People also ask

What is anyRequest () authenticated ()?

anyRequest(). authenticated() is that any request must be authenticated otherwise my Spring app will return a 401 response.

What are antMatchers in Spring Security?

The antMatchers() is a Springboot HTTP method used to configure the URL paths from which the Springboot application security should permit requests based on the user's roles. The antmatchers() method is an overloaded method that receives both the HTTP request methods and the specific URLs as its arguments.

Can I have multiple WebSecurityConfigurerAdapter?

When using Java configuration, the way to define multiple security realms is to have multiple @Configuration classes that extend the WebSecurityConfigurerAdapter base class – each with its own security configuration. These classes can be static and placed inside the main config.

What is the difference between hasAuthority and hasRole?

The main difference is that roles have special semantics. Starting with Spring Security 4, the 'ROLE_' prefix is automatically added (if it's not already there) by any role related method. So hasAuthority('ROLE_ADMIN') is similar to hasRole('ADMIN') because the 'ROLE_' prefix gets added automatically.


2 Answers

http
    .authorizeRequests()
    .antMatchers("/tokens").access(
            "hasIpAddress('10.0.0.0/16') or hasIpAddress('127.0.0.1/32')")
....
like image 64
Milanka Avatar answered Oct 18 '22 03:10

Milanka


Try to set this configuration in the spring security configuration file like this

<http auto-config="true" use-expressions="true">
<intercept-url pattern="/tokens**" access="hasIpAddress('10.0.0.0/16') or hasIpAddress('127.0.0.1/32')" />
</http>
like image 4
aimadbk Avatar answered Oct 18 '22 02:10

aimadbk