Я только что закончил следовать тому, что казалось простым учебником, по адресу https://www.baeldung.com/spring-security-whitelist-ip-range Но я снова обнаружил, что хожу по кругу, пытаясь заставить его работать, и задавался вопросом, может ли кто-нибудь помочь.
Я пытаюсь настроить белый список IP-адресов для доступа к чему-либо в / admin.Тогда доступ к остальным путям может получить любой.
Я могу подтвердить, что точки останова 1,2 сработали, но важная точка останова 3 никогда не достигается.Все в / admin отказано в доступе.У меня нет других настроенных средств аутентификации.Кто-нибудь знает, что происходит?
Также есть SecurityConfig.java :
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private CustomIpAuthenticationProvider authenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 1
auth.authenticationProvider(authenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").authenticated()
.antMatchers("/**").permitAll()
.and()
.csrf().disable();
}
}
CustomIpAuthenticationProvider.java
@Component
public class CustomIpAuthenticationProvider implements AuthenticationProvider {
private Set<String> whitelist = new HashSet<String>();
public CustomIpAuthenticationProvider() {
// 2
whitelist.add("x.x.x.x");
}
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// 3
WebAuthenticationDetails details = (WebAuthenticationDetails) authentication.getDetails();
String userIp = details.getRemoteAddress();
if(! whitelist.contains(userIp)){
throw new BadCredentialsException("Invalid IP Address");
}
return authentication;
}
@Override
public boolean supports(Class<?> authentication) {
return true; // or false, doesn't make any difference
}
}