Repository navigation
Expand file tree
/
Copy pathSystemSecurityConfig.java
More file actions
44 lines (40 loc) · 1.94 KB
/
Copy pathSystemSecurityConfig.java
File metadata and controls
44 lines (40 loc) · 1.94 KB
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
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
// marks this class as the Spring Security configuration for the application
@Configuration
@EnableWebSecurity
public class SystemSecurityConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
// rejects all plain HTTP traffic, HTTPS required for every request
.requiresChannel(channel -> channel
.anyRequest().requiresSecure())
// login page is publicly accessible, all other endpoints require authentication
.authorizeHttpRequests(auth -> auth
.requestMatchers("/login").permitAll()
.anyRequest().authenticated())
// successful authentication redirects to the protected dashboard
.formLogin(form -> form
.loginPage("/login")
.defaultSuccessUrl("/dashboard", true)
.permitAll())
// logout clears the session and returns to the login page
.logout(logout -> logout
.logoutSuccessUrl("/login")
.permitAll());
return http.build();
}
// BCrypt registered as a Spring bean, available wherever credential verification occurs
// one-way hashing means stored accessKey values cannot be reversed to plaintext
// a phished or leaked hash yields no usable credential information to an attacker
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}