/
CustomAuthenticationFilter.java
37 lines (31 loc) · 1.64 KB
/
CustomAuthenticationFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.mang.example.security.config.security;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mang.example.security.app.user.domain.User;
import com.mang.example.security.exception.InputNotFoundException;
import lombok.extern.log4j.Log4j2;
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.web.authentication.UsernamePasswordAuthenticationFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Log4j2
public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
public CustomAuthenticationFilter(final AuthenticationManager authenticationManager) {
super.setAuthenticationManager(authenticationManager);
}
@Override
public Authentication attemptAuthentication(final HttpServletRequest request, final HttpServletResponse response) throws AuthenticationException{
final UsernamePasswordAuthenticationToken authRequest;
try{
final User user = new ObjectMapper().readValue(request.getInputStream(), User.class);
authRequest = new UsernamePasswordAuthenticationToken(user.getEmail(), user.getPw());
} catch (IOException exception){
throw new InputNotFoundException();
}
setDetails(request, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
}