使用通用URL模式保护REST资源

时间:2016-01-14 07:34:19

标签: java rest java-ee web.xml jaas

我正在使用REST WS开发JEE应用程序,我想为web.xml中的特定角色保护一些特定的REST资源

例如: 我有四个角色:“Role1”,“Role2”,“Role3”和“RoleEdit”

我希望只有角色“RoleEdit”才能访问这些特定资源:

SQLQUERY='select `'"${columnname}"'` from table'
SQLQUERY="select \`${columnname}\` from table"

rest/SomePATH/0/Edit
rest/SomePATH/1/Edit
rest/SomePATH/2/Edit
...
rest/SomePATH/10/Edit

其他角色可以访问:

rest/SomeOtherPATH/0/Edit
rest/SomeOtherPATH/1/Edit
rest/SomeOtherPATH/2/Edit
...
rest/SomeOtherPATH/10/Edit

我为RoleEdit的web.xml添加了以下URL模式:

rest/SomePATH/0/query
...
rest/SomeOtherPATH/0/getInfo
...
rest/SomeOtherPATH/0/query
...
rest/SomeOtherPATH/0/getInfo
...

似乎安全容器无法识别<security-constraint> <display-name>EditRessources</display-name> <web-resource-collection> <web-resource-name>Edit</web-resource-name> <description/> <url-pattern>/rest/*/*/Edit</url-pattern> </web-resource-collection> <auth-constraint> <description/> <role-name>RoleEdit</role-name> </auth-constraint> </security-constraint> ,所以其他所有 角色最后可以访问。

有没有办法阻止在web.xml中编写所有的资源(只是使用通用模式)。

提前致谢

1 个答案:

答案 0 :(得分:1)

我解决了问题,thnx到@SteveC

第一个url-pattern规范是:

  
      
  1. 以“/”字符开头并以“/ *”后缀结尾的字符串用于路径映射。
  2.   
  3. 以“*”开头的字符串。 prefix用作扩展映射。
  4.   
  5. 仅包含'/'字符的字符串表示应用程序的“默认”servlet。在这种情况下,servlet路径是请求URI减去上下文路径,路径信息为空。
  6.   
  7. 所有其他字符串仅用于完全匹配。
  8.   

source =&gt; Url pattern spec,因此无法识别rest/*/*/path

我建议那些使用jersy RESTFUL API的人:

1-在jersey servlet容器内的web.xml中添加以下内容:

    <init-param>
        <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name> 
        <param-value>
            com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory
        </param-value>
    </init-param>

2-在那里使用javax.annotation.security.RolesAllowed WS(安全方法或服务): 例如:

@Path("SomePATH")
public class SampleWS{
   ...

   @POST
   @Path("{layer}/Edit")
   @Produces("application/json")
   @RolesAllowed({"RoleEdit"})
   public String edit(@PathParam("layer")String layer){
       //some code
   }
}

就是这样。