为啥尽管 web.ignoring().antMatchers() 仍然在此端点上调用我的过滤器? [复制]
Posted
技术标签:
【中文标题】为啥尽管 web.ignoring().antMatchers() 仍然在此端点上调用我的过滤器? [复制]【英文标题】:Why is my filter still being called on this endpoint despite web.ignoring().antMatchers()? [duplicate]为什么尽管 web.ignoring().antMatchers() 仍然在此端点上调用我的过滤器? [复制] 【发布时间】:2018-07-05 12:01:18 【问题描述】:我有一个端点 (GetMapping( "/testendpoint" )
),我想在不运行身份验证过滤器的情况下离开。但无论我做什么,它仍然调用过滤器并将其拒绝为 401,因为标题中没有 Jwt。
注意:它只调用一次过滤器。
如何使其不安全并使其不调用过滤器?
@EnableWebSecurity
@Import( Config.class )
@EnableConfigurationProperties( JwtProperties.class )
public class SecurityConfig extends WebSecurityConfigurerAdapter
@Autowired
private JwtProperties jwtProperties;
@Override
public void configure(WebSecurity web) throws Exception
web.ignoring()
.antMatchers( "/swagger-ui.html" ) //This goes through without security
.antMatchers( "/metrics" ) //This calls the filter
.antMatchers( "/testendpoint" ); //This calls the filter
@Override
protected void configure(HttpSecurity http) throws Exception
JWTAuthenticationFilter jwtAuthenticationFilter = new JWTAuthenticationFilter( jwtProperties );
ExceptionHandlerFilter exceptionHandlerFilter = new ExceptionHandlerFilter();
http.csrf().disable().authorizeRequests()
.antMatchers( "/" ).permitAll()
.antMatchers( "/testendpoint" ).permitAll() //Doesn't stop the filter
.and()
.addFilterAfter( jwtAuthenticationFilter, BasicAuthenticationFilter.class )
.addFilterBefore( exceptionHandlerFilter, JWTAuthenticationFilter.class );
/* Adding the below also did not work
.authorizeRequests()
.antMatchers( "/testendpoint/**" ).permitAll();
*/
我的端点是一个执行器端点:
@Configuration
@ManagementContextConfiguration
@CacheController
public class TestController extends AbstractMvcEndpoint
@GetMapping( value = "/testendpoint", produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
public String getSomething(HttpServletRequest request) throws Exception
return "hello";
【问题讨论】:
试试web.ignoring().antMatchers( "/swagger-ui.html", "/metrics", "/testendpoint");
@Ritesh 那没解决
@dur 这仍然不是我的答案。问题是当它应该允许我的“/testendpoint”时它抛出了一个未经授权的异常。这就是我要问的问题。
@DonRhummy:请阅读另一个问题并像接受的答案那样做。当然,您的过滤器正在抛出异常,因为它根本不应该被执行。
【参考方案1】:
如果您有刚刚在测试期间使用的端点,只需将其放在src/test/java
而不是src/main/java
。这样它只会在测试阶段自动装配,不会出现在生产代码中。
这样你就可以跳过生产 spring 安全配置中的忽略配置。
【讨论】:
在正常运行时使用,而不仅仅是测试以上是关于为啥尽管 web.ignoring().antMatchers() 仍然在此端点上调用我的过滤器? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
尽管 REST 具有以下优势,为啥 Google 仍使用大量 SOAP?
尽管存在`@Transactional`,为啥这些数据库修改不回滚?