-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSecurityConfig.java
83 lines (70 loc) · 3.58 KB
/
WebSecurityConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.engine.security;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true) // Enable @PreAuthorize and @PostAuthorize annotations
@Profile("!dev")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter implements WebMvcConfigurer {
@Autowired
private AuthenticationExceptionHandlerEntryPoint handler;
@Override
protected void configure(HttpSecurity http) throws Exception {
// HttpSecurity is the first layer
// @formatter:off
http.authorizeRequests()
.antMatchers(HttpMethod.POST,"/users/authorize").permitAll()
.antMatchers(HttpMethod.POST,"/users/reset_request").permitAll()
.antMatchers(HttpMethod.POST,"/users/reset_token").permitAll()
.antMatchers(HttpMethod.POST,"/users/reset_password").permitAll()
.antMatchers(HttpMethod.POST,"/users/confirmation_token").permitAll()
.antMatchers(HttpMethod.POST,"/users/reset_user_password").permitAll()
.anyRequest().authenticated()
.and().formLogin().permitAll()
.and().csrf().disable();
http.httpBasic().disable();
// If a user try to access a resource without having enough permissions
http.exceptionHandling().authenticationEntryPoint(handler);
// No session will be created or used by spring security
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
// @formatter:on
}
@Override
public void configure(WebSecurity web) throws Exception {
// WebSecurity is the second layer
web.ignoring()
.antMatchers(HttpMethod.POST,"/users/authorize")
.antMatchers(HttpMethod.POST,"/users/reset_request")
.antMatchers(HttpMethod.POST,"/users/reset_token")
.antMatchers(HttpMethod.POST,"/users/reset_password")
.antMatchers(HttpMethod.POST,"/users/confirmation_token")
.antMatchers(HttpMethod.POST,"/users/reset_user_password");
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder(12);
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**");
}
}