3
@SuppressWarnings("SpringJavaAutowiringInspection")
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private JwtAuthenticationEntryPoint unauthorizedHandler;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
public void configureAuthentication(AuthenticationManagerBuilder
authenticationManagerBuilder) throws Exception {
authenticationManagerBuilder.userDetailsService(userDetailsService);
}
@Bean
public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
return new JwtAuthenticationTokenFilter();
}
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.csrf().disable()
.exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler)
.and()
.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/test").permitAll()
.antMatchers("/api/**").permitAll()
.anyRequest().authenticated();
httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
}
}
我有一個在Spring Security之前運行的自定義過濾器。我希望能夠從篩選器中排除一些URL(例如/test
),並且Spring Security和其他人將被攔截(如/api/**
)。Spring Security排除了自定義過濾器上的URL
當使用郵遞員測試localhost/test
它仍然通過過濾器,即使我有antMatchers("/test").permitAll()
。
如何繞過過濾器?
@YoungHyunYoo:如果你想排除整個配置,你也可以使用'antMatcher'(而不是'antMatchers'),另請參閱https://stackoverflow.com/questions/35890540/when-to-use-彈簧securitys-antmatcher。 – dur