whitelist ip range
This commit is contained in:
parent
3ccac06c46
commit
04d56b7ab3
@ -0,0 +1,12 @@
|
||||
package org.baeldung.ip;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
|
||||
|
||||
@SpringBootApplication
|
||||
public class IpApplication extends SpringBootServletInitializer {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(IpApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package org.baeldung.ip.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.security.authentication.AuthenticationProvider;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.AuthenticationException;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||
import org.springframework.security.web.authentication.WebAuthenticationDetails;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class CustomIpAuthenticationProvider implements AuthenticationProvider {
|
||||
|
||||
Set<String> whitelist = new HashSet<String>();
|
||||
|
||||
public CustomIpAuthenticationProvider() {
|
||||
super();
|
||||
whitelist.add("11.11.11.11");
|
||||
whitelist.add("127.0.0.1");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Authentication authenticate(Authentication auth) throws AuthenticationException {
|
||||
WebAuthenticationDetails details = (WebAuthenticationDetails) auth.getDetails();
|
||||
String userIp = details.getRemoteAddress();
|
||||
if(! whitelist.contains(userIp)){
|
||||
throw new BadCredentialsException("Invalid IP Address");
|
||||
}
|
||||
final String name = auth.getName();
|
||||
final String password = auth.getCredentials().toString();
|
||||
|
||||
if (name.equals("john") && password.equals("123")) {
|
||||
List<GrantedAuthority> authorities =new ArrayList<GrantedAuthority>();
|
||||
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
|
||||
return new UsernamePasswordAuthenticationToken(name, password, authorities);
|
||||
}
|
||||
else{
|
||||
throw new BadCredentialsException("Invalid username or password");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(Class<?> authentication) {
|
||||
return authentication.equals(UsernamePasswordAuthenticationToken.class);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.baeldung.ip.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||
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;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
private CustomIpAuthenticationProvider authenticationProvider;
|
||||
|
||||
@Override
|
||||
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth.inMemoryAuthentication().withUser("john").password("{noop}123").authorities("ROLE_USER");
|
||||
// auth.authenticationProvider(authenticationProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(final HttpSecurity http) throws Exception {
|
||||
// @formatter:off
|
||||
http.authorizeRequests()
|
||||
.antMatchers("/login").permitAll()
|
||||
// .antMatchers("/foos/**").hasIpAddress("11.11.11.11")
|
||||
.antMatchers("/foos/**").access("isAuthenticated() and hasIpAddress('11.11.11.11')")
|
||||
.anyRequest().authenticated()
|
||||
.and().formLogin().permitAll()
|
||||
.and().csrf().disable();
|
||||
// @formatter:on
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package org.baeldung.ip.config;
|
||||
|
||||
|
||||
//@Configuration
|
||||
//@EnableWebSecurity
|
||||
//@ImportResource({ "classpath:spring-security-ip.xml" })
|
||||
public class SecurityXmlConfig {
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package org.baeldung.ip.web;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.baeldung.custom.persistence.model.Foo;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
public class MainController {
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/foos/{id}")
|
||||
@ResponseBody
|
||||
public Foo findById(@PathVariable final long id, HttpServletRequest request) {
|
||||
return new Foo("Sample");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:security="http://www.springframework.org/schema/security"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<security:authentication-manager>
|
||||
<security:authentication-provider>
|
||||
<security:user-service>
|
||||
<security:user name="john" password="{noop}123" authorities="ROLE_USER" />
|
||||
</security:user-service>
|
||||
</security:authentication-provider>
|
||||
</security:authentication-manager>
|
||||
|
||||
<security:http>
|
||||
<security:form-login/>
|
||||
<security:intercept-url pattern="/login" access="permitAll()" />
|
||||
<security:intercept-url pattern="/foos/**" access="hasIpAddress('11.11.11.11')" />
|
||||
<security:intercept-url pattern="/**" access="isAuthenticated()" />
|
||||
</security:http>
|
||||
</beans>
|
@ -0,0 +1,27 @@
|
||||
package org.baeldung.web;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
public class IpLiveTest {
|
||||
|
||||
@Test
|
||||
public void givenUser_whenGetHomePage_thenOK() {
|
||||
final Response response = RestAssured.given().auth().form("john", "123").get("http://localhost:8082/");
|
||||
assertEquals(200, response.getStatusCode());
|
||||
assertTrue(response.asString().contains("Welcome"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserWithWrongIP_whenGetFooById_thenForbidden() {
|
||||
final Response response = RestAssured.given().auth().form("john", "123").get("http://localhost:8082/foos/1");
|
||||
assertEquals(403, response.getStatusCode());
|
||||
assertTrue(response.asString().contains("Forbidden"));
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user