使用查询参数绕过对 url 的授权

Bypass authorisation for url with query parameters

提问人:Rashmi Patil 提问时间:11/17/2023 更新时间:11/18/2023 访问量:24

问:

我的项目中有 spring security 和 JWT auth。因此,有一个类 WebSecurityConfig.java 具有以下方法,允许在未经授权的情况下允许某些 API

@Bean
    public SecurityFilterChain filterChain(HttpSecurity httpSecurity, JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint,
                                           JwtRequestFilter jwtRequestFilter) throws Exception {

        httpSecurity.csrf(AbstractHttpConfigurer::disable)
                .exceptionHandling(configurer -> configurer
                        .authenticationEntryPoint(jwtAuthenticationEntryPoint))
                .sessionManagement(configurer -> configurer
                        .sessionCreationPolicy(SessionCreationPolicy.STATELESS))
                .authorizeHttpRequests(authorize ->
                        authorize.requestMatchers("/authenticate").permitAll()
                                .requestMatchers("/get-is-account-locked").permitAll()
                                .requestMatchers("/*").permitAll()
                                .anyRequest().authenticated());

     
        if (sslEnable) {
            httpSecurity.requiresChannel(channelRequestMatcherRegistry -> channelRequestMatcherRegistry.anyRequest().requiresSecure());
        }

       
        httpSecurity.addFilterBefore(jwtRequestFilter, UserPasswordAuthenticationFilter.class);
        return httpSecurity.build();
    }

如您所见,/authenticate 和 /get-is-account-locked api 我们在未经授权的情况下绕过了它,这意味着当这些 api 被命中时,不需要授权。

我想在未经授权的情况下再允许一个 api,但问题是它只有在具有具有特定值的查询参数时才应该跳过授权。

例 http://localhost:8080/legendary?type=system&name=sigma

所以我想我可以将其包含为,但这不起作用,它仍然需要对此 URL 进行授权。.requestMatchers("legendary?type=system&name=sigma").permitAll()

我认为由于查询参数而无法正常工作。当 url 与这些查询参数值完全相同时,有人可以帮忙跳过授权/未经授权允许此 api 吗?

java url spring-security

评论

1赞 Toerktumlare 11/18/2023
你是做什么的,因为您确实知道不建议将 JWT 分发给浏览器,也不建议使用 JWT 作为会话,这就是为什么在 Spring Security 中没有实现它的原因。那么,为什么要构建一些不推荐且未在 spring 安全文档中定义的东西。你为什么要阅读文档?jwtRequestFilter

答:

0赞 Mojtaba 11/18/2023 #1

为此,可以改用路径变量。 您的控制器将如下所示:

@GetMapping("/legendary/{type}/{name}")
public String api(@PathVariable String type, @PathVariable String name) {
    return "OK!!!";
}

您的安全配置将如下所示:

.requestMatchers("/legendary/system/sigma").permitAll()

但是如果你想通过查询参数来做到这一点,我认为你应该在控制器中处理它并检查参数值,如果它们不匹配,你应该抛出异常。