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

Last change on this file since 401a211 was 401a211, checked in by Gjoko <goko_kostadinov@…>, 21 months ago

Fixing security configuration

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[cf9cdbf]1package edu.gjoko.schedlr.config;
2
[401a211]3import edu.gjoko.schedlr.services.PostgresUserDetailsService;
[cf9cdbf]4import org.springframework.context.annotation.Bean;
5import org.springframework.context.annotation.Configuration;
[401a211]6import org.springframework.security.authentication.AuthenticationManager;
7import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
[cf9cdbf]8import org.springframework.security.config.annotation.web.builders.HttpSecurity;
[401a211]9import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
10import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
[cf9cdbf]11import org.springframework.security.config.core.GrantedAuthorityDefaults;
12import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
[401a211]13import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
[cf9cdbf]14import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
15
16@Configuration
[401a211]17@EnableWebSecurity
18public class AppSecurityConfig extends WebSecurityConfigurerAdapter {
19
20 private final PostgresUserDetailsService userDetailsService;
21
22 private final BCryptPasswordEncoder passwordEncoder;
23
24 private final AuthenticationSuccessHandler authenticationSuccessHandler;
25
26 public AppSecurityConfig(PostgresUserDetailsService userDetailsService, BCryptPasswordEncoder passwordEncoder,
27 AuthenticationSuccessHandler authenticationSuccessHandler) {
28 this.userDetailsService = userDetailsService;
29 this.passwordEncoder = passwordEncoder;
30 this.authenticationSuccessHandler = authenticationSuccessHandler;
31 }
[cf9cdbf]32
33 @Bean
[401a211]34 public AuthenticationManager customAuthenticationManager() throws Exception {
35 return authenticationManager();
36 }
37
38 @Override
39 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
40 auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
41 }
42
43 @Override
44 protected void configure(HttpSecurity http) throws Exception {
45 http.csrf()
[cf9cdbf]46 .disable()
[401a211]47 .authorizeRequests()
48 .antMatchers("/login*").permitAll()
49 .antMatchers("/css/**").permitAll()
50 .antMatchers("/anonymous*").anonymous()
51 .anyRequest()
52 .fullyAuthenticated()
53 .and()
[cf9cdbf]54 .httpBasic()
55 .authenticationEntryPoint(new AppAuthenticationEntryPoint())
56 .and()
57 .addFilterBefore(new AppFilter(), BasicAuthenticationFilter.class)
58 .formLogin()
59 .loginPage("/login")
60 .loginProcessingUrl("/login")
[401a211]61 .successHandler(authenticationSuccessHandler);
[cf9cdbf]62 }
63}
Note: See TracBrowser for help on using the repository browser.