我正在使用Jersey / Tomcat开发RESTful服务。
我需要支持两组公共和私有API。公共API对所有人开放,并使用API密钥进行身份验证。私有API只能从我自己的WebServer层调用,以执行某些内部操作。
我更喜欢在同一台服务器上托管两组API,以便我可以有效地管理/扩展它们。
The URIs for the APIs will look some thing like this:
https://myapis/v1.0/* --> for public APIs
and
https://myapis/v1.0/secure/* --> for public APIs
出于安全原因,我想使用客户端证书验证私有API 。即,Web服务器必须使用安装在Tomcat的trustStore上的自签名证书调用secure / * API调用。
根据我的研究,我发现可以通过using web.xml或JAVA security Annotations来实现
但就我而言,两者都不起作用。
如果我采用web.xml路由(我根据证书属性在tomcat-user.xml中添加了一个tomcat用户)两个API都不可访问(错误403)
如果我采用注释路由,则无需身份验证即可访问这两个API。
我发现了几篇关于这个主题的文章,我做了几次试错。但似乎都没有效果。
这样做的正确方法是什么?
这是我的web.xml
新泽西州的serlvet com.sun.jersey.spi.container.servlet.ServletContainer com.sun.jersey.config.property.packages com.mycompany.reststuff 1
<servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/v1.0/*</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>PuplicAPIs</web-resource-name> <url-pattern>/v1.0/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <!-- no auth-constraint --> </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>PrivateAPIs</web-resource-name> <url-pattern>/v1.0/secure/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>WebCaller</role-name> </auth-constraint> </security-constraint> <security-role> <role-name>WebCaller</role-name> </security-role> <login-config> <auth-method>CLIENT-AUTH</auth-method> </login-config> ...