package org.schoellerfamily.gedbrowser;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

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.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity;

/**
 * @author Dick Schoeller
 */
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    /**
     * {@inheritDoc}
     */
    @Override
    protected final void configure(final HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/surnames?db=schoeller&letter=A")
                .permitAll()
                .and()
            .logout()
                .deleteCookies("JSESSIONID")
                .invalidateHttpSession(true)
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .permitAll();
    }

    /**
     * @param auth the authentication manager builder
     * @throws Exception if there is a problem
     */
    @Autowired
    public final void configureGlobal(final AuthenticationManagerBuilder auth)
            throws Exception {
        final InMemoryUserDetailsManagerConfigurer<
            AuthenticationManagerBuilder> configurer =
            auth.inMemoryAuthentication();
        final Set<User> users = readUserFile();
        for (final User user : users) {
            configurer.withUser(user.getUsername())
                    .password(user.getPassword()).roles(user.getRoles());
        }
    }

    /**
     * @return the set of users from the user file
     */
    private Set<User> readUserFile() {
        final String userFile = "/var/lib/gedbrowser/userFile.csv";
        final Set<User> set = new HashSet<>();
        try (BufferedReader br = new BufferedReader(new FileReader(userFile))) {
            String line;
            while ((line = br.readLine()) != null) {
                final String[] userFields = line.split(",");
                final User user = buildUser(userFields);
                set.add(user);
            }
        } catch (IOException e) {
            final User user = new User();
            user.setUsername("guest");
            user.setPassword("guest");
            user.addRole("USER");
            set.add(user);
        }
        return set;
    }

    /**
     * @param userFields string array from the reader
     * @return built user
     */
    private User buildUser(final String[] userFields) {
        final User user = new User();
        int i = 0;
        user.setUsername(userFields[i++]);
        user.setFirstname(userFields[i++]);
        user.setLastname(userFields[i++]);
        user.setEmail(userFields[i++]);
        user.setPassword(userFields[i++]);
        for (; i < userFields.length; i++) {
            user.addRole(userFields[i]);
        }
        return user;
    }
}

@@ -84,22 +84,27 @@
 .io.File
-Reader
+InputStream
 ;%0Aimport
@@ -121,24 +121,81 @@
 OException;%0A
+import java.io.InputStreamReader;%0Aimport java.io.Reader;%0A
 import java.
@@ -2585,32 +2585,126 @@
 ry (
-Buffered
+%0A                final FileInputStream fis = new FileInputStream(userFile);%0A                final 
 Reader 
-b
+reade
 r = new 
 Buff
@@ -2703,48 +2703,127 @@
 new 
-BufferedReader(new FileReader(userFile))
+InputStreamReader(fis, %22UTF-8%22);%0A                final BufferedReader br = new BufferedReader(reader);%0A                
 ) %7B%0A

Fix a findbugs problem There is another new one that I am currently tweaking thresholds to accomodate.