[f25d07e] | 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"); // mozda ke treba da se smeni vo username
|
---|
| 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 * 1000))
|
---|
| 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 | }
|
---|