From 08db1dc106a304bdff1ac75d342a912466b8c491 Mon Sep 17 00:00:00 2001 From: Moritz <59416038+Morazzer@users.noreply.github.com> Date: Sun, 15 Sep 2024 14:42:36 +0200 Subject: [PATCH] fix: correct error display (#4) --- .../cookies/backend/auth/SecurityConfig.java | 2 +- .../backend/controllers/HealthController.java | 19 +++++++++++++++++++ .../backend/services/MojangAuthService.java | 9 ++------- src/main/resources/application.yml | 3 +++ 4 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 src/main/java/dev/morazzer/cookies/backend/controllers/HealthController.java diff --git a/src/main/java/dev/morazzer/cookies/backend/auth/SecurityConfig.java b/src/main/java/dev/morazzer/cookies/backend/auth/SecurityConfig.java index b2b044e..e336b55 100644 --- a/src/main/java/dev/morazzer/cookies/backend/auth/SecurityConfig.java +++ b/src/main/java/dev/morazzer/cookies/backend/auth/SecurityConfig.java @@ -23,7 +23,7 @@ public SecurityConfig(BearerTokenAuthFilter bearerTokenAuthFilter) { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests(request -> { - request.requestMatchers("/login", "/auth").permitAll(); + request.requestMatchers("/login", "/auth", "/health", "/error").permitAll(); request.anyRequest().authenticated(); }).csrf(CsrfConfigurer::disable).addFilterBefore(bearerTokenAuthFilter, BasicAuthenticationFilter.class); return http.build(); diff --git a/src/main/java/dev/morazzer/cookies/backend/controllers/HealthController.java b/src/main/java/dev/morazzer/cookies/backend/controllers/HealthController.java new file mode 100644 index 0000000..6a0e653 --- /dev/null +++ b/src/main/java/dev/morazzer/cookies/backend/controllers/HealthController.java @@ -0,0 +1,19 @@ +package dev.morazzer.cookies.backend.controllers; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping +public class HealthController { + + public record Status(boolean ok) {} + + @GetMapping(value = "/health", produces = MediaType.APPLICATION_JSON_VALUE) + public Status health() { + return new Status(true); + } + +} diff --git a/src/main/java/dev/morazzer/cookies/backend/services/MojangAuthService.java b/src/main/java/dev/morazzer/cookies/backend/services/MojangAuthService.java index 7b0c220..73df14c 100644 --- a/src/main/java/dev/morazzer/cookies/backend/services/MojangAuthService.java +++ b/src/main/java/dev/morazzer/cookies/backend/services/MojangAuthService.java @@ -4,17 +4,11 @@ import dev.morazzer.cookies.backend.entities.other.MinecraftUser; import dev.morazzer.cookies.backend.utils.redis.UUIDKey; import dev.morazzer.cookies.backend.utils.redis.UsernameKey; -import dev.morazzer.cookies.entities.request.AuthRequest; -import java.io.IOException; import java.net.URI; import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.util.Optional; -import java.util.concurrent.TimeUnit; -import javax.swing.text.html.Option; -import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; -import org.springframework.web.client.RestTemplate; @Service public class MojangAuthService { @@ -44,7 +38,8 @@ public Optional isAuthenticated(String sharedSecret, String usern MinecraftRedisService.INSTANCE.write(new UUIDKey(username), user.uuid()); return Optional.of(user); } - } catch (IOException e) { + } catch (Exception e) { + e.printStackTrace(); throw new RuntimeException(e); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 2241ba9..2eedf53 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -53,6 +53,9 @@ spring: username: ${RABBIT_MQ_USERNAME} password: ${RABBIT_MQ_PASSWORD} host: ${RABBIT_MQ_HOST} + mvc: + servlet: + path: ${API_PATH} backend: auth: sign: ${BACKEND_SIGN_KEY}