[3fc9e50] | 1 | package it.finki.charitable.security;
|
---|
| 2 |
|
---|
| 3 | import it.finki.charitable.entities.UserRole;
|
---|
| 4 | import it.finki.charitable.services.UserService;
|
---|
[b8dc761] | 5 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
[3fc9e50] | 6 | import org.springframework.context.annotation.Configuration;
|
---|
| 7 | import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
---|
| 8 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
---|
| 9 | import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
---|
| 10 | import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
---|
[5306751] | 11 | import org.springframework.security.web.DefaultRedirectStrategy;
|
---|
| 12 | import org.springframework.security.web.RedirectStrategy;
|
---|
| 13 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
---|
[3fc9e50] | 14 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
---|
| 15 |
|
---|
| 16 | @Configuration
|
---|
| 17 | public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
---|
| 18 |
|
---|
[b8dc761] | 19 | @Autowired
|
---|
| 20 | private UserO2AuthService userO2AuthService;
|
---|
| 21 | @Autowired
|
---|
| 22 | private O2AuthSuccessHandler o2AuthSuccessHandler;
|
---|
| 23 |
|
---|
[3fc9e50] | 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",
|
---|
[b8dc761] | 43 | "/post-photos/**",
|
---|
| 44 | "/oauth2/authorization/google"
|
---|
[3fc9e50] | 45 | };
|
---|
| 46 |
|
---|
| 47 | @Override
|
---|
| 48 | protected void configure(HttpSecurity http) throws Exception {
|
---|
| 49 | http
|
---|
| 50 | .authorizeRequests()
|
---|
| 51 | .antMatchers(publicMatchers).permitAll()
|
---|
[5306751] | 52 | .antMatchers("/moderator-photos/**", "/moderator/**").hasAuthority(UserRole.MODERATOR.name())
|
---|
| 53 | .anyRequest().hasAuthority(UserRole.USER.name());
|
---|
[3fc9e50] | 54 |
|
---|
| 55 | http
|
---|
| 56 | .csrf().disable()
|
---|
| 57 | .cors().disable()
|
---|
| 58 | .formLogin().loginPage("/login")
|
---|
[5306751] | 59 | .successHandler(authenticationSuccessHandler)
|
---|
[3fc9e50] | 60 | .and()
|
---|
[b8dc761] | 61 | .oauth2Login()
|
---|
| 62 | .loginPage("/login")
|
---|
| 63 | .userInfoEndpoint()
|
---|
| 64 | .userService(userO2AuthService)
|
---|
| 65 | .and()
|
---|
| 66 | .successHandler(o2AuthSuccessHandler)
|
---|
| 67 | .and()
|
---|
[3fc9e50] | 68 | .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
|
---|
| 69 | .logoutSuccessUrl("/").deleteCookies("remember-me")
|
---|
| 70 | .and()
|
---|
| 71 | .rememberMe();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[5306751] | 74 | AuthenticationSuccessHandler authenticationSuccessHandler = (httpServletRequest, httpServletResponse, authentication) -> {
|
---|
| 75 | RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
|
---|
| 76 | if(authentication.getAuthorities().toString().contains("MODERATOR")) {
|
---|
[0c37625] | 77 | redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/moderator/approval?page=1&sort=id");
|
---|
[5306751] | 78 | } else {
|
---|
| 79 | redirectStrategy.sendRedirect(httpServletRequest, httpServletResponse, "/");
|
---|
| 80 | }
|
---|
| 81 | };
|
---|
| 82 |
|
---|
[b8dc761] | 83 |
|
---|
| 84 |
|
---|
[3fc9e50] | 85 | @Override
|
---|
| 86 | protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
---|
| 87 | auth.userDetailsService(userService).passwordEncoder(passwordEncoder());
|
---|
| 88 | }
|
---|
| 89 | }
|
---|