PMT-9: Wrap Reading of JWT from User Auth, so it can be passed later on

This commit is contained in:
Dominik Säume 2024-09-30 09:09:01 +02:00
parent d3755984d9
commit 319f187cae
Signed by: SZUT-Dominik
GPG key ID: 67D15BB250B41E7C
2 changed files with 50 additions and 0 deletions

View file

@ -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;
}

View file

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