package finki.it.phoneluxbackend.security; import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; import com.fasterxml.jackson.databind.ObjectMapper; import finki.it.phoneluxbackend.entities.User; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter { private final AuthenticationManager authenticationManager; public CustomAuthenticationFilter(AuthenticationManager authenticationManager) { this.authenticationManager = authenticationManager; } @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException { String email = request.getParameter("email"); String password = request.getParameter("password"); UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email,password); return authenticationManager.authenticate(authenticationToken); } @Override protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException { User user = (User)authentication.getPrincipal(); Algorithm algorithm = Algorithm.HMAC256("secret".getBytes()); String access_token = JWT.create() .withSubject(user.getEmail()) .withExpiresAt(new Date(System.currentTimeMillis() + 10 * 60 * 100000)) // approx. 16.5 hours .withIssuer(request.getRequestURL().toString()) .withClaim("role", user.getAuthorities().stream() .map(GrantedAuthority::getAuthority).collect(Collectors.toList())) .sign(algorithm); // String refresh_token = JWT.create() // .withSubject(user.getEmail()) // .withExpiresAt(new Date(System.currentTimeMillis() + 30 * 60 * 1000)) // .withIssuer(request.getRequestURL().toString()) // .withClaim("role",user.getAuthorities().stream() // .map(GrantedAuthority::getAuthority).collect(Collectors.toList())) // .sign(algorithm); Map tokens = new HashMap<>(); tokens.put("access_token",access_token); // tokens.put("refresh_token",refresh_token); response.setContentType(APPLICATION_JSON_VALUE); new ObjectMapper().writeValue(response.getOutputStream(),tokens); } }