继承的于AbstractAuthenticationProcessingFilter的自定义认证的问题

来源:4-7 图片验证码

过客12

2020-04-23

继承的于AbstractAuthenticationProcessingFilter后,加入了filters中,但是使用它匹配的路径不会经过这个filter。

@Configuration
@EnableWebSecurity
class MyWebSecurityConfig : WebSecurityConfigurerAdapter() {

    @Autowired
    lateinit var authenticationFailureHandler: LeavingAuthenticationFailureHandler

    @Autowired
    lateinit var authenticationSuccessHandler: LeavingAuthenticationSuccessHandler

    @Autowired
    lateinit var studentUserDetailsServiceImpl: StudentUserDetailsServiceImpl

    @Autowired
    lateinit var adminUserDetailsServiceImpl: AdminUserDetailsServiceImpl

    @Autowired
    lateinit var mapper: ObjectMapper

    override fun configure(http: HttpSecurity) {
        //
        //
        //val manager = http.getSharedObject(AuthenticationManager::class.java)
        //println(manager)
        //restAuthenticationFilter.setAuthenticationManager(manager)
        http    .authenticationProvider(RestDaoAuthenticationProvider(studentUserDetailsServiceImpl, adminUserDetailsServiceImpl))
                .addFilterAt(authenticationFilter(), UsernamePasswordAuthenticationFilter::class.java)
                .authorizeRequests()
                //.antMatchers("/captcha.jpg").permitAll()
                .antMatchers("/api/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().disable()
                .httpBasic().disable()
                //.loginPage("/login")
                //.loginProcessingUrl("/login")
                //.successHandler(authenticationSuccessHandler)
                //.failureHandler(authenticationFailureHandler)
                .csrf().disable()
                .cors()
    }

    @Bean
    fun authenticationFilter() : RestAuthenticationFilter{
        val restAuthenticationFilter = RestAuthenticationFilter(mapper)
        restAuthenticationFilter.setAuthenticationFailureHandler(authenticationFailureHandler)
        restAuthenticationFilter.setAuthenticationSuccessHandler(authenticationSuccessHandler)
        restAuthenticationFilter.setAuthenticationManager(authenticationManagerBean())
        return restAuthenticationFilter
    }
}

老师帮我看看哪里配置有问题呢?
路径全都是“/api/login”,但是不会经过这个filter,调试也进不去匹配。。。
找了一晚上了,百度也搜了很多,没有用。希望老师能够帮我这个忙,万分感谢!

写回答

1回答

JoJo

2020-04-29

addFilterAt 换成 addFilterBefore 试下。

0
0

Spring Security技术栈开发企业级认证与授权

Spring Security技术栈,REST风格开发常见接口,独立开发认证授权模块保证REST服务安全

2662 学习 · 1561 问题

查看课程