package edu.gjoko.schedlr.config; import edu.gjoko.schedlr.services.PostgresUserDetailsService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.authentication.AuthenticationManager; 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 org.springframework.security.config.core.GrantedAuthorityDefaults; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; @Configuration @EnableWebSecurity public class AppSecurityConfig extends WebSecurityConfigurerAdapter { private final PostgresUserDetailsService userDetailsService; private final BCryptPasswordEncoder passwordEncoder; private final AuthenticationSuccessHandler authenticationSuccessHandler; public AppSecurityConfig(PostgresUserDetailsService userDetailsService, BCryptPasswordEncoder passwordEncoder, AuthenticationSuccessHandler authenticationSuccessHandler) { this.userDetailsService = userDetailsService; this.passwordEncoder = passwordEncoder; this.authenticationSuccessHandler = authenticationSuccessHandler; } @Bean public AuthenticationManager customAuthenticationManager() throws Exception { return authenticationManager(); } @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf() .disable() .authorizeRequests() .antMatchers("/login*").permitAll() .antMatchers("/css/**").permitAll() .antMatchers("/anonymous*").anonymous() .anyRequest() .fullyAuthenticated() .and() .httpBasic() .authenticationEntryPoint(new AppAuthenticationEntryPoint()) .and() .addFilterBefore(new AppFilter(), BasicAuthenticationFilter.class) .formLogin() .loginPage("/login") .loginProcessingUrl("/login") .successHandler(authenticationSuccessHandler); } }