package finki.it.phoneluxbackend.security; import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; import com.auth0.jwt.interfaces.DecodedJWT; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import org.springframework.web.filter.OncePerRequestFilter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; import static org.springframework.http.HttpHeaders.AUTHORIZATION; import static org.springframework.http.HttpStatus.FORBIDDEN; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; public class CustomAuthorizationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if(request.getServletPath().equals("/login")){ filterChain.doFilter(request,response); // not doing anything, just passing the request to the next filter in the filter chain } else{ String authorizationHeader = request.getHeader(AUTHORIZATION); if(authorizationHeader != null && authorizationHeader.startsWith("Bearer ")) { try { String token = authorizationHeader.substring("Bearer ".length()); Algorithm algorithm = Algorithm.HMAC256("secret".getBytes()); JWTVerifier verifier = JWT.require(algorithm).build(); DecodedJWT decodedJWT = verifier.verify(token); String email = decodedJWT.getSubject(); String [] roles = decodedJWT.getClaim("role").asArray(String.class); Collection authorities = new ArrayList<>(); Arrays.stream(roles).forEach(role -> { authorities.add(new SimpleGrantedAuthority(role)); }); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email,null,authorities); SecurityContextHolder.getContext().setAuthentication(authenticationToken); filterChain.doFilter(request,response); }catch(Exception exception){ response.setHeader("error", exception.getMessage()); response.setStatus(FORBIDDEN.value()); Map error = new HashMap<>(); error.put("error_message", exception.getMessage()); response.setContentType(APPLICATION_JSON_VALUE); new ObjectMapper().writeValue(response.getOutputStream(),error); } } else{ filterChain.doFilter(request,response); } } } }