授权运动衫使用web.xml

时间:2015-11-24 21:40:59

标签: java jersey authorization jax-rs

我正在尝试通过web.xml要求对我的资源进行授权,但以下内容无效。我做错了什么?

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

<display-name>security-web</display-name>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

<context-param>
    <param-name>resteasy.role.based.security</param-name>
    <param-value>true</param-value>
</context-param>

<security-constraint> 
    <web-resource-collection> 
        <web-resource-name>Docente Service</web-resource-name> 
        <url-pattern>/servicios/docentes</url-pattern> 
        <http-method>GET</http-method> 
    </web-resource-collection> 

    <auth-constraint>
        <description>Admin role</description> 
        <role-name>admin</role-name> 
    </auth-constraint> 
</security-constraint> 

<security-role> 
    <role-name>admin</role-name> 
</security-role>    
</web-app>

2 个答案:

答案 0 :(得分:0)

我为GlassFish 4.1项目工作了这个:

<servlet>
        <servlet-name>Jersey</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.classnames</param-name>
            <param-value>org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature</param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.mypackage</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

答案 1 :(得分:0)

您可以将此作为使用基本授权保护RESTful Web服务的示例以获取更多信息,请转至site

<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>