source: springapp/src/main/java/mk/profesori/springapp/Security/SecurityConfiguration.java@ af801e3

main
Last change on this file since af801e3 was af801e3, checked in by viktor <viktor@…>, 19 months ago

finished edit/delete/displace opinion/thread from report (react); todo reporting user/opinion/thread interface, public user pages and messaging (springboot)

  • Property mode set to 100644
File size: 2.6 KB
Line 
1package mk.profesori.springapp.Security;
2
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5import org.springframework.security.authentication.AuthenticationManager;
6import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
7import org.springframework.security.config.annotation.web.builders.HttpSecurity;
8import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
9import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
10import org.springframework.security.crypto.password.PasswordEncoder;
11import org.springframework.security.web.SecurityFilterChain;
12import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
13import org.springframework.web.servlet.config.annotation.CorsRegistry;
14import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
15
16import lombok.RequiredArgsConstructor;
17
18@Configuration
19@RequiredArgsConstructor
20@EnableWebSecurity
21public class SecurityConfiguration {
22
23 @Bean
24 public PasswordEncoder passwordEncoder() {
25 return new BCryptPasswordEncoder();
26 }
27
28 @Bean
29 public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
30 throws Exception {
31 return authenticationConfiguration.getAuthenticationManager();
32 }
33
34 @Bean
35 public WebMvcConfigurer corsConfigurer() {
36 return new WebMvcConfigurer() {
37 @Override
38 public void addCorsMappings(CorsRegistry registry) {
39 registry.addMapping("/**").allowedOrigins("http://192.168.0.29:3000", "http://192.168.0.28:3000")
40 .allowCredentials(true);
41 }
42 };
43 }
44
45 @Bean
46 public AuthenticationSuccessHandler customAuthenticationSuccessHandler() {
47 return new CustomAuthenticationSuccessHandler();
48 }
49
50 @Bean
51 protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
52 http
53 .httpBasic() // neenkriptirani credentials osven ako ne e preky https/tls
54 .and()
55 .cors()
56 .and()
57 .csrf().disable() // PRIVREMENO
58 .authorizeRequests()
59 .antMatchers("/secure/**").hasAnyAuthority("REGULAR", "MODERATOR")
60 .antMatchers("/public/**").permitAll()
61 .antMatchers("/registration/**").permitAll()
62 .and()
63 .formLogin().successHandler(customAuthenticationSuccessHandler());
64
65 return http.build();
66 }
67}
Note: See TracBrowser for help on using the repository browser.