-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
149aa08
commit d865688
Showing
4 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -96,7 +96,6 @@ | |
<artifactId>java-jwt</artifactId> | ||
<version>4.2.1</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/pi/procurarteapi/app/auth/config/FilterToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package pi.procurarteapi.app.auth.config; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import pi.procurarteapi.app.auth.services.TokenService; | ||
import pi.procurarteapi.infra.repositories.IMusicianRepository; | ||
|
||
@Component | ||
public class FilterToken extends OncePerRequestFilter{ | ||
|
||
@Autowired | ||
private TokenService tokenService; | ||
|
||
@Autowired | ||
private IMusicianRepository musicianRepository; | ||
|
||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, | ||
HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
String token; | ||
var authorizationHeader = request.getHeader("Authorization"); | ||
|
||
if(authorizationHeader != null){ | ||
token = authorizationHeader.replace("Bearer ", ""); | ||
var subject = tokenService.getSubject(token); | ||
|
||
var musician = musicianRepository.findByEmail(subject); | ||
|
||
var authentication = new UsernamePasswordAuthenticationToken(musician,null, musician.getAuthorities()); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters