|
5 | 5 | import com.auth0.jwt.JWTVerifier;
|
6 | 6 | import com.auth0.jwt.algorithms.Algorithm;
|
7 | 7 | import com.auth0.jwt.interfaces.DecodedJWT;
|
8 |
| -import org.springframework.beans.factory.annotation.Value; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
9 | 10 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
10 | 11 | import org.springframework.security.core.GrantedAuthority;
|
11 | 12 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
12 | 13 | import org.springframework.security.core.context.SecurityContextHolder;
|
13 |
| -import org.springframework.security.core.userdetails.User; |
14 | 14 | import org.springframework.web.filter.OncePerRequestFilter;
|
15 | 15 |
|
16 | 16 | import javax.servlet.FilterChain;
|
17 | 17 | import javax.servlet.ServletException;
|
18 | 18 | import javax.servlet.http.HttpServletRequest;
|
19 | 19 | import javax.servlet.http.HttpServletResponse;
|
20 | 20 | import java.io.IOException;
|
21 |
| -import java.security.SignatureException; |
22 | 21 | import java.util.ArrayList;
|
23 | 22 | import java.util.Arrays;
|
24 | 23 | import java.util.Collection;
|
25 | 24 |
|
26 | 25 | public class JwtAuthorizationFilter extends OncePerRequestFilter {
|
| 26 | + |
| 27 | + private Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 28 | + |
27 | 29 | @Override
|
28 | 30 | protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
|
29 | 31 | String authorizationHeader = request.getHeader("Authorization");
|
30 |
| - if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { |
31 |
| - try { |
32 |
| - String accessToken = authorizationHeader.substring(7); |
33 |
| - Algorithm algorithm = Algorithm.HMAC256(SecurityUtils.HMAC_KEY); |
34 |
| - JWTVerifier verifier = JWT.require(algorithm).build(); |
35 |
| - DecodedJWT jwt = verifier.verify(accessToken); |
36 |
| - String username = jwt.getSubject(); |
37 |
| - String roles[] = jwt.getClaim("roles").asArray(String.class); |
38 |
| - Collection<GrantedAuthority> authorities = new ArrayList<>(); |
39 |
| - Arrays.stream(roles).forEach(role -> authorities.add(new SimpleGrantedAuthority(role))); |
40 |
| - UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, null, authorities); |
41 |
| - SecurityContextHolder.getContext().setAuthentication(authenticationToken); |
| 32 | + if(!SecurityUtils.verifyPath(request.getServletPath())){ |
| 33 | + if (authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { |
| 34 | + logger.info("a Bearer container request "); |
| 35 | + try { |
| 36 | + String accessToken = authorizationHeader.substring(7); |
| 37 | + Algorithm algorithm = Algorithm.HMAC256(SecurityUtils.HMAC_KEY); |
| 38 | + JWTVerifier verifier = JWT.require(algorithm).build(); |
| 39 | + DecodedJWT jwt = verifier.verify(accessToken); |
| 40 | + String username = jwt.getSubject(); |
| 41 | + String roles[] = jwt.getClaim("roles").asArray(String.class); |
| 42 | + Collection<GrantedAuthority> authorities = new ArrayList<>(); |
| 43 | + Arrays.stream(roles).forEach(role -> authorities.add(new SimpleGrantedAuthority(role))); |
| 44 | + UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, null, authorities); |
| 45 | + SecurityContextHolder.getContext().setAuthentication(authenticationToken); |
| 46 | + filterChain.doFilter(request, response); |
| 47 | + } catch (Exception e) { |
| 48 | + logger.error("the token is expired"); |
| 49 | + response.setHeader("error", "the token is expired"); |
| 50 | + response.sendError(HttpServletResponse.SC_FORBIDDEN); |
| 51 | + } |
| 52 | + } else |
42 | 53 | filterChain.doFilter(request, response);
|
43 |
| - } catch (Exception e) { |
44 |
| - logger.error("the token is expired"); |
45 |
| - response.setHeader("error","the token is expired"); |
46 |
| - response.sendError(HttpServletResponse.SC_FORBIDDEN); |
47 |
| - } |
48 | 54 | }else
|
49 | 55 | filterChain.doFilter(request,response);
|
50 | 56 |
|
|
0 commit comments