1 | package edu.gjoko.schedlr.config;
|
---|
2 |
|
---|
3 | import edu.gjoko.schedlr.services.PostgresUserDetailsService;
|
---|
4 | import lombok.AllArgsConstructor;
|
---|
5 | import org.springframework.context.annotation.Bean;
|
---|
6 | import org.springframework.context.annotation.Configuration;
|
---|
7 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
8 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
9 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
10 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
---|
11 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
12 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
13 | import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
---|
14 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
---|
15 | import org.springframework.security.web.authentication.logout.HeaderWriterLogoutHandler;
|
---|
16 | import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
|
---|
17 | import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
---|
18 | import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter;
|
---|
19 |
|
---|
20 | import static org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive.*;
|
---|
21 |
|
---|
22 | @Configuration
|
---|
23 | @EnableWebSecurity
|
---|
24 | @AllArgsConstructor
|
---|
25 | public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
26 |
|
---|
27 | private static final ClearSiteDataHeaderWriter.Directive[] SOURCE =
|
---|
28 | {CACHE, COOKIES, STORAGE, EXECUTION_CONTEXTS};
|
---|
29 |
|
---|
30 | private final PostgresUserDetailsService userDetailsService;
|
---|
31 |
|
---|
32 | private final BCryptPasswordEncoder passwordEncoder;
|
---|
33 |
|
---|
34 | private final AuthenticationFailureHandler authenticationFailureHandler;
|
---|
35 |
|
---|
36 | @Bean
|
---|
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()
|
---|
49 | .disable()
|
---|
50 | .httpBasic()
|
---|
51 | .authenticationEntryPoint(new AppAuthenticationEntryPoint())
|
---|
52 | .and()
|
---|
53 | .addFilterAfter(new AppFilter(userDetailsService), BasicAuthenticationFilter.class)
|
---|
54 | .formLogin()
|
---|
55 | .loginPage("/login")
|
---|
56 | .loginProcessingUrl("/login")
|
---|
57 | .failureHandler(authenticationFailureHandler)
|
---|
58 | .defaultSuccessUrl("/login")
|
---|
59 | .and()
|
---|
60 | .logout(logout -> logout
|
---|
61 | .logoutUrl("/logout")
|
---|
62 | .logoutSuccessUrl("/login")
|
---|
63 | .addLogoutHandler(new SecurityContextLogoutHandler())
|
---|
64 | .addLogoutHandler(new HeaderWriterLogoutHandler(new ClearSiteDataHeaderWriter(SOURCE))))
|
---|
65 | .authorizeRequests()
|
---|
66 | .antMatchers("/login").permitAll()
|
---|
67 | .antMatchers("/logout").permitAll()
|
---|
68 | .antMatchers("/register_customer").permitAll()
|
---|
69 | .antMatchers("/register_business").permitAll()
|
---|
70 | .antMatchers("/api/nomenclature/*").permitAll()
|
---|
71 | .antMatchers("/api/user/me").permitAll()
|
---|
72 | .antMatchers("/api/business").permitAll()
|
---|
73 | .antMatchers("/api/appointment").permitAll()
|
---|
74 | .antMatchers("/homepage").permitAll()
|
---|
75 | .antMatchers("/css/**").permitAll()
|
---|
76 | .antMatchers("/js/**").permitAll()
|
---|
77 | .antMatchers("/anonymous*").anonymous()
|
---|
78 | .anyRequest()
|
---|
79 | .fullyAuthenticated();
|
---|
80 | }
|
---|
81 | }
|
---|