Spring-Security的自定义过滤器

分手后的思念是犯贱 2021-08-19 20:44 540阅读 0赞

一 参考文章

http://www.spring4all.com/article/422

二 代码位置

https://github.com/cakin24/spring-security-demos/tree/master/02%20-%20%E8%87%AA%E5%AE%9A%E4%B9%89%E7%99%BB%E5%BD%95

三 关键代码

1 过滤器定义

  1. package com.spring4all.config;
  2. import org.springframework.http.HttpMethod;
  3. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  4. import org.springframework.security.core.Authentication;
  5. import org.springframework.security.core.AuthenticationException;
  6. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  7. import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
  8. import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. /**
  16. * 自定义表单登录
  17. */
  18. public class CustomFromLoginFilter extends AbstractAuthenticationProcessingFilter {
  19. CustomFromLoginFilter(String defaultFilterProcessesUrl) {
  20. super(new AntPathRequestMatcher(defaultFilterProcessesUrl, HttpMethod.POST.name()));
  21. }
  22. @Override
  23. public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
  24. String username = httpServletRequest.getParameter("username");
  25. String password = httpServletRequest.getParameter("password");
  26. customCheck(username, password);
  27. List<SimpleGrantedAuthority> simpleGrantedAuthorities = new ArrayList<>();
  28. simpleGrantedAuthorities.add(new SimpleGrantedAuthority("USER"));
  29. return new UsernamePasswordAuthenticationToken(username, password, simpleGrantedAuthorities);
  30. }
  31. private void customCheck(String username, String password){
  32. if (!("anoyi".equals(username) && "anoyi".equals(password))){
  33. throw new RuntimeException("用户名或密码错误!");
  34. }
  35. }
  36. }

2 过滤器配置

  1. package com.spring4all.config;
  2. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  3. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
  6. @EnableWebSecurity
  7. public class WebSecurityConfig extends WebSecurityConfigurerAdapter{
  8. /**
  9. * 匹配 "/" 路径,不需要权限即可访问
  10. * 匹配 "/user" 及其以下所有路径,都需要 "USER" 权限
  11. * 退出登录的地址为 "/logout",退出成功后跳转到页面 "/login"
  12. * 默认启用 CSRF
  13. */
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. http
  17. .authorizeRequests()
  18. .antMatchers("/").permitAll()
  19. .antMatchers("/user/**").hasAuthority("USER")
  20. .and()
  21. .logout().logoutUrl("/logout").logoutSuccessUrl("/login");
  22. http.addFilterAt(customFromLoginFilter(), UsernamePasswordAuthenticationFilter.class);
  23. }
  24. /**
  25. * 自定义认证过滤器
  26. */
  27. private CustomFromLoginFilter customFromLoginFilter() {
  28. return new CustomFromLoginFilter("/login");
  29. }
  30. }

addFilterAt该函数的用法参考: https://blog.csdn.net/qq_36882793/article/details/102869583

四 调试

我们调试下看看有哪些过滤器,以及过滤器的执行顺序。

1 在下面两个过滤器中设置断点

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70 1

2 浏览器输入: http://localhost:8080/login

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70 2

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70 3

从调试结果看,先执行优先级高的过滤器。

发表评论

表情:
评论列表 (有 0 条评论,540人围观)

还没有评论,来说两句吧...

相关阅读