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

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

Added more controllers

  • Property mode set to 100644
File size: 2.9 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 .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}
Note: See TracBrowser for help on using the repository browser.