package bg.car_wash.configurations.security;

import bg.car_wash.areas.user.services.UserService;
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.method.configuration.EnableGlobalMethodSecurity;
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 org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
	private UserService userService;

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(this.userService).passwordEncoder(getBCryptPasswordEncoder());
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http
				.authorizeRequests()
				.antMatchers("/", "/user/register", "/bootstrap/**", "/jquery/**").permitAll()
				.antMatchers("/user/status").access("hasRole('ADMIN') OR hasRole('DIRECTOR')")
                .antMatchers("/user/**").access("hasRole('WORKER') OR hasRole('PAYMASTER')")
				.antMatchers("/car/add").access("hasRole('WORKER') OR hasRole('PAYMASTER')")
//                .antMatchers("/admin/**").hasRole("ADMIN")
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage("/user/login").permitAll()
                .usernameParameter("email")
                .passwordParameter("password")
//                .and()
//                .rememberMe()
//                .rememberMeCookieName("RememberMeFromLecture")
//                .rememberMeParameter("rememberMe")
//                .key("GolqmTaina")
//                .tokenValiditySeconds(1000)
                .and()
                .logout().logoutSuccessUrl("/user/logout").permitAll()
                .and()
                .exceptionHandling().accessDeniedPage("/unauthorized")
				.and()
				.csrf().disable();
	}

	@Bean
	public BCryptPasswordEncoder getBCryptPasswordEncoder() {
		return new BCryptPasswordEncoder();
	}
}

@@ -1535,138 +1535,87 @@
 %22).a
-ccess(%22hasRole('WORKER') OR hasRole('PAYMASTER')%22)%0A%09%09%09%09.antMatchers(%22/car/add%22).access(%22hasRole('WORKER') OR hasRole('PAYMASTER')%22
+uthenticated()%0A%09%09%09%09.antMatchers(%22/car/all-cars%22, %22/car/add-car%22).authenticated(
 )%0A//

update spring security configuration