unicon.matthews.admin.AdminUserProcessingFilter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 48
loc 48
rs 10
eloc 32
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A unsuccessfulAuthentication(HttpServletRequest,HttpServletResponse,AuthenticationException) 5 5 1
A successfulAuthentication(HttpServletRequest,HttpServletResponse,FilterChain,Authentication) 4 4 1
A AdminUserProcessingFilter(String,AuthenticationSuccessHandler,AuthenticationFailureHandler,ObjectMapper) 6 6 1
B attemptAuthentication(HttpServletRequest,HttpServletResponse) 19 19 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
30
  private static Logger logger = LoggerFactory.getLogger(AjaxLoginProcessingFilter.class);
0 ignored issues
show
Comprehensibility introduced by
Your field or variable is shadowing field logger in the class GenericFilterBean. Consider renaming it.
Loading history...
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