source: phonelux-backend/src/main/java/finki/it/phoneluxbackend/security/CustomAuthorizationFilter.java@ f25d07e

Last change on this file since f25d07e was f25d07e, checked in by Marko <Marko@…>, 22 months ago

Edited registration and login services

  • Property mode set to 100644
File size: 3.3 KB
Line 
1package finki.it.phoneluxbackend.security;
2
3import com.auth0.jwt.JWT;
4import com.auth0.jwt.JWTVerifier;
5import com.auth0.jwt.algorithms.Algorithm;
6import com.auth0.jwt.interfaces.DecodedJWT;
7import com.fasterxml.jackson.databind.ObjectMapper;
8import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
9import org.springframework.security.core.authority.SimpleGrantedAuthority;
10import org.springframework.security.core.context.SecurityContextHolder;
11import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
12import org.springframework.web.filter.OncePerRequestFilter;
13
14import javax.servlet.FilterChain;
15import javax.servlet.ServletException;
16import javax.servlet.http.HttpServletRequest;
17import javax.servlet.http.HttpServletResponse;
18import java.io.IOException;
19import java.util.*;
20
21import static org.springframework.http.HttpHeaders.AUTHORIZATION;
22import static org.springframework.http.HttpStatus.FORBIDDEN;
23import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
24
25public class CustomAuthorizationFilter extends OncePerRequestFilter {
26 @Override
27 protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
28 if(request.getServletPath().equals("/login")){
29 filterChain.doFilter(request,response); // not doing anything, just passing the request to the next filter in the filter chain
30 }
31 else{
32 String authorizationHeader = request.getHeader(AUTHORIZATION);
33 if(authorizationHeader != null && authorizationHeader.startsWith("Bearer "))
34 {
35 try {
36 String token = authorizationHeader.substring("Bearer ".length());
37 Algorithm algorithm = Algorithm.HMAC256("secret".getBytes());
38 JWTVerifier verifier = JWT.require(algorithm).build();
39 DecodedJWT decodedJWT = verifier.verify(token);
40 String email = decodedJWT.getSubject();
41 String [] roles = decodedJWT.getClaim("role").asArray(String.class);
42 Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
43 Arrays.stream(roles).forEach(role -> {
44 authorities.add(new SimpleGrantedAuthority(role));
45 });
46 UsernamePasswordAuthenticationToken authenticationToken =
47 new UsernamePasswordAuthenticationToken(email,null,authorities);
48
49 SecurityContextHolder.getContext().setAuthentication(authenticationToken);
50 filterChain.doFilter(request,response);
51
52 }catch(Exception exception){
53 response.setHeader("error", exception.getMessage());
54 response.setStatus(FORBIDDEN.value());
55 Map<String,String> error = new HashMap<>();
56 error.put("error_message", exception.getMessage());
57 response.setContentType(APPLICATION_JSON_VALUE);
58 new ObjectMapper().writeValue(response.getOutputStream(),error);
59 }
60 }
61 else{
62 filterChain.doFilter(request,response);
63 }
64
65 }
66 }
67}
Note: See TracBrowser for help on using the repository browser.