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.config.core.GrantedAuthorityDefaults;
|
---|
13 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
14 | import org.springframework.security.web.access.AccessDeniedHandler;
|
---|
15 | import org.springframework.security.web.authentication.AuthenticationFailureHandler;
|
---|
16 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
---|
17 | import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
|
---|
18 |
|
---|
19 | @Configuration
|
---|
20 | @EnableWebSecurity
|
---|
21 | @AllArgsConstructor
|
---|
22 | public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
23 |
|
---|
24 | private final PostgresUserDetailsService userDetailsService;
|
---|
25 |
|
---|
26 | private final BCryptPasswordEncoder passwordEncoder;
|
---|
27 |
|
---|
28 | private final AuthenticationSuccessHandler authenticationSuccessHandler;
|
---|
29 |
|
---|
30 | private final AuthenticationFailureHandler authenticationFailureHandler;
|
---|
31 |
|
---|
32 | @Bean
|
---|
33 | public AuthenticationManager customAuthenticationManager() throws Exception {
|
---|
34 | return authenticationManager();
|
---|
35 | }
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
---|
39 | auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | protected void configure(HttpSecurity http) throws Exception {
|
---|
44 | http.csrf()
|
---|
45 | .disable()
|
---|
46 | .httpBasic()
|
---|
47 | .authenticationEntryPoint(new AppAuthenticationEntryPoint())
|
---|
48 | .and()
|
---|
49 | .addFilterAfter(new AppFilter(userDetailsService), BasicAuthenticationFilter.class)
|
---|
50 | .formLogin()
|
---|
51 | .loginPage("/login")
|
---|
52 | .loginProcessingUrl("/login")
|
---|
53 | .successHandler(authenticationSuccessHandler)
|
---|
54 | .failureHandler(authenticationFailureHandler)
|
---|
55 | .defaultSuccessUrl("/homepage")
|
---|
56 | .and()
|
---|
57 | .authorizeRequests()
|
---|
58 | .antMatchers("/login").permitAll()
|
---|
59 | .antMatchers("/register_customer").permitAll()
|
---|
60 | .antMatchers("/register_business").permitAll()
|
---|
61 | .antMatchers("/api/nomenclatures/*").permitAll()
|
---|
62 | .antMatchers("/api/business").permitAll()
|
---|
63 | .antMatchers("/homepage").permitAll()
|
---|
64 | .antMatchers("/css/**").permitAll()
|
---|
65 | .antMatchers("/js/**").permitAll()
|
---|
66 | .antMatchers("/anonymous*").anonymous()
|
---|
67 | .anyRequest()
|
---|
68 | .fullyAuthenticated();
|
---|
69 | }
|
---|
70 | }
|
---|