Skip to content

Commit f192d26

Browse files
committed
Added some updates for SecurityConfig
1 parent 7edca47 commit f192d26

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

docker-compose.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ services:
3636
# Use the Docker Image postgres. This will pull the 14 version.
3737
#image: postgres:14-alpine
3838
#healthcheck:
39-
# test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
39+
# test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U" ]
4040
#timeout: 45s
4141
#interval: 10s
4242
#retries: 10
@@ -99,7 +99,7 @@ services:
9999
networks:
100100
- keycloak
101101
healthcheck:
102-
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
102+
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U" ]
103103
timeout: 45s
104104
interval: 10s
105105
retries: 10

src/main/java/com/kaluzny/demo/config/SecurityConfig.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.kaluzny.demo.config;
22

3-
import lombok.RequiredArgsConstructor;
43
import org.springframework.context.annotation.Bean;
54
import org.springframework.context.annotation.Configuration;
65
import org.springframework.http.HttpMethod;
@@ -17,18 +16,19 @@
1716

1817
@Configuration
1918
@EnableWebSecurity
20-
//@EnableMethodSecurity
19+
@EnableMethodSecurity
2120
class SecurityConfig {
2221

2322
@Bean
2423
public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {
2524

2625
httpSecurity
27-
.authorizeHttpRequests(registry -> registry
28-
.requestMatchers(HttpMethod.GET,"/api/**").hasRole("USER")
29-
.requestMatchers(HttpMethod.POST,"/api/**").hasRole("PERSON")
26+
//TODO: security without @PreAuthorize
27+
/* .authorizeHttpRequests(registry -> registry
28+
.requestMatchers(HttpMethod.GET, "/api/**").hasRole("USER")
29+
.requestMatchers(HttpMethod.POST, "/api/**").hasRole("PERSON")
3030
.anyRequest().authenticated()
31-
)
31+
)*/
3232
.oauth2ResourceServer(oauth2Configurer -> oauth2Configurer
3333
.jwt(jwtConfigurer -> jwtConfigurer
3434
.jwtAuthenticationConverter(jwt -> {

src/main/java/com/kaluzny/demo/web/AutomobileRestController.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public Automobile saveAutomobile(@Valid @RequestBody Automobile automobile) {
5757
@GetMapping("/automobiles")
5858
@ResponseStatus(HttpStatus.OK)
5959
//@Cacheable(value = "automobile", sync = true)
60-
@PreAuthorize("hasRole('ADMIN')")
60+
@PreAuthorize("hasRole('USER')")
6161
public Collection<Automobile> getAllAutomobiles() {
6262
log.info("getAllAutomobiles() - start");
6363
Collection<Automobile> collection = repository.findAll();
@@ -68,6 +68,8 @@ public Collection<Automobile> getAllAutomobiles() {
6868
@GetMapping("/automobiles/{id}")
6969
@ResponseStatus(HttpStatus.OK)
7070
//@Cacheable(value = "automobile", sync = true)
71+
//TODO: We do not have PERSON on the user map
72+
@PreAuthorize("hasRole('PERSON')")
7173
public Automobile getAutomobileById(@PathVariable Long id) {
7274
log.info("getAutomobileById() - start: id = {}", id);
7375
Automobile receivedAutomobile = repository.findById(id)

0 commit comments

Comments
 (0)