From 319f187caefff432c003d0c07c43ef2c345f5498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20S=C3=A4ume?= Date: Mon, 30 Sep 2024 09:09:01 +0200 Subject: [PATCH] PMT-9: Wrap Reading of JWT from User Auth, so it can be passed later on --- src/main/java/de/hmmh/pmt/auth/JwtToken.java | 12 ++++++ .../java/de/hmmh/pmt/auth/JwtTokenFilter.java | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 src/main/java/de/hmmh/pmt/auth/JwtToken.java create mode 100644 src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java diff --git a/src/main/java/de/hmmh/pmt/auth/JwtToken.java b/src/main/java/de/hmmh/pmt/auth/JwtToken.java new file mode 100644 index 0000000..67c5897 --- /dev/null +++ b/src/main/java/de/hmmh/pmt/auth/JwtToken.java @@ -0,0 +1,12 @@ +package de.hmmh.pmt.auth; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.stereotype.Component; + +@Getter +@Setter +@Component +public class JwtToken{ + private String token; +} diff --git a/src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java b/src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java new file mode 100644 index 0000000..d165241 --- /dev/null +++ b/src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java @@ -0,0 +1,38 @@ +package de.hmmh.pmt.auth; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import java.io.IOException; + +@Component +public class JwtTokenFilter extends OncePerRequestFilter { + + @Autowired + private JwtToken token; + + @Override + protected void doFilterInternal( + HttpServletRequest request, + HttpServletResponse response, + FilterChain filterChain + ) throws ServletException, IOException { + token.setToken(null); + String authHeader = request.getHeader("Authorization"); + if (authHeader == null) { + filterChain.doFilter(request, response); + return; + } + if (!authHeader.startsWith("Bearer ")) { + filterChain.doFilter(request, response); + return; + } + token.setToken(authHeader.substring("Bearer ".length())); + filterChain.doFilter(request, response); + } +}