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); + } +}