In my Spring Boot project I am trying to give access to several admin users with specific IP address.
Is it possible to map a single role to multiple IP addresses?
Here is the code from my security configuration which didn't work. (I am giving hard coded role name and ip addresses for simplicity)
@SuppressWarnings("ALL")
@Configuration
@EnableWebSecurity
public class MyWebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
List<String> ipAddresses = new ArrayList<>();
ipAddresses.add("127.0.0.1");
ipAddresses.add("192.168.1.0/24");
ipAddresses.add("0:0:0:0:0:0:0:1");
for (String ip : ipAddresses) {
http.authorizeRequests().
antMatchers("/admin" + "/**")
.access("hasRole('admin') and hasIpAddress('" + ip + "')");
}
}
//some other configurations
}
URL of my request: http://localhost:9595/admin/checkappeals/211
See Question&Answers more detail:os