A lot of resources and stackoverflow questions that I've viewed provide answers to using .xml
files:
IP filter using Spring Security
http://websystique.com/spring-security/spring-security-4-method-security-using-preauthorize-postauthorize-secured-el/
http://docs.spring.io/spring-security/site/docs/3.0.x/reference/appendix-namespace.html#nsa-gms
All that I would like to know is if it's possible to whitelist an IP address range using Spring Security without using XML configs?
Below is a simple method in my controller:
@RequestMapping(value = "/makeit", method = RequestMethod.GET)
@ResponseBody
//@PreAuthorize("hasIpAddress('192.168.0.0/16')")
public String requestData() {
return "youve made it";
}
I've created a separate class for the security config but it doesn't have much, I just created it for the EnableGlobalMethodSecurity
annotation - so that I can use the @PreAuthorize
annotation (from an answer here: @PreAuthorize annotation not working spring security).
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http
.authorizeRequests()
.anyRequest().access("hasIpAddress('0.0.0.0/0')");
/*http
.authorizeRequests()
.anyRequest().hasIpAddress("0.0.0.0/0");*/
/*http
.authorizeRequests()
.antMatchers("/**").hasIpAddress("0.0.0.0/0");*/
/*http
.authorizeRequests()
.antMatchers("/**").access("hasIpAddress('0.0.0.0/0')");*/
/*http
.authorizeRequests()
.anyRequest().access("hasIpAddress('0.0.0.0/0')");*/
}
}
However, when I tried, it responded with (through POSTMAN):
{
"timestamp": 1486743507520,
"status": 401,
"error": "Unauthorized",
"message": "Full authentication is required to access this resource",
"path": "/makeit"
}
Additional facts:
My IP address is in this range. And I'm using Spring release 1.3.1 (Spring Security is 4.0.3, I believe).