source: src/main/java/edu/gjoko/schedlr/config/AppSecurityConfig.java

Last change on this file was 53765dd, checked in by gjoko kostadinov <gjokokostadinov@…>, 6 months ago

Fix bugs.

  • Property mode set to 100755
File size: 3.8 KB
Line 
1package edu.gjoko.schedlr.config;
2
3import edu.gjoko.schedlr.services.PostgresUserDetailsService;
4import lombok.AllArgsConstructor;
5import org.springframework.context.annotation.Bean;
6import org.springframework.context.annotation.Configuration;
7import org.springframework.security.authentication.AuthenticationManager;
8import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
9import org.springframework.security.config.annotation.web.builders.HttpSecurity;
10import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
11import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
12import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
13import org.springframework.security.web.authentication.AuthenticationFailureHandler;
14import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
15import org.springframework.security.web.authentication.logout.HeaderWriterLogoutHandler;
16import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
17import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
18import org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter;
19
20import static org.springframework.security.web.header.writers.ClearSiteDataHeaderWriter.Directive.*;
21
22@Configuration
23@EnableWebSecurity
24@AllArgsConstructor
25public 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}
Note: See TracBrowser for help on using the repository browser.