package edu.gjoko.schedlr.config; import edu.gjoko.schedlr.services.PostgresUserDetailsService; import lombok.AllArgsConstructor; 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.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.web.authentication.AuthenticationFailureHandler; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.security.web.authentication.logout.HeaderWriterLogoutHandler; import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler; import org.springframework.security.web.authentication.www.BasicAuthenticationFilter; import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter; import static org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive.*; @Configuration @EnableWebSecurity @AllArgsConstructor public class AppSecurityConfig extends WebSecurityConfigurerAdapter { private static final ClearSiteDataHeaderWriter.Directive[] SOURCE = {CACHE, COOKIES, STORAGE, EXECUTION_CONTEXTS}; private final PostgresUserDetailsService userDetailsService; private final BCryptPasswordEncoder passwordEncoder; private final AuthenticationSuccessHandler authenticationSuccessHandler; private final AuthenticationFailureHandler authenticationFailureHandler; @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() .httpBasic() .authenticationEntryPoint(new AppAuthenticationEntryPoint()) .and() .addFilterAfter(new AppFilter(userDetailsService), BasicAuthenticationFilter.class) .formLogin() .loginPage("/login") .loginProcessingUrl("/login") .successHandler(authenticationSuccessHandler) .failureHandler(authenticationFailureHandler) .defaultSuccessUrl("/homepage") .and() .logout(logout -> logout .logoutUrl("/logout") .logoutSuccessUrl("/login") .addLogoutHandler(new SecurityContextLogoutHandler()) .addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE)))) .authorizeRequests() .antMatchers("/login").permitAll() .antMatchers("/logout").permitAll() .antMatchers("/register_customer").permitAll() .antMatchers("/register_business").permitAll() .antMatchers("/api/nomenclatures/*").permitAll() .antMatchers("/api/user/me").permitAll() .antMatchers("/api/business").permitAll() .antMatchers("/api/appointment").permitAll() .antMatchers("/homepage").permitAll() .antMatchers("/css/**").permitAll() .antMatchers("/js/**").permitAll() .antMatchers("/anonymous*").anonymous() .anyRequest() .fullyAuthenticated(); } }