tomcat webapp中基于注释的安全性

时间:2013-04-02 14:07:35

标签: web-services jersey jaas

我希望实现像这样的webservice安全性 (http://docs.oracle.com/cd/E24329_01/web.1211/e24983/secure.htm - 示例适用于javaee)

但是在tomcat + jersey里面应用。是否可以这样做?

据我所知,tomcat确实有一个注释-api,在这种情况下可以工作。无法弄清楚如何。

package samples.helloworld;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.annotation.Security.RolesAllowed;


@Path("/helloworld")
@RolesAllowed({"ADMIN", "ORG1"})
public class helloWorld {

   @GET
   @Path("sayHello")  
   @Produces("text/plain")
   @RolesAllows("ADMIN")
   public String sayHello() {
      return "Hello World!";
   }
}

1 个答案:

答案 0 :(得分:0)

如上面的链接所述,您应该保护您的Rest Web服务。 Web.xml示例

<web-app>
    <servlet>
        <servlet-name>RestServlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RestServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <security-constraint>
         <web-resource-collection>
             <web-resource-name>Orders</web-resource-name>
             <url-pattern>/orders</url-pattern>
             <http-method>GET</http-method>
             <http-method>POST</http-method>
         </web-resource-collection>
         <auth-constraint>
             <role-name>admin</role-name> 
         </auth-constraint>
    </security-constraint>
        <login-config>
            <auth-method>BASIC</auth-method>
            <realm-name>default</realm-name>
        </login-config>
    <security-role>
        <role-name>admin</role-name>
    </security-role>
</web-app>