如何使用web.xml等来禁用来自Java应用程序的休息端点(how to disable rest endpoint from java application using web.xml or s

编程入门 行业动态 更新时间:2024-10-27 14:34:14
如何使用web.xml等来禁用来自Java应用程序的休息端点(how to disable rest endpoint from java application using web.xml or so)

我有Java应用程序(myapp),它有三个休息终结点

本地主机:8080 /人 本地主机:8080 /语言 本地主机:8080 /国家

我的要求是我需要部署3个相同的应用程序例如myapp1,myapp2,myapp3。 myapp1应该只允许/个人端点myapp2应该只允许/语言端点myapp3应该只允许/国家端点

为了达到这个目的,我不希望有太多的java代码改变。 有没有可能通过web.xml或springsecurity.xml或类似的东西来实现?

我的Java应用程序使用球衣框架。

以下是web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ResourcesAPI</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-security.xml</param-value> </context-param> <servlet> <servlet-name>PersonDataService</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.local.service.PersonDataApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>PersonDataService</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>request-logging</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

I have java application (myapp) which has say following three rest endpoints

localhost:8080/persons localhost:8080/languages localhost:8080/countries

My requirement is I need to deploy 3 instances of the same application say myapp1, myapp2, myapp3. myapp1 should allow only /persons endpoint myapp2 should allow only /languages endpoint myapp3 should allow only /countries endpoint

To achieve this i prefer not to have much of java code change. Is there any possibility to achieve via web.xml or springsecurity.xml or something of similar?

My java application is using jersey framework.

Following is web.xml

<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ResourcesAPI</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-security.xml</param-value> </context-param> <servlet> <servlet-name>PersonDataService</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.local.service.PersonDataApplication</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>PersonDataService</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <filter> <filter-name>springSecurityFilterChain</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>request-logging</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>

最满意答案

如果我理解正确,您将返回com.local.service.PersonDataApplication中的服务类列表。

您可以将运行tomcat的命令行中的参数作为系统属性,并在com.local.service.PersonDataApplication :: getClasses方法中决定返回哪些类。

假设你为每个端点都有一个服务类,这也是有意义的。

假设你正在用脚本catalina.sh启动tomcat,你可以这样做:

export JAVA_OPTS=-DpersonsEndpoint=true catalina.sh

在你的getClasses方法中:

if(System.getProperty("personsEndpoint") != null && System.getProperty("personsEndpoint").equals("true")) { ... return the person endpoint class }

编辑:

另外一个选择

我不知道为什么你选择了自定义的应用程序类(它可能对你的使用有意义),但是如果你可以放弃它,你可以在你的web.xml中使用jersey.config.server.provider.classnames来定义要扫描哪些类,然后只能在web.xml中进行更改。

在一个配置中:

<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value> com.local.service.PersonService </param-value> </init-param>

在另一个配置中:

<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value> com.local.service.LanguagesService </param-value> </init-param>

这里的假设是这些类是分开的。

com.local.service.PersonService - 所有您的jaxrs注释方法都与/ persons端点有关的类。

If I understand correctly you are returning the list of service classes in com.local.service.PersonDataApplication.

You could take a parameter from the command line running tomcat as a system property, and decide what classes to return in com.local.service.PersonDataApplication::getClasses method.

Assuming you have a service class for each endpoint, which makes sense anyway...

Assuming you are starting tomcat with the script catalina.sh you can do something like:

export JAVA_OPTS=-DpersonsEndpoint=true catalina.sh

And in your getClasses method:

if(System.getProperty("personsEndpoint") != null && System.getProperty("personsEndpoint").equals("true")) { ... return the person endpoint class }

EDIT:

Another option

I do not know why you have chosen to have a custom Application class (It might make sense to your usage), but if you could drop it, you could use jersey.config.server.provider.classnames in your web.xml to define what classes to scan, and then you could do your changes only in the web.xml.

In one config:

<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value> com.local.service.PersonService </param-value> </init-param>

In another config:

<init-param> <param-name>jersey.config.server.provider.classnames</param-name> <param-value> com.local.service.LanguagesService </param-value> </init-param>

Also here the assumption is that the classes are separated.

com.local.service.PersonService - the class where all your jaxrs annotated methods are regarding the /persons endpoint.

更多推荐

本文发布于:2023-07-15 05:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1110696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   等来   应用程序   xml   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!