Çağ*_*lur 12 spring spring-security x509 jwt spring-boot
我想检查不同端点的不同身份验证方法。我想使用的方法是 x509 和 jwt。我只需要对某些端点使用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()
;
}
}
}
Run Code Online (Sandbox Code Playgroud)
此配置仅检查/api/transaction/testf端点的 x509 证书,并允许所有其他端点响应。我需要其他端点在没有 jwt 令牌的情况下返回 503。
您有两个过滤器链。它们都没有正确配置的入口点模式http.antMatcher。这意味着它们被配置为/**用作它们的入口点模式。
例如
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().fullyAuthenticated()
Run Code Online (Sandbox Code Playgroud)
是一样的东西说:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.authorizeRequests()
.anyRequest().fullyAuthenticated()
Run Code Online (Sandbox Code Playgroud)
我们在这里说的是
http - 安全过滤器链http.antMatcher - 安全过滤器链的入口点http.authorizeRequests - 开始我的端点访问限制http.authorizeRequests.antMatchers - 具有特定访问权限的 URL 列表所以你需要做的是改变你的@Order(1)过滤器链来缩小模式。例如:http.antMatcher("/api/transaction/**")
您的配置现在看起来像
@Configuration
@Order(1)
public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/api/transaction/**") //customized entry point
.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
.antMatcher("/**") //this is default
.authorizeRequests()
.antMatchers("/oauth/token", "/api/dealer/login").permitAll()
.and()
.authorizeRequests()
.anyRequest()
.authenticated()
;
}
Run Code Online (Sandbox Code Playgroud)
使用您现有的配置,命名的过滤器链ApiWebSecurityConfig将捕获所有调用。ApiTokenSecurityConfig从未使用过另一个过滤器链。
您可以在此答案中看到另一个描述
SpringSecurity:仅通过单个端点即可实现 RESTful API 基本身份验证
| 归档时间: |
|
| 查看次数: |
4312 次 |
| 最近记录: |