绕过带查询参数的url授权

问题描述 投票:0回答:1

我的项目中有 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

所以我想我可以将其包含为

.requestMatchers("legendary?type=system&name=sigma").permitAll()
但这不起作用,它仍然需要对该 URL 进行授权。

我认为由于查询参数的原因它不起作用。当 url 最终与上面的查询参数值完全相同时,有人可以帮助如何跳过授权/未经授权允许此 api 吗?

java spring spring-boot url spring-security
1个回答
0
投票

您可以使用路径变量来完成此操作。 你的控制器将是这样的:

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

你的安全配置将是这样的:

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

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

© www.soinside.com 2019 - 2024. All rights reserved.