|
|
@@ -3,7 +3,9 @@ package com.example.plantsforyou.filter;
|
|
|
import com.auth0.jwt.JWT;
|
|
|
import com.auth0.jwt.algorithms.Algorithm;
|
|
|
import com.example.plantsforyou.appuser.AppUser;
|
|
|
+import com.example.plantsforyou.appuser.LoginCredentials;
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import lombok.NoArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
@@ -16,6 +18,7 @@ import javax.servlet.FilterChain;
|
|
|
import javax.servlet.ServletException;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
@@ -27,6 +30,7 @@ import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
|
|
|
@Slf4j
|
|
|
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
|
|
|
private final AuthenticationManager authenticationManager;
|
|
|
+ private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
|
|
public CustomAuthenticationFilter(AuthenticationManager authenticationManager){
|
|
|
this.authenticationManager = authenticationManager;
|
|
|
@@ -34,12 +38,26 @@ public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFi
|
|
|
|
|
|
@Override
|
|
|
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
|
|
|
- String email = request.getParameter("username");
|
|
|
- String password = request.getParameter("password");
|
|
|
- log.info("Email is: {}", email);
|
|
|
- log.info("Password is: {}", password);
|
|
|
- UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email, password);
|
|
|
- return authenticationManager.authenticate(authenticationToken);
|
|
|
+
|
|
|
+ try {
|
|
|
+ BufferedReader reader = request.getReader();
|
|
|
+ StringBuilder stringBuilder = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ stringBuilder.append(line);
|
|
|
+ }
|
|
|
+ LoginCredentials credentials = objectMapper.readValue(stringBuilder.toString(), LoginCredentials.class);
|
|
|
+
|
|
|
+ String email = credentials.getUsername();
|
|
|
+ String password = credentials.getPassword();
|
|
|
+ log.info("Email is: {}", email);
|
|
|
+ log.info("Password is: {}", password);
|
|
|
+ UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email, password);
|
|
|
+ return authenticationManager.authenticate(authenticationToken);
|
|
|
+ }
|
|
|
+ catch(IOException e){
|
|
|
+ throw new IllegalArgumentException(e.getMessage());
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|