1 | package finki.it.phoneluxbackend.security;
|
---|
2 |
|
---|
3 | import com.auth0.jwt.JWT;
|
---|
4 | import com.auth0.jwt.algorithms.Algorithm;
|
---|
5 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
6 | import finki.it.phoneluxbackend.entities.User;
|
---|
7 | import org.springframework.security.authentication.AuthenticationManager;
|
---|
8 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
---|
9 | import org.springframework.security.core.Authentication;
|
---|
10 | import org.springframework.security.core.AuthenticationException;
|
---|
11 | import org.springframework.security.core.GrantedAuthority;
|
---|
12 | import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
|
---|
13 |
|
---|
14 | import javax.servlet.FilterChain;
|
---|
15 | import javax.servlet.ServletException;
|
---|
16 | import javax.servlet.http.HttpServletRequest;
|
---|
17 | import javax.servlet.http.HttpServletResponse;
|
---|
18 | import java.io.IOException;
|
---|
19 | import java.util.Date;
|
---|
20 | import java.util.HashMap;
|
---|
21 | import java.util.Map;
|
---|
22 | import java.util.stream.Collectors;
|
---|
23 |
|
---|
24 | import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
---|
25 |
|
---|
26 | public 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 | .withClaim("name", user.getFirstName())
|
---|
53 | .withClaim("id", user.getId())
|
---|
54 | .sign(algorithm);
|
---|
55 |
|
---|
56 |
|
---|
57 | Map<String,String> authInfo = new HashMap<>();
|
---|
58 | authInfo.put("access_token",access_token);
|
---|
59 | response.setContentType(APPLICATION_JSON_VALUE);
|
---|
60 | new ObjectMapper().writeValue(response.getOutputStream(),authInfo);
|
---|
61 | }
|
---|
62 | }
|
---|