source: sources/app/src/main/java/parkup/configs/CustomAuthenticationFilter.java@ 74fb0dc

Last change on this file since 74fb0dc was 74fb0dc, checked in by andrejTavchioski <andrej.tavchioski@…>, 2 years ago

edited JWT token

  • Property mode set to 100644
File size: 4.5 KB
Line 
1package parkup.configs;
2
3import com.auth0.jwt.JWT;
4import com.auth0.jwt.algorithms.Algorithm;
5
6import com.fasterxml.jackson.databind.ObjectMapper;
7
8
9import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
10import org.springframework.security.authentication.AuthenticationManager;
11import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
12import org.springframework.security.core.Authentication;
13import org.springframework.security.core.AuthenticationException;
14import org.springframework.security.core.GrantedAuthority;
15import org.springframework.security.core.userdetails.User;
16import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
17import org.springframework.web.bind.annotation.CrossOrigin;
18import parkup.entities.Administrator;
19import parkup.entities.RegisteredUser;
20import parkup.entities.Worker;
21
22import javax.servlet.FilterChain;
23import javax.servlet.ServletException;
24import javax.servlet.http.HttpServletRequest;
25import javax.servlet.http.HttpServletResponse;
26import java.io.IOException;
27import java.util.*;
28import java.util.stream.Collectors;
29
30import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
31
32@CrossOrigin
33public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
34 private final AuthenticationManager authenticationManager;
35
36 public CustomAuthenticationFilter(AuthenticationManager authenticationManager) {
37 this.authenticationManager = authenticationManager;
38 }
39
40 @Override
41 public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
42 String username = request.getParameter("username");
43 String password = request.getParameter("password");
44 UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
45 return authenticationManager.authenticate(authenticationToken);
46 }
47
48 @Override
49 protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException {
50 Object principal = authentication.getPrincipal();
51 String className = principal.getClass().getName().split("\\.")[2];
52 String email=null;
53 String fullName=null;
54 Collection<? extends GrantedAuthority> roles= new ArrayList<>();
55 switch (className) {
56 case "RegisteredUser": {
57 RegisteredUser user = (RegisteredUser) authentication.getPrincipal();
58 fullName=user.getFirstName()+" "+user.getLastName();
59 email = user.getEmail();
60 roles = user.getAuthorities();
61 break;
62 }
63 case "Worker": {
64 Worker user = (Worker) authentication.getPrincipal();
65 email = user.getEmail();
66 fullName=user.getFirstName()+" "+user.getLastName();
67 roles = user.getAuthorities();
68 break;
69 }
70 case "Administrator": {
71 Administrator user = (Administrator) authentication.getPrincipal();
72 email = user.getEmail();
73 fullName=user.getFirstName()+" "+user.getLastName();
74 roles = user.getAuthorities();
75 break;
76 }
77 }
78 //TODO see if guest needs to go through authentication
79 Algorithm algorithm = Algorithm.HMAC256("secret".getBytes());
80 String access_token = JWT.create()
81 .withSubject(email)
82 .withClaim("fullName",fullName)
83 .withExpiresAt(new Date(System.currentTimeMillis() + 60 * 60 * 1000))
84 .withIssuer(request.getRequestURL().toString())
85 .withClaim("roles", roles.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()))
86 .sign(algorithm);
87// String refresh_token = JWT.create()
88// .withSubject(user.getUsername())
89// .withExpiresAt(new Date(System.currentTimeMillis() + 30 * 60 * 1000))
90// .withIssuer(request.getRequestURL().toString())
91// .sign(algorithm);
92 Map<String, String> tokens = new HashMap<>();
93 tokens.put("access_token", access_token);
94// tokens.put("refresh_token", refresh_token);
95 response.setContentType(APPLICATION_JSON_VALUE);
96 new ObjectMapper().writeValue(response.getOutputStream(), tokens);
97 }
98}
Note: See TracBrowser for help on using the repository browser.