Вы должны заявить в порядке, который вы хотите в конфигурации.
@Component
public class IPWhiteLister implements AuthenticationProvider {
Set<String> whitelist = new HashSet<String>();
public CustomIpAuthenticationProvider() {
whitelist.add("10.1.1.1");
whitelist.add("172.1.1.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");
}
return auth;
}
и следуйте инструкциям из https://www.baeldung.com/spring-security-multiple-auth-providers, чтобы использовать несколько поставщиков аутентификации.Вы также должны переопределить другой метод configure для построения AuthenticationManagerBuilder.Вы можете добавить несколько провайдеров аутентификации с помощью метода authenticationProvider.Это немного обманчиво, так как подпись не говорит, что она позволит вам добавить более одного поставщика аутентификации.
public class ApplicationSecurity extends WebSecurityConfigurerAdapter
{
Logger logger = LoggerFactory.getLogger(ApplicationSecurity.class);
@Autowired
private IPWhiteListFilter ipWhiteListFilter;
@Override
protected void configure(AuthenticationManagerBuilder auth){
auth.authenticationProvider(ipWhiteListFilter);
// FOLLOWING is just an example
auth.authenticationProvider(userNamePasswordAuthenticator)
}
...
Вместо следующей строки в вашем коде
.access("(hasIpAddress('xx.xx.xx.xx') and isAuthenticated()")
Просто используйте
.authenticated()