gpt4 book ai didi

spring - spring 请求映射中的多个正斜杠

转载 作者:行者123 更新时间:2023-12-03 21:19:20 25 4
gpt4 key购买 nike

@RestController
@RequestMapping("/api")
public class AbcController {

@RequestMapping(value = "/abc", method = RequestMethod.GET)
public String abc(){
return "Hello";
}
}

有效网址: http://localhost:8080/api/abc

无效的 URL:
http://localhost:8080////api/abc

http://localhost:8080/////api////abc

http://localhost:8080/////////api/////abc

问题:我的 Controller 接受上述所有网址。我想限制它并只接受有效的 url 并在无效的 url 上抛出错误。

注:我没有使用任何自定义路由。这是默认的 Spring 。

最佳答案

为 spring 安全性添加 maven 依赖项,并使用以下代码允许在不登录的情况下访问所有路径。

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter
{
@Override
public void configure(WebSecurity web) throws Exception
{
web
.ignoring()
.antMatchers("/**");
}
}

关于spring - spring 请求映射中的多个正斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52550909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com