Skip to content

Commit

Permalink
Merge pull request #17 from burakkggul/jwt_username_check
Browse files Browse the repository at this point in the history
jwt username existing check added.
  • Loading branch information
burakkggul authored Jul 15, 2022
2 parents c1a8d44 + 1469912 commit 4221de2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
2 changes: 1 addition & 1 deletion idea-httpclient/funct-tests.http
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Content-Type: application/json

### HealthCheck Request
GET http://localhost:8080/healthCheck
Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJidXJhayIsImlzcyI6ImJ1cmFrZ3VsLmNvbS50ciIsImlhdCI6MTY1Nzg5MTg1MCwiZXhwIjoxNjU3ODkyMTUwfQ.9NbJxa4WMN4s-bRe_lo-hNOfYZ6oK5STncUdD3wr1onDidKV57QRIkTk5kZDAeZK7scE0cTu-CWvvYYcFPmzFA
Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJidXJhayIsImlzcyI6ImJ1cmFrZ3VsLmNvbS50ciIsImlhdCI6MTY1Nzg5NDY0NywiZXhwIjoxNjU3ODk0OTQ3fQ._8EUA5YPckRefiuYmHWz-eyy2VvPw63W1KKG7J8LsTlTkYFI7ibFvi96j-bidXa6IG0wYr--o4uaZY7xabUdgg
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
public class JwtTokenFilter extends OncePerRequestFilter {

@Autowired
public JwtTokenFilter(TokenManager tokenManager) {
public JwtTokenFilter(TokenManager tokenManager, UserDetailService userDetailService) {
this.tokenManager = tokenManager;
this.userDetailService = userDetailService;
}

private TokenManager tokenManager;
private UserDetailService userDetailService;

/**
* Bu metod gelen her isteği karşılamaktadır.
Expand Down Expand Up @@ -81,11 +83,14 @@ public void doFilterInternal(HttpServletRequest httpServletRequest,
*/
if (token != null && username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
if (tokenManager.hasTokenValid(token)) {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username,
null,
new ArrayList<>());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
UserDetails user = this.userDetailService.loadUserByUsername(username);
if (Objects.nonNull(user)) {
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user,
null,
new ArrayList<>());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
}
}
/*
Expand Down

0 comments on commit 4221de2

Please sign in to comment.