|
1
|
|
|
package com.osomapps.pt.config; |
|
2
|
|
|
|
|
3
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
4
|
|
|
import org.springframework.context.annotation.Bean; |
|
5
|
|
|
import org.springframework.context.annotation.Configuration; |
|
6
|
|
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; |
|
7
|
|
|
import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
|
8
|
|
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; |
|
9
|
|
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
|
10
|
|
|
import org.springframework.security.core.userdetails.UserDetailsService; |
|
11
|
|
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; |
|
12
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder; |
|
13
|
|
|
|
|
14
|
|
|
@EnableWebSecurity |
|
15
|
|
|
@Configuration |
|
16
|
|
|
class SecurityConfig extends WebSecurityConfigurerAdapter { |
|
|
|
|
|
|
17
|
|
|
@Autowired private UserDetailsService userDetailsService; |
|
18
|
|
|
@Autowired private CustomLoginSuccessHandler loginSuccessHandler; |
|
19
|
|
|
@Autowired private CustomLoginFailureHandler loginFailureHandler; |
|
20
|
|
|
@Autowired private CustomLogoutSuccessHandler logoutSuccessHandler; |
|
21
|
|
|
@Autowired private CustomAuthenticationEntryPoint authenticationEntryPoint; |
|
22
|
|
|
|
|
23
|
|
|
@Override |
|
24
|
|
|
public void configure(AuthenticationManagerBuilder auth) throws Exception { |
|
25
|
|
|
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder()); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
@Bean |
|
29
|
|
|
PasswordEncoder passwordEncoder() { |
|
30
|
|
|
return new BCryptPasswordEncoder(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
@Override |
|
34
|
|
|
protected void configure(HttpSecurity http) throws Exception { |
|
35
|
|
|
http.csrf() |
|
36
|
|
|
.disable() |
|
37
|
|
|
.authorizeRequests() |
|
38
|
|
|
.antMatchers("/public/**") |
|
39
|
|
|
.permitAll() |
|
40
|
|
|
.antMatchers("/api/v1/admin/**", "/api/v1/auth/**") |
|
41
|
|
|
.fullyAuthenticated() |
|
42
|
|
|
.and() |
|
43
|
|
|
.formLogin() |
|
44
|
|
|
.loginPage("/login") |
|
45
|
|
|
.failureUrl("/login?error") |
|
46
|
|
|
.permitAll() |
|
47
|
|
|
.successHandler(loginSuccessHandler) |
|
48
|
|
|
.failureHandler(loginFailureHandler) |
|
49
|
|
|
.and() |
|
50
|
|
|
.logout() |
|
51
|
|
|
.permitAll() |
|
52
|
|
|
.logoutSuccessHandler(logoutSuccessHandler); |
|
53
|
|
|
http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|