【发布时间】:2019-07-09 10:01:32
【问题描述】:
我想检查不同端点的不同身份验证方法。我想使用的方法是 x509 和 jwt。我需要 only x509 用于某些端点,并使用 JWT 处理所有其他请求。
这是我的网络安全配置:
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Configuration
@Order(1)
public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/transaction/testf").authenticated().and()
.x509()
.subjectPrincipalRegex("CN=(.*?)(?:,|$)")
.userDetailsService(new X509UserDetailsService())
;
}
}
@Configuration
@Order(2)
public static class ApiTokenSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/oauth/token", "/api/dealer/login").permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
;
}
}
}
此配置仅检查 /api/transaction/testf 端点的 x509 证书,并允许所有其他端点响应。我需要其他端点在没有 jwt 令牌的情况下返回 503。
【问题讨论】:
-
对于每个过滤器链,您必须为该链指定蚂蚁模式。
http.antMatcher(不是 http.authorizeRequests 作为第一次调用)这篇文章描述了你需要什么:stackoverflow.com/questions/54657163/…
标签: spring spring-boot spring-security jwt x509