source: src/main/java/it/finki/charitable/security/SecurityConfig.java@ a64f926

Last change on this file since a64f926 was b8dc761, checked in by NikolaCenevski <cenevskinikola@…>, 3 years ago

part 2

  • Property mode set to 100644
File size: 3.4 KB
Line 
1package it.finki.charitable.security;
2
3import it.finki.charitable.entities.UserRole;
4import it.finki.charitable.services.UserService;
5import org.springframework.beans.factory.annotation.Autowired;
6import org.springframework.context.annotation.Configuration;
7import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
8import org.springframework.security.config.annotation.web.builders.HttpSecurity;
9import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
10import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
11import org.springframework.security.web.DefaultRedirectStrategy;
12import org.springframework.security.web.RedirectStrategy;
13import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
14import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
15
16@Configuration
17public class SecurityConfig extends WebSecurityConfigurerAdapter {
18
19 @Autowired
20 private UserO2AuthService userO2AuthService;
21 @Autowired
22 private O2AuthSuccessHandler o2AuthSuccessHandler;
23
24 private final UserService userService;
25 private BCryptPasswordEncoder passwordEncoder() {
26 return PasswordEncoder.bCryptPasswordEncoder();
27 }
28
29 public SecurityConfig(UserService userService) {
30 this.userService = userService;
31 }
32
33 private final static String[] publicMatchers = {
34 "/css/**",
35 "/js/**",
36 "/image/**",
37 "/",
38 "/login",
39 "/register",
40 "/validate",
41 "/album/**",
42 "/post",
43 "/post-photos/**",
44 "/oauth2/authorization/google"
45 };
46
47 @Override
48 protected void configure(HttpSecurity http) throws Exception {
49 http
50 .authorizeRequests()
51 .antMatchers(publicMatchers).permitAll()
52 .antMatchers("/moderator-photos/**", "/moderator/**").hasAuthority(UserRole.MODERATOR.name())
53 .anyRequest().hasAuthority(UserRole.USER.name());
54
55 http
56 .csrf().disable()
57 .cors().disable()
58 .formLogin().loginPage("/login")
59 .successHandler(authenticationSuccessHandler)
60 .and()
61 .oauth2Login()
62 .loginPage("/login")
63 .userInfoEndpoint()
64 .userService(userO2AuthService)
65 .and()
66 .successHandler(o2AuthSuccessHandler)
67 .and()
68 .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
69 .logoutSuccessUrl("/").deleteCookies("remember-me")
70 .and()
71 .rememberMe();
72 }
73
74 AuthenticationSuccessHandler authenticationSuccessHandler = (httpServletRequest, httpServletResponse, authentication) -> {
75 RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
76 if(authentication.getAuthorities().toString().contains("MODERATOR")) {
77 redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/moderator/approval?page=1&sort=id");
78 } else {
79 redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/");
80 }
81 };
82
83
84
85 @Override
86 protected void configure(AuthenticationManagerBuilder auth) throws Exception {
87 auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
88 }
89}
Note: See TracBrowser for help on using the repository browser.