Skip to content

Commit 255f8f5

Browse files
committed
add basic spring security config (allow most of the endpoints, and setup in memory user and admin!)
1 parent ae8f028 commit 255f8f5

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

pom.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
<artifactId>spring-boot-starter-data-jpa</artifactId>
2323
</dependency>
2424

25-
<!--<dependency>
25+
<dependency>
2626
<groupId>org.springframework.boot</groupId>
2727
<artifactId>spring-boot-starter-security</artifactId>
28-
</dependency>-->
28+
</dependency>
2929

3030
<dependency>
3131
<groupId>org.springframework.boot</groupId>
@@ -104,6 +104,10 @@
104104
<version>4.8.1</version>
105105
<scope>test</scope>
106106
</dependency>
107+
<dependency>
108+
<groupId>org.springframework.boot</groupId>
109+
<artifactId>spring-boot-starter-security</artifactId>
110+
</dependency>
107111

108112
<!--<dependency>
109113
<groupId>org.springframework.security</groupId>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.csaba79coder.littersnap.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
6+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
7+
import org.springframework.security.core.userdetails.User;
8+
import org.springframework.security.core.userdetails.UserDetails;
9+
import org.springframework.security.core.userdetails.UserDetailsService;
10+
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
11+
import org.springframework.security.crypto.password.PasswordEncoder;
12+
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
13+
import org.springframework.security.web.SecurityFilterChain;
14+
15+
@Configuration
16+
@EnableWebSecurity
17+
public class SecurityConfiguration {
18+
19+
@Bean
20+
public PasswordEncoder encoder() {
21+
return new BCryptPasswordEncoder();
22+
}
23+
24+
@Bean
25+
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
26+
http
27+
.authorizeRequests()
28+
.anyRequest().permitAll() // Allow access to all endpoints
29+
.and()
30+
.formLogin()
31+
.loginPage("/login")
32+
.permitAll()
33+
.and()
34+
.logout()
35+
.permitAll()
36+
.and()
37+
.csrf().disable(); // Disable CSRF protection
38+
return http.build();
39+
40+
/*
41+
this disables all authentication and authorization for the moment!
42+
http
43+
.authorizeRequests()
44+
.anyRequest().permitAll() // Allow access to all endpoints
45+
.and()
46+
.formLogin()
47+
.disable() // Disable form-based login
48+
.and()
49+
.logout()
50+
.disable() // Disable logout functionality
51+
.and()
52+
.csrf()
53+
.disable(); // Disable CSRF protection
54+
return http.build();
55+
*/
56+
}
57+
58+
@Bean
59+
public UserDetailsService users() {
60+
UserDetails user = User.builder()
61+
.username("user")
62+
.password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW") // password
63+
.roles("USER_ROLE")
64+
.build();
65+
UserDetails admin = User.builder()
66+
.username("admin")
67+
.password("{bcrypt}$2a$10$GRLdNijSQMUvl/au9ofL.eDwmoohzzS7.rmNSJZ.0FxO/BTk76klW") // password
68+
.roles("ADMIN_ROLE")
69+
.build();
70+
return new InMemoryUserDetailsManager(user, admin);
71+
}
72+
}

0 commit comments

Comments
 (0)