需要先增加一个自定义的Filter去继承 UsernamePasswordAuthenticationFilter 或者 AbstractAuthenticationProcessingFilter
然后在自定义的Filter里面指定登录的Url . 设置过滤器的时候,必须为过滤器指定一个 authenticationManager ,并且初始化构造函数的时候,要传入该manager.
再编写一个Provider , 把自定义的UserDetailService传给该provider.
具体实现过程如下:
添加配置:
@Override
protected void configure(AuthenticationManagerBuilder auth) {
//增加自定义的UserDetailService
userDetailsAuthenticationProvider.setUserDetailsService(userDetailsService);
//设置一个Provider
auth.authenticationProvider(userDetailsAuthenticationProvider);
}
关键配置:
@Override
protected void configure(HttpSecurity http) throws Exception {
//手动实现的权限验证管理器
movieAuthorizeConfigProviderManager.configure(http.authorizeRequests());
//添加前台登录验证过滤器与userDetailService,不同的登录处理URL需要在Filter里面指定
UserAuthenticationFilter userAuthenticationFilter = new UserAuthenticationFilter();
//每个Filter必须指定一个authenticationManager
userAuthenticationFilter.setAuthenticationManager(authenticationManager());
//设置登录成功处理事件
userAuthenticationFilter.setAuthenticationSuccessHandler(movieAuthenticationSuccessHandler);
//设置登录失败处理事件
userAuthenticationFilter.setAuthenticationFailureHandler(movieAuthenticationFailureHandler);
http.addFilterBefore(userAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
}
完整配置: