Security 自定义 UsernamePasswordAuthenticationFilter 替换原拦截器

4 篇文章 0 订阅
1 篇文章 0 订阅

背景:

默认的 UsernamePasswordAuthenticationFilter过滤器通过 from 表单 post 请求形式获取用户名密码参数,并且请求头为 application/x-www-form-urlencoded(官方登陆源码说明:默认登陆表单), 并通过 ==request.getParameter(this.usernameParameter)==获取用户名密码
参考源码 :

@Override
	public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
			throws AuthenticationException {
		if (this.postOnly && !request.getMethod().equals("POST")) {
			throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
		}
		String username = obtainUsername(request);
		username = (username != null) ? username : "";
		username = username.trim();
		String password = obtainPassword(request);
		password = (password != null) ? password : "";
		UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);
		// Allow subclasses to set the "details" property
		setDetails(request, authRequest);
		return this.getAuthenticationManager().authenticate(authRequest);
	}

然而,按实际需求我们可能需要ajax发送 json格式的请求参数(包含用户名和密码),默认的过滤器并不能满足需求,在 UserDetailService中拿到的 username会一直为 null
这种情况下我们需要自定义一个 UsernamePasswordAuthenticationFilter来处理 json 数据格式的登陆请求

说明:

鉴于官方已有 UsernamePasswordAuthenticationFilter过滤器,所以我们可以直接复制一个过来修改一下即可

public class UsernamePasswordJsonAuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    public static final String SPRING_SECURITY_FORM_USERNAME_KEY = "username";

    public static final String SPRING_SECURITY_FORM_PASSWORD_KEY = "password";

    /**
     * 登陆拦截请求
     */
    private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER = new AntPathRequestMatcher("/user/login",
            "POST");

    private String usernameParameter = SPRING_SECURITY_FORM_USERNAME_KEY;

    private String passwordParameter = SPRING_SECURITY_FORM_PASSWORD_KEY;

    private boolean postOnly = true;

    public UsernamePasswordJsonAuthenticationFilter() {
        super(DEFAULT_ANT_PATH_REQUEST_MATCHER);
    }

    public UsernamePasswordJsonAuthenticationFilter(AuthenticationManager authenticationManager) {
        super(DEFAULT_ANT_PATH_REQUEST_MATCHER, authenticationManager);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {
        if (this.postOnly && !request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }
        //  在这里取消原有的通过 request parameter 形式获取用户名密码的方法, 替换为 json 数据格式的获取
        ObjectMapper mapper = new ObjectMapper();
        UsernamePasswordAuthenticationToken authRequest = null;
        try (InputStream is = request.getInputStream()) {
            Map<String,String> authenticationBean = mapper.readValue(is, Map.class);
            authRequest = new UsernamePasswordAuthenticationToken(
                    authenticationBean.get("username"), authenticationBean.get("password"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 源码方法
        /*String username = obtainUsername(request);
        username = (username != null) ? username : "";
        username = username.trim();
        String password = obtainPassword(request);
        password = (password != null) ? password : "";
        UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username, password);*/
        // Allow subclasses to set the "details" property
        setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }
	// ... 以下省略其他源码方法
}

这样的话就可以获取到 json 中的用户名和密码了。
在这里有个需要注意的地方

private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER = new AntPathRequestMatcher("/user/login",
            "POST");

这一句配置的是自定义登陆的拦截请求,也就是 Security 中配置的 loginProcessingUrl,而默认的拦截器中配置的是:

private static final AntPathRequestMatcher DEFAULT_ANT_PATH_REQUEST_MATCHER = new AntPathRequestMatcher("/login",
			"POST");

我们自定义的拦截器需要填上自己的登陆 API才能走自定义的拦截器。
下一步是将自定义的过滤器注册到 Security 中。

 /**
     * 自定义登录拦截器
     */
    @Bean
    UsernamePasswordJsonAuthenticationFilter usernamePasswordJsonAuthenticationFilter() throws Exception {
        UsernamePasswordJsonAuthenticationFilter authenticationFilter = new UsernamePasswordJsonAuthenticationFilter();
        authenticationFilter.setAuthenticationSuccessHandler(myAuthenticationSuccessHandler);
        authenticationFilter.setAuthenticationFailureHandler(myAuthenticationFailureHandler);
        authenticationFilter.setAuthenticationManager(authenticationManagerBean());
        return authenticationFilter;
    }

    /**
     *  注意 :在使用自定义的登陆拦截器后 loginProcessingUrl 可以不配置了,在拦截器中也可以配置
     *  当然这里也可以配置但是必须得和拦截器中的一样       
     */

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests((authorize) -> authorize
                        .antMatchers("/css/**", "/index", "/user/login", "/authentication/require").permitAll()
                        .antMatchers("/user/info").hasRole("USER")
                )
                .userDetailsService(userDetailsService)
                .formLogin((formLogin) -> formLogin
                        .loginPage("/authentication/require")
//                        .loginProcessingUrl("/user/login")
                        .successHandler(myAuthenticationSuccessHandler)
                        .failureHandler(myAuthenticationFailureHandler)
                )
                .csrf().disable()
                .cors();
        http.addFilterAt(usernamePasswordJsonAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
    }

部分同学会经常遇到添加了自定义的拦截器还是走原拦截器的大多数是因为这里还是配置了 loginProcessingUrl,如果实在是想在这里配置也可以,但是配置的 API必须和自定义拦截器的
DEFAULT_ANT_PATH_REQUEST_MATCHER 的 API 一致才行


好了,到此前端就可以通过发送 json 格式的数据来登陆了

谢谢参观,仅供参考 😄🎉

  • 19
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
Spring Security 6中自定义权限过滤器的步骤如下: 1.创建一个类并实现`org.springframework.web.filter.OncePerRequestFilter`接口。 2.覆盖`doFilterInternal`方法,该方法接收`HttpServletRequest`和`HttpServletResponse`对象作为参数,并在其中编写自定义过滤器的逻辑。 3.使用`@Component`注释将自定义过滤器类标记为Spring组件。 4.在Spring Security配置类中使用`http.addFilterBefore()`方法将自定义过滤器添加到过滤器链中。 下面是一个示例代码,演示如何在Spring Security 6中创建自定义权限过滤器: ```java import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; @Component public class CustomAuthorizationFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { // 在这里编写自定义过滤器的逻辑 // 检查用户是否有足够的权限访问请求的资源 // 如果没有权限,可以返回HTTP 403 Forbidden响应 // 如果有权限,可以继续处理请求 filterChain.doFilter(request, response); } } ``` 在Spring Security配置类中添加以下代码: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private CustomAuthorizationFilter customAuthorizationFilter; @Override protected void configure(HttpSecurity http) throws Exception { http.addFilterBefore(customAuthorizationFilter, UsernamePasswordAuthenticationFilter.class) .authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasRole("USER") .anyRequest().authenticated() .and() .formLogin() .and() .httpBasic(); } } ``` 在上面的示例中,我们创建了一个名为`CustomAuthorizationFilter`的自定义过滤器,并将其添加到Spring Security过滤器链中。在Spring Security配置类中,我们使用`http.addFilterBefore()`方法将自定义过滤器添加到过滤器链中,并使用`authorizeRequests()`方法配置了请求的授权规则。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值