Mini refactor

This commit is contained in:
2025-04-19 01:41:46 +02:00
parent a8411b6e63
commit 07141f3a1c
5 changed files with 26 additions and 8 deletions

View File

@ -39,13 +39,13 @@ public class SecurityConfig {
http http
.csrf(csrf -> csrf .csrf(csrf -> csrf
.ignoringRequestMatchers("/api/v1/**") .ignoringRequestMatchers("/**")
.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())) // Disable CSRF for API routes .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())) // Disable CSRF for API routes
.sessionManagement(sessionManagement -> sessionManagement .sessionManagement(sessionManagement -> sessionManagement
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) // No session will be created by Spring Security .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // No session will be created by Spring Security
) )
.authorizeHttpRequests(authorize -> authorize .authorizeHttpRequests(authorize -> authorize
.requestMatchers("/api/v1/secure/**").authenticated() // Secure these endpoints .requestMatchers("/secure/**").authenticated() // Secure these endpoints
.anyRequest().permitAll() // All other requests are allowed without authentication .anyRequest().permitAll() // All other requests are allowed without authentication
) )
.headers(headers -> headers .headers(headers -> headers

View File

@ -0,0 +1,14 @@
package de.w665.biblenotes.config;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// Map static resources to the root path
registry.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
}
}

View File

@ -16,7 +16,7 @@ import java.util.Map;
@Slf4j @Slf4j
@RestController @RestController
@RequestMapping("/api/v1/auth") @RequestMapping("/auth")
public class AuthenticationController { public class AuthenticationController {
private final AuthenticationService authenticationService; private final AuthenticationService authenticationService;

View File

@ -17,7 +17,8 @@ import org.springframework.web.bind.annotation.*;
import java.util.Optional; import java.util.Optional;
@RestController @RestController
public class BibleReadingPlanMapping extends SecureApiRestController { @RequestMapping("/secure/bible-reading-plan")
public class BibleReadingPlanMapping {
private final EntityManager entityManager; private final EntityManager entityManager;
private final BibleReadingPlanRepository bibleReadingPlanRepository; private final BibleReadingPlanRepository bibleReadingPlanRepository;
@ -27,8 +28,8 @@ public class BibleReadingPlanMapping extends SecureApiRestController {
this.bibleReadingPlanRepository = bibleReadingPlanRepository; this.bibleReadingPlanRepository = bibleReadingPlanRepository;
} }
@GetMapping("/bible-reading-plan") @GetMapping
public ResponseEntity<Object> getBibleReadingPlans(@RequestParam(required = true) Long id) { public ResponseEntity<Object> getBibleReadingPlans(@RequestParam(name = "id", required = true) Long id) {
Optional<BibleReadingPlan> brp = bibleReadingPlanRepository.findById(id); Optional<BibleReadingPlan> brp = bibleReadingPlanRepository.findById(id);
if(brp.isEmpty()) { if(brp.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
@ -44,7 +45,7 @@ public class BibleReadingPlanMapping extends SecureApiRestController {
return new ResponseEntity<>(bibleReadingPlanDTO, HttpStatus.OK); return new ResponseEntity<>(bibleReadingPlanDTO, HttpStatus.OK);
} }
@PostMapping("/bible-reading-plan") @PostMapping
public ResponseEntity<Object> createBibleReadingPlan(@RequestBody BibleReadingPlanDTO bibleReadingPlanDTO) { public ResponseEntity<Object> createBibleReadingPlan(@RequestBody BibleReadingPlanDTO bibleReadingPlanDTO) {
Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Authentication auth = SecurityContextHolder.getContext().getAuthentication();

View File

@ -15,4 +15,7 @@ spring.jpa.database=postgresql
logging.level.de.w665.biblenotes=DEBUG logging.level.de.w665.biblenotes=DEBUG
# Static path # Static path
spring.web.resources.static-locations=classpath:/static/ spring.web.resources.static-locations=classpath:/static/
# If this is removed, this prefix must be added to the security config
spring.mvc.servlet.path=/api/v1