// Path: spring-security-radius-login/src/main/java/com/pavelsklenar/radiuslogin/authentication/RadiusAuthenticationProvider.java
// public class RadiusAuthenticationProvider implements AuthenticationProvider {
// 
// 	private static final Logger logger = LoggerFactory.getLogger(RadiusAuthenticationProvider.class);
// 
// 	@Value("${com.pavelsklenar.radius.server}")
// 	private String serverConfigurationToken;
// 
// 	private List<NetworkAccessServer> clients = new ArrayList<>();
// 
// 	@PostConstruct
// 	public void initServers() {
// 		List<RadiusServer> servers = RadiusUtil.parseServerConfigurationToken(serverConfigurationToken);
// 		servers.forEach(it -> {
// 			clients.add(new NetworkAccessServer(it));
// 		});
// 	}
// 
// 	@Override
// 	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// 		String username = authentication.getName();
// 		RadiusPacket response = null;
// 		int attemptCount = 0;
// 		while (response == null && attemptCount++ < clients.size()) {
// 			response = authenticateInternally(clients.get(attemptCount - 1), username,
// 					authentication.getCredentials().toString());
// 		}
// 		if (response == null) {
// 			logger.warn("User {}, calling radius does not return any value.", username);
// 			return null;
// 		}
// 		if (response.getPacketType() == RadiusPacket.ACCESS_ACCEPT) {
// 			logger.info("User {} successfully authenticated using radius", username);
// 			return new UsernamePasswordAuthenticationToken(username, "", new ArrayList<>());
// 		} else {
// 			logger.warn("User {}, returned response {}", username, response);
// 			return null;
// 		}
// 	}
// 
// 	private RadiusPacket authenticateInternally(NetworkAccessServer client, String username, String password) {
// 		logger.info("Calling radius server to authenticate user {}", username);
// 		try {
// 			return client.authenticate(username, password);
// 		} catch (Exception e) {
// 			logger.error("Exception when calling remote radius server.", e);
// 			return null;
// 		}
// 	}
// 
// 	@Override
// 	public boolean supports(Class<?> authentication) {
// 		return authentication.equals(UsernamePasswordAuthenticationToken.class);
// 	}
// 
// }


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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;
import com.pavelsklenar.radiuslogin.authentication.RadiusAuthenticationProvider;
package com.pavelsklenar.radiuslogin.bootstrap;



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
    
    @Autowired
	public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
		auth.authenticationProvider(radiusAuthenticationProvider());
	}

	@Bean
// Path: spring-security-radius-login/src/main/java/com/pavelsklenar/radiuslogin/authentication/RadiusAuthenticationProvider.java
// public class RadiusAuthenticationProvider implements AuthenticationProvider {
// 
// 	private static final Logger logger = LoggerFactory.getLogger(RadiusAuthenticationProvider.class);
// 
// 	@Value("${com.pavelsklenar.radius.server}")
// 	private String serverConfigurationToken;
// 
// 	private List<NetworkAccessServer> clients = new ArrayList<>();
// 
// 	@PostConstruct
// 	public void initServers() {
// 		List<RadiusServer> servers = RadiusUtil.parseServerConfigurationToken(serverConfigurationToken);
// 		servers.forEach(it -> {
// 			clients.add(new NetworkAccessServer(it));
// 		});
// 	}
// 
// 	@Override
// 	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
// 		String username = authentication.getName();
// 		RadiusPacket response = null;
// 		int attemptCount = 0;
// 		while (response == null && attemptCount++ < clients.size()) {
// 			response = authenticateInternally(clients.get(attemptCount - 1), username,
// 					authentication.getCredentials().toString());
// 		}
// 		if (response == null) {
// 			logger.warn("User {}, calling radius does not return any value.", username);
// 			return null;
// 		}
// 		if (response.getPacketType() == RadiusPacket.ACCESS_ACCEPT) {
// 			logger.info("User {} successfully authenticated using radius", username);
// 			return new UsernamePasswordAuthenticationToken(username, "", new ArrayList<>());
// 		} else {
// 			logger.warn("User {}, returned response {}", username, response);
// 			return null;
// 		}
// 	}
// 
// 	private RadiusPacket authenticateInternally(NetworkAccessServer client, String username, String password) {
// 		logger.info("Calling radius server to authenticate user {}", username);
// 		try {
// 			return client.authenticate(username, password);
// 		} catch (Exception e) {
// 			logger.error("Exception when calling remote radius server.", e);
// 			return null;
// 		}
// 	}
// 
// 	@Override
// 	public boolean supports(Class<?> authentication) {
// 		return authentication.equals(UsernamePasswordAuthenticationToken.class);
// 	}
// 
// }


// Path: spring-security-radius-login/src/main/java/com/pavelsklenar/radiuslogin/bootstrap/WebSecurityConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
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;
import com.pavelsklenar.radiuslogin.authentication.RadiusAuthenticationProvider;

package com.pavelsklenar.radiuslogin.bootstrap;



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .permitAll()
                .and()
            .logout()
                .permitAll();
    }
    
    @Autowired
	public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
		auth.authenticationProvider(radiusAuthenticationProvider());
	}

	@Bean
	public RadiusAuthenticationProvider radiusAuthenticationProvider() {