【问题标题】:Spring filter not able to bypass certain requestSpring过滤器无法绕过某些请求
【发布时间】:2020-09-09 21:10:31
【问题描述】:
@EnableWebSecurity
public class SecurityTokenConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .csrf().disable()
        // make sure we use stateless session; session won't be used to store user's state.
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)     
    .and()
        // handle an authorized attempts 
        .exceptionHandling().authenticationEntryPoint((req, rsp, e) -> rsp.sendError(HttpServletResponse.SC_UNAUTHORIZED))  
    .and()
       // Add a filter to validate the tokens with every request
       .addFilterBefore(new JwtTokenAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class)
    // authorization requests config
    .authorizeRequests()
       // allow all who are accessing "auth" service
       .antMatchers(HttpMethod.POST, "/${basePath:/}${version:/}/session/**").permitAll()  
       // must be an admin if trying to access admin area (authentication is also required here)
       // Any other request must be authenticated
       .anyRequest().authenticated(); 

}

}

上述过滤器无法允许 /session 请求绕过“JwtTokenAuthenticationFilter”。

有人知道这是什么问题吗?

【问题讨论】:

    标签: java spring-boot spring-mvc spring-security jwt


    【解决方案1】:

    您可以通过覆盖configure(WebSecurity) 方法以另一种方式实现它,如下所示:

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("**/session/**");
    }
    

    并删除antMatchers(HttpMethod.POST, "/${basePath:/}${version:/}/session/**").permitAll()

    【讨论】:

    • 不,它仍然会进入那个过滤器
    猜你喜欢
    • 2014-07-01
    • 2020-08-09
    • 2014-01-31
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2016-06-24
    • 2013-04-13
    相关资源
    最近更新 更多