package fi.oispakaljaa.karhu.config;

import fi.oispakaljaa.karhu.auth.JpaAuthenticationProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter;
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;

@Configuration
@EnableWebMvcSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/login").permitAll()
                .antMatchers("/signup").permitAll()
                .antMatchers("/public*").permitAll()
                .antMatchers("/templates*").permitAll()
                .antMatchers(HttpMethod.POST, "/api/bars").permitAll();

        http.formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/authenticate")
                .defaultSuccessUrl("/index")
                .usernameParameter("username")
                .passwordParameter("password")
                .permitAll();

        http.logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/login")
                .permitAll()
                .invalidateHttpSession(true);
    }

    @Configuration
    protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {

        @Autowired
        private JpaAuthenticationProvider jpaAuthenticationProvider;

        @Override
        public void init(AuthenticationManagerBuilder auth) throws Exception {
            auth.authenticationProvider(jpaAuthenticationProvider);
        }
    }
}

@@ -1273,229 +1273,54 @@
 rs%22)
-.permitAll();%0A%0A        http.formLogin()%0A                .loginPage(%22/login%22)%0A                .loginProcessingUrl(%22/authenticate%22)%0A                .defaultSuccessUrl(%22/index%22)%0A                .usernameParameter(%22username
+%0A                .hasAnyRole(%22USER%22, %22ADMIN
 %22)
+;
 %0A   
@@ -1328,78 +1328,31 @@
     
-        .passwordParameter(%22password%22)%0A                .permitAll
+http.csrf().disable
 ();%0A
-%0A
     
@@ -1443,21 +1443,16 @@
 ssUrl(%22/
-login
 %22)%0A     

Fix security issues