PMT-9: Wrap Reading of JWT from User Auth, so it can be passed later on
This commit is contained in:
parent
d3755984d9
commit
319f187cae
2 changed files with 50 additions and 0 deletions
12
src/main/java/de/hmmh/pmt/auth/JwtToken.java
Normal file
12
src/main/java/de/hmmh/pmt/auth/JwtToken.java
Normal 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;
|
||||
}
|
38
src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java
Normal file
38
src/main/java/de/hmmh/pmt/auth/JwtTokenFilter.java
Normal 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);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue