[cf9cdbf] | 1 | package edu.gjoko.schedlr.config;
|
---|
| 2 |
|
---|
[401a211] | 3 | import edu.gjoko.schedlr.services.PostgresUserDetailsService;
|
---|
[8bcd64c] | 4 | import lombok.AllArgsConstructor;
|
---|
[cf9cdbf] | 5 | import org.springframework.context.annotation.Bean;
|
---|
| 6 | import org.springframework.context.annotation.Configuration;
|
---|
[401a211] | 7 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
| 8 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
[cf9cdbf] | 9 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
[401a211] | 10 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
| 11 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
[cf9cdbf] | 12 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[8bcd64c] | 13 | import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
---|
[401a211] | 14 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
---|
[950fa0d] | 15 | import org.springframework.security.web.authentication.logout.HeaderWriterLogoutHandler;
|
---|
| 16 | import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
---|
[cf9cdbf] | 17 | import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
---|
[950fa0d] | 18 | import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter;
|
---|
| 19 |
|
---|
| 20 | import static org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive.*;
|
---|
[cf9cdbf] | 21 |
|
---|
| 22 | @Configuration
|
---|
[401a211] | 23 | @EnableWebSecurity
|
---|
[8bcd64c] | 24 | @AllArgsConstructor
|
---|
[401a211] | 25 | public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
| 26 |
|
---|
[950fa0d] | 27 | private static final ClearSiteDataHeaderWriter.Directive[] SOURCE =
|
---|
| 28 | {CACHE, COOKIES, STORAGE, EXECUTION_CONTEXTS};
|
---|
| 29 |
|
---|
[401a211] | 30 | private final PostgresUserDetailsService userDetailsService;
|
---|
| 31 |
|
---|
| 32 | private final BCryptPasswordEncoder passwordEncoder;
|
---|
| 33 |
|
---|
[8bcd64c] | 34 | private final AuthenticationFailureHandler authenticationFailureHandler;
|
---|
[cf9cdbf] | 35 |
|
---|
| 36 | @Bean
|
---|
[401a211] | 37 | public AuthenticationManager customAuthenticationManager() throws Exception {
|
---|
| 38 | return authenticationManager();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | @Override
|
---|
| 42 | protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
---|
| 43 | auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | @Override
|
---|
| 47 | protected void configure(HttpSecurity http) throws Exception {
|
---|
| 48 | http.csrf()
|
---|
[cf9cdbf] | 49 | .disable()
|
---|
[a436340] | 50 | .httpBasic()
|
---|
| 51 | .authenticationEntryPoint(new AppAuthenticationEntryPoint())
|
---|
| 52 | .and()
|
---|
[8bcd64c] | 53 | .addFilterAfter(new AppFilter(userDetailsService), BasicAuthenticationFilter.class)
|
---|
[a436340] | 54 | .formLogin()
|
---|
| 55 | .loginPage("/login")
|
---|
| 56 | .loginProcessingUrl("/login")
|
---|
[8bcd64c] | 57 | .failureHandler(authenticationFailureHandler)
|
---|
[77205be] | 58 | .defaultSuccessUrl("/login")
|
---|
[a436340] | 59 | .and()
|
---|
[950fa0d] | 60 | .logout(logout -> logout
|
---|
| 61 | .logoutUrl("/logout")
|
---|
| 62 | .logoutSuccessUrl("/login")
|
---|
| 63 | .addLogoutHandler(new SecurityContextLogoutHandler())
|
---|
| 64 | .addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE))))
|
---|
[401a211] | 65 | .authorizeRequests()
|
---|
[763289e] | 66 | .antMatchers("/login").permitAll()
|
---|
[950fa0d] | 67 | .antMatchers("/logout").permitAll()
|
---|
[204464d] | 68 | .antMatchers("/register_customer").permitAll()
|
---|
| 69 | .antMatchers("/register_business").permitAll()
|
---|
[53765dd] | 70 | .antMatchers("/api/nomenclature/*").permitAll()
|
---|
[950fa0d] | 71 | .antMatchers("/api/user/me").permitAll()
|
---|
[46fd0c7] | 72 | .antMatchers("/api/business").permitAll()
|
---|
[950fa0d] | 73 | .antMatchers("/api/appointment").permitAll()
|
---|
[763289e] | 74 | .antMatchers("/homepage").permitAll()
|
---|
[401a211] | 75 | .antMatchers("/css/**").permitAll()
|
---|
[044bd76] | 76 | .antMatchers("/js/**").permitAll()
|
---|
[401a211] | 77 | .antMatchers("/anonymous*").anonymous()
|
---|
| 78 | .anyRequest()
|
---|
[a436340] | 79 | .fullyAuthenticated();
|
---|
[cf9cdbf] | 80 | }
|
---|
| 81 | }
|
---|