source: phonelux-backend/src/main/java/finki/it/phoneluxbackend/security/CustomAuthenticationFilter.java@ e5b84dc

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

Prototype version

  • Property mode set to 100644
File size: 3.2 KB
Line 
1package finki.it.phoneluxbackend.security;
2
3import com.auth0.jwt.JWT;
4import com.auth0.jwt.algorithms.Algorithm;
5import com.fasterxml.jackson.databind.ObjectMapper;
6import finki.it.phoneluxbackend.entities.User;
7import org.springframework.security.authentication.AuthenticationManager;
8import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
9import org.springframework.security.core.Authentication;
10import org.springframework.security.core.AuthenticationException;
11import org.springframework.security.core.GrantedAuthority;
12import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
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.Date;
20import java.util.HashMap;
21import java.util.Map;
22import java.util.stream.Collectors;
23
24import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
25
26public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
27 private final AuthenticationManager authenticationManager;
28
29 public CustomAuthenticationFilter(AuthenticationManager authenticationManager) {
30 this.authenticationManager = authenticationManager;
31 }
32
33 @Override
34 public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
35 String email = request.getParameter("email");
36 String password = request.getParameter("password");
37 UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email,password);
38 return authenticationManager.authenticate(authenticationToken);
39 }
40
41 @Override
42 protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException {
43 User user = (User)authentication.getPrincipal();
44 Algorithm algorithm = Algorithm.HMAC256("secret".getBytes());
45
46 String access_token = JWT.create()
47 .withSubject(user.getEmail())
48 .withExpiresAt(new Date(System.currentTimeMillis() + 10 * 60 * 100000)) // approx. 16.5 hours
49 .withIssuer(request.getRequestURL().toString())
50 .withClaim("role", user.getAuthorities().stream()
51 .map(GrantedAuthority::getAuthority).collect(Collectors.toList()))
52 .sign(algorithm);
53
54// String refresh_token = JWT.create()
55// .withSubject(user.getEmail())
56// .withExpiresAt(new Date(System.currentTimeMillis() + 30 * 60 * 1000))
57// .withIssuer(request.getRequestURL().toString())
58// .withClaim("role",user.getAuthorities().stream()
59// .map(GrantedAuthority::getAuthority).collect(Collectors.toList()))
60// .sign(algorithm);
61
62
63 Map<String,String> tokens = new HashMap<>();
64 tokens.put("access_token",access_token);
65// tokens.put("refresh_token",refresh_token);
66 response.setContentType(APPLICATION_JSON_VALUE);
67 new ObjectMapper().writeValue(response.getOutputStream(),tokens);
68 }
69}
Note: See TracBrowser for help on using the repository browser.