content: package com.spring4all.config;
  
  import org.springframework.http.HttpMethod;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.AuthenticationException;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
  import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * 自定义表单登录
   */
  public class CustomFromLoginFilter extends AbstractAuthenticationProcessingFilter {
  
      CustomFromLoginFilter(String defaultFilterProcessesUrl) {
          super(new AntPathRequestMatcher(defaultFilterProcessesUrl, HttpMethod.POST.name()));
      }
  
      @Override
      public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
          String username = httpServletRequest.getParameter("username");
          String password = httpServletRequest.getParameter("password");
          customCheck(username, password);
          List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
          simpleGrantedAuthorities.add(new SimpleGrantedAuthority("USER"));
          return new UsernamePasswordAuthenticationToken(username, password, simpleGrantedAuthorities);
      }
  
      private void customCheck(String username, String password){
          if (!("anoyi".equals(username) && "anoyi".equals(password))){
              throw new RuntimeException("用户名或密码错误！");
          }
  
      }
  
  }
  