aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/berzerkula/builddb/config/SecurityConfig.java
blob: b346b59f4beae58dd239487d55b0cdd0afe78d47 (plain)
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
package org.berzerkula.builddb.config;

import org.berzerkula.builddb.BuilddbConstants;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
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;

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {

	@Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        return http
                .authorizeHttpRequests( auth -> auth
                		.requestMatchers("/").permitAll()
						.requestMatchers("/actuator/**").hasRole(BuilddbConstants.ROLE_ADMIN)
						.requestMatchers("/env/**").hasRole(BuilddbConstants.ROLE_ADMIN)
						.requestMatchers("/health/**").hasRole(BuilddbConstants.ROLE_ADMIN)
						.requestMatchers("/info/**").hasRole(BuilddbConstants.ROLE_ADMIN)
                		.requestMatchers("/contact").permitAll()
	                    .requestMatchers("/pkgs/**").hasRole(BuilddbConstants.ROLE_CLIENT)
	                    .requestMatchers("/register").permitAll()
	                    .requestMatchers("/login").permitAll()
	                    .requestMatchers("/logout").permitAll()
	                    .anyRequest().authenticated()
                )
                .formLogin(form -> form
                		.loginPage("/login")
                		.usernameParameter("email")
                		.passwordParameter("password")
                		.defaultSuccessUrl("/", true)
                )
                .logout(config -> config.logoutSuccessUrl("/"))
        		.build();
    }
	
	
	@Bean
	public PasswordEncoder passwordEncoder() {
	    return new BCryptPasswordEncoder();
	}
}