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

Last change on this file since 8bcd64c was 8bcd64c, checked in by Gjoko Kostadinov <gjoko.kostadinov@…>, 15 months ago

Add admin functionality and business admin functionality.

  • Property mode set to 100644
File size: 3.1 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.config.core.GrantedAuthorityDefaults;
13import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
14import org.springframework.security.web.access.AccessDeniedHandler;
15import org.springframework.security.web.authentication.AuthenticationFailureHandler;
16import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
17import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
18
19@Configuration
20@EnableWebSecurity
21@AllArgsConstructor
22public 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}
Note: See TracBrowser for help on using the repository browser.