自定义认证过滤器:
    UsernamePasswordAuthenticationFilter认证过滤器 此过滤器只接收form表单请求 且只过滤用户名和密码
    我们可以仿造此类 自己构造认证过滤器

    1)先继承AbstractAuthenticationProcessingFilter对象
    2)构造方法中定义父类的有参构造方法 参数为过滤路径
    3)重写attemptAuthentication方法
    4)方法中先判断是否为ajax请求
    5)如果是 就把请求通过jackson对象把输入流转换为map对象 并获取值
    6)通过值 生成token
    7)交给认证管理器认证token

    此认证方法只获取了用户名和密码 顺便做了验证码的校对 通过验证之后再执行认证通过之后的方法

    1. /**
    2. * @author by itheima
    3. * @Date 2022/1/21
    4. * @Description
    5. */
    6. public class MyUserNamePasswordAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    7. /**
    8. * 设置构造,传入自定义登录url地址
    9. * @param defaultFilterProcessesUrl
    10. */
    11. public MyUserNamePasswordAuthenticationFilter(String defaultFilterProcessesUrl) {
    12. super(defaultFilterProcessesUrl);
    13. }
    14. @Override
    15. public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
    16. //从post请求流中获取登录信息
    17. String username=null;
    18. String password=null;
    19. try {
    20. //判断 是否是ajax登录
    21. if (request.getContentType().equalsIgnoreCase(MediaType.APPLICATION_JSON_VALUE) || request.getContentType().equalsIgnoreCase(MediaType.APPLICATION_JSON_UTF8_VALUE)) {
    22. ServletInputStream in = request.getInputStream();
    23. HashMap<String,String> map = new ObjectMapper().readValue(in, HashMap.class);
    24. username=map.get("username");
    25. password=map.get("password");
    26. }
    27. } catch (IOException e) {
    28. e.printStackTrace();
    29. }
    30. //生成认证token
    31. UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
    32. //交给认证管理器取认证token
    33. return this.getAuthenticationManager().authenticate(token);
    34. }
    35. /**
    36. * 认证成功处理方法
    37. * @param request
    38. * @param response
    39. * @param chain
    40. * @param authResult
    41. * @throws IOException
    42. * @throws ServletException
    43. */
    44. @Override
    45. protected void successfulAuthentication(HttpServletRequest request,
    46. HttpServletResponse response,
    47. FilterChain chain,
    48. Authentication authResult) throws IOException, ServletException {
    49. response.getWriter().write("login success 666.....");
    50. }
    51. /**
    52. * 认证失败处理方法
    53. * @param request
    54. * @param response
    55. * @param failed
    56. * @throws IOException
    57. * @throws ServletException
    58. */
    59. @Override
    60. protected void unsuccessfulAuthentication(HttpServletRequest request,
    61. HttpServletResponse response,
    62. AuthenticationException failed) throws IOException, ServletException {
    63. response.getWriter().write("login failue 999");
    64. }
    65. }

    在Security配置类中配置过滤器
    1)把过滤器bean加入ioc容器 里面设置认证管理器
    2)在configure方法中定义过滤器执行先后

    1. /**
    2. * 配置授权策略
    3. * @param http
    4. * @throws Exception
    5. */
    6. @Override
    7. protected void configure(HttpSecurity http) throws Exception {
    8. http
    9. //坑-过滤器要添加在默认过滤器之前,否则,登录失效
    10. .addFilterBefore(myUserNamePasswordAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
    11. .csrf().disable();
    12. }
    13. @Bean
    14. public MyUserNamePasswordAuthenticationFilter myUserNamePasswordAuthenticationFilter() throws Exception {
    15. //设置默认登录路径
    16. MyUserNamePasswordAuthenticationFilter myUserNamePasswordAuthenticationFilter =
    17. new MyUserNamePasswordAuthenticationFilter("/authentication/form");
    18. //设置认证管理器 固定写法myUserNamePasswordAuthenticationFilter.setAuthenticationManager(authenticationManagerBean());
    19. return myUserNamePasswordAuthenticationFilter;
    20. }