Skip to content

Commit 71d24a6

Browse files
committed
fix(security): fixes filter
1 parent 6a561ec commit 71d24a6

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/main/java/com/github/renancvitor/inventory/infra/security/SecurityFilter.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,42 @@ public class SecurityFilter extends OncePerRequestFilter {
2727
private TokenService tokenService;
2828

2929
@Override
30-
protected void doFilterInternal(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response,
31-
@NotNull FilterChain filterChain) throws ServletException, IOException {
32-
30+
protected boolean shouldNotFilter(HttpServletRequest request) {
3331
String path = request.getServletPath();
3432

35-
if ("/login".equals(path)
36-
|| path.startsWith("/swagger-ui")
37-
|| path.startsWith("/v3/api-docs")
33+
return path.equals("/login")
3834
|| path.startsWith("/health")
39-
|| path.startsWith("/actuator")) {
40-
filterChain.doFilter(request, response);
41-
return;
42-
}
35+
|| path.startsWith("/actuator")
36+
|| path.startsWith("/swagger-ui")
37+
|| path.startsWith("/v3/api-docs");
38+
}
4339

44-
var tokenJWT = recoveryToken(request);
40+
@Override
41+
protected void doFilterInternal(
42+
HttpServletRequest request,
43+
HttpServletResponse response,
44+
FilterChain filterChain) throws ServletException, IOException {
4545

46-
if (tokenJWT != null) {
47-
var subject = tokenService.getSubject(tokenJWT);
48-
userRepository.findByPersonEmailAndActiveTrue(subject).ifPresent(user -> {
49-
var authentication = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
46+
String tokenJWT = recoveryToken(request);
5047

51-
SecurityContextHolder.getContext().setAuthentication(authentication);
52-
});
48+
if (tokenJWT != null) {
49+
String subject = tokenService.getSubject(tokenJWT);
50+
userRepository.findByPersonEmailAndActiveTrue(subject)
51+
.ifPresent(user -> {
52+
var auth = new UsernamePasswordAuthenticationToken(
53+
user, null, user.getAuthorities());
54+
SecurityContextHolder.getContext().setAuthentication(auth);
55+
});
5356
}
5457

5558
filterChain.doFilter(request, response);
5659
}
5760

58-
public String recoveryToken(HttpServletRequest request) {
59-
var authorizationHeader = request.getHeader("Authorization");
60-
if (authorizationHeader != null) {
61-
return authorizationHeader.replace("Bearer ", "");
61+
private String recoveryToken(HttpServletRequest request) {
62+
String authorizationHeader = request.getHeader("Authorization");
63+
if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) {
64+
return authorizationHeader.substring(7);
6265
}
63-
6466
return null;
6567
}
66-
6768
}

0 commit comments

Comments
 (0)