1 | package it.finki.charitable.security;
|
---|
2 |
|
---|
3 | import it.finki.charitable.entities.UserRole;
|
---|
4 | import it.finki.charitable.services.UserService;
|
---|
5 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
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;
|
---|
11 | import org.springframework.security.web.DefaultRedirectStrategy;
|
---|
12 | import org.springframework.security.web.RedirectStrategy;
|
---|
13 | import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
|
---|
14 | import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
|
---|
15 |
|
---|
16 | @Configuration
|
---|
17 | public 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 | }
|
---|