春季靴 - 是否可以禁用终点

发布于 2025-02-01 20:12:37 字数 951 浏览 2 评论 0原文

假设我有一个控制器:

public class MyController {

  public String endpoint1() {...}

  public String endpoint2() {...}
}

我想在春季出于任何原因禁用endpoint1。简而言之,只是禁用它,以免它访问。因此,我不是在寻找在这种情况下返回的响应或如何确保该端点的响应。只是想简单地禁用端点,例如@disabled注释左右。

解决方案更新:

感谢所有贡献的人。我决定提出@adolink建议。但是,该解决方案只会禁用对控制器的访问,从而未找到404。但是,如果您使用OpenAPI,则您的控制器及其所有模型(例如请求/响应主体)仍将在Swagger中显示出来。

因此,除了Adolin的建议外,还向我的控制器添加了@hidden OpenAPI注释,例如:

in application.properties ,设置:

cars.controller.enabled=false

然后在您的控制器中,使用它。要从OpenAPI/Swagger隐藏控制器,您可以使用@hiden tag:

@Hidden 
@ConditionalOnExpression("${cars.controller.enabled}")
@RestController
@RequestMapping("/cars")  
public class Carontroller {
   ...
}

此后,该控制器处理的每个端点都将返回404,并且OpenAPI/Swagger不会显示控制器,也不会显示控制器其任何相关的模式对象,例如carrequestModel,carresponsemodel等。

Assuming I have a controller like:

public class MyController {

  public String endpoint1() {...}

  public String endpoint2() {...}
}

I want to disable endpoint1 for whatever reason in Spring. Simply, just disable it so that it cannot be accessed. So, I am not looking for how and what response to return in that case or how to secure that endpoint. Just looking to simply disable the endpoint, something like @Disabled annotation on it or so.

SOLUTION UPDATE:

Thanks all who contributed. I decided to go with @AdolinK suggestion . However, that solution will only disable access to the controller resulting into 404 Not Found. However, if you use OpenApi, your controller and all of its models such as request/response body will still show in swagger.

So, in addition to Adolin's suggestion and also added @Hidden OpenApi annotation to my controllers like:

In application.properties, set:

cars.controller.enabled=false

Then in your controller, use it. To hide controller from the OpenApi/Swagger as well, you can use @Hiden tag:

@Hidden 
@ConditionalOnExpression("${cars.controller.enabled}")
@RestController
@RequestMapping("/cars")  
public class Carontroller {
   ...
}

After this, every end point handled by this controller will return 404 Not Found and OpenApi/Swagger will not show the controllers nor any of its related schema objects such as CarRequestModel, CarResponseModel etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

掀纱窥君容 2025-02-08 20:12:37

您可以使用@conditionalonexpression注释。

public class MyController {

  @ConditionalOnExpression("${my.controller.enabled:false}")
  public String endpoint1() {...}

  public String endpoint2() {...}
}

在application.properties中,您指出默认情况下启用控制器的contrenter

my.controller.enabled=true

artivealOnexpression设置了false属性,并且不允许访问端点

You can use @ConditionalOnExpression annotation.

public class MyController {

  @ConditionalOnExpression("${my.controller.enabled:false}")
  public String endpoint1() {...}

  public String endpoint2() {...}
}

In application.properties, you indicates that controller is enabled by default

my.controller.enabled=true

ConditionalOnExpression sets false your property, and doesn't allow access to end-point

七度光 2025-02-08 20:12:37

为什么不删除该方法上的映射注释?

Why not remove the mapping annotation over that method?

余生一个溪 2025-02-08 20:12:37

尝试这种简单的方法:您可以定义属性is.enable.enpoint1以灵活的方式打开/关闭端点。

如果关闭端点,请返回404错误页面,这取决于您的情况。

@Value("${is.enable.enpoint1}")
private String isEnableEnpoint1;
 
public String endpoint1() {
    if (!"true".equals(isEnableEnpoint1)) {
        return "404";  
    }
    // code
}
 

Try this simple approach: You can define a property is.enable.enpoint1 to turn on/off your endpoint in a flexible way.

If you turn off the endpoint, then return a 404 or error page, which depends on your situation.

@Value("${is.enable.enpoint1}")
private String isEnableEnpoint1;
 
public String endpoint1() {
    if (!"true".equals(isEnableEnpoint1)) {
        return "404";  
    }
    // code
}
 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文