Spring boot 在SpringBoot安全过滤器处理中,GET和POST是否可能有不同的过滤器?
我试图实现的是允许GET使用一个简单的apiKey,但需要一个基于经过身份验证的用户的JWT令牌来发布/更新/删除 我在想这样的事情Spring boot 在SpringBoot安全过滤器处理中,GET和POST是否可能有不同的过滤器?,spring-boot,spring-security,Spring Boot,Spring Security,我试图实现的是允许GET使用一个简单的apiKey,但需要一个基于经过身份验证的用户的JWT令牌来发布/更新/删除 我在想这样的事情 httpSecurity. antMatcher("/api/**"). csrf().disable(). sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
httpSecurity.
antMatcher("/api/**").
csrf().disable().
sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).
and()
.addFilter(postFilter)
.authorizeRequests()
.antMatchers(HttpMethod.POST, "/**").authenticated()
.antMatchers(HttpMethod.PUT, "/**").authenticated()
.antMatchers(HttpMethod.DELETE, "/**").authenticated().
and()
.addFilter(getFilter)
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/**").authenticated();
这不会获得所需的行为,此设置将路由到过滤器,根据哪个标头通过apiKey vs jwtToken。定义一个(或两个)
OncePerRequestFilter
并使用提供的HttpServletRequest
对象的getMethod()
来管理您的用例有什么问题?但是,在这种情况下,您应该删除行antMatchers(HttpMethod.XXX,“/**”).authenticated()
,因为您将使用新的筛选器来管理这种情况,我的意思是,anyRequest().authenticated()和()