|
1
|
|
|
package unicon.matthews.admin; |
|
2
|
|
|
|
|
3
|
|
|
import java.io.IOException; |
|
4
|
|
|
|
|
5
|
|
|
import javax.servlet.FilterChain; |
|
6
|
|
|
import javax.servlet.ServletException; |
|
7
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
8
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
9
|
|
|
|
|
10
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
11
|
|
|
import org.slf4j.Logger; |
|
12
|
|
|
import org.slf4j.LoggerFactory; |
|
13
|
|
|
import org.springframework.http.HttpMethod; |
|
14
|
|
|
import org.springframework.security.authentication.AuthenticationServiceException; |
|
15
|
|
|
import org.springframework.security.core.Authentication; |
|
16
|
|
|
import org.springframework.security.core.AuthenticationException; |
|
17
|
|
|
import org.springframework.security.core.context.SecurityContextHolder; |
|
18
|
|
|
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter; |
|
19
|
|
|
import org.springframework.security.web.authentication.AuthenticationFailureHandler; |
|
20
|
|
|
import org.springframework.security.web.authentication.AuthenticationSuccessHandler; |
|
21
|
|
|
|
|
22
|
|
|
import unicon.matthews.common.WebUtil; |
|
23
|
|
|
import unicon.matthews.security.auth.ajax.AjaxLoginProcessingFilter; |
|
24
|
|
|
import unicon.matthews.security.auth.ajax.LoginRequest; |
|
25
|
|
|
import unicon.matthews.security.exception.AuthMethodNotSupportedException; |
|
26
|
|
|
|
|
27
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
28
|
|
|
|
|
29
|
|
View Code Duplication |
public class AdminUserProcessingFilter extends AbstractAuthenticationProcessingFilter { |
|
|
|
|
|
|
30
|
|
|
private static Logger logger = LoggerFactory.getLogger(AjaxLoginProcessingFilter.class); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
private final AuthenticationSuccessHandler successHandler; |
|
33
|
|
|
private final AuthenticationFailureHandler failureHandler; |
|
34
|
|
|
|
|
35
|
|
|
private final ObjectMapper objectMapper; |
|
36
|
|
|
|
|
37
|
|
|
public AdminUserProcessingFilter(String defaultProcessUrl, AuthenticationSuccessHandler successHandler, |
|
38
|
|
|
AuthenticationFailureHandler failureHandler, ObjectMapper mapper) { |
|
39
|
|
|
super(defaultProcessUrl); |
|
40
|
|
|
this.successHandler = successHandler; |
|
41
|
|
|
this.failureHandler = failureHandler; |
|
42
|
|
|
this.objectMapper = mapper; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
@Override |
|
46
|
|
|
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, |
|
47
|
|
|
ServletException { |
|
48
|
|
|
if (!HttpMethod.POST.name().equals(request.getMethod()) || !WebUtil.isAjax(request)) { |
|
49
|
|
|
if (logger.isDebugEnabled()) { |
|
50
|
|
|
logger.debug("Authentication method not supported. Request method: " + request.getMethod()); |
|
51
|
|
|
} |
|
52
|
|
|
throw new AuthMethodNotSupportedException("Authentication method not supported"); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
LoginRequest loginRequest = objectMapper.readValue(request.getReader(), LoginRequest.class); |
|
56
|
|
|
|
|
57
|
|
|
if (StringUtils.isBlank(loginRequest.getUsername()) || StringUtils.isBlank(loginRequest.getPassword())) { |
|
58
|
|
|
throw new AuthenticationServiceException("Username or Password not provided"); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
AdminUserAuthenticationToken token = new AdminUserAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()); |
|
62
|
|
|
|
|
63
|
|
|
return this.getAuthenticationManager().authenticate(token); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
@Override |
|
67
|
|
|
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) |
|
68
|
|
|
throws IOException, ServletException { |
|
69
|
|
|
successHandler.onAuthenticationSuccess(request, response, authResult); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
@Override |
|
73
|
|
|
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) |
|
74
|
|
|
throws IOException, ServletException { |
|
75
|
|
|
SecurityContextHolder.clearContext(); |
|
76
|
|
|
failureHandler.onAuthenticationFailure(request, response, failed); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|