小编tle*_*and的帖子

使用Spring Boot对身份验证过滤器进行集成测试

我想实现一个集成测试,以测试我的身份验证过滤器,该过滤器是通过Spring Security和Spring Boot实现的。但是...我迷路了...

首先,这是我的“生产”实现:

我让我的Web配置器适配器创建一个身份验证管理器并声明我的过滤器:

@EnableWebSecurity
public class SecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Autowired
    private IdentityService loginService;
    @Autowired
    private PersonService personService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(loginService).passwordEncoder(new BCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers(PATH_LOGIN).permitAll();
        http.authorizeRequests().antMatchers("/**").fullyAuthenticated();

        http.addFilterBefore(new AuthenticationFilter(PATH_LOGIN, authenticationManager(), personService),
            UsernamePasswordAuthenticationFilter.class);
    }
Run Code Online (Sandbox Code Playgroud)

然后,这是我的过滤器实现:

public class AuthenticationFilter extends AbstractAuthenticationProcessingFilter {

    private ObjectMapper objectMapper = new ObjectMapper();

    private PersonService personService;

    protected AuthenticationFilter(String loginPath, AuthenticationManager authenticationManager,
        PersonService personService) {
        super(loginPath);
        this.personService = personService;
        setAuthenticationManager(authenticationManager);
    }

    @Override
    public Authentication …
Run Code Online (Sandbox Code Playgroud)

unit-testing mocking spring-security spring-boot

5
推荐指数
1
解决办法
4540
查看次数