1

我正在尝试使用 Glassfish v3 上的基本身份验证获得 Java EE jersey restful 服务。

目前,该服务要求对用户进行身份验证,如果提供了正确的凭据,则允许领域内的任何用户(具有任何角色)访问该服务。它应该只允许管理员角色访问。

此外,如果我尝试限制使用 @RolesAllowed 注释,这也会被忽略。

其余代码:

@Path("/file")
public class FileResource {

    @GET
    @Produces("application/json")
    public Response getJson() {
        String result = "{\"data\":\"test\"}";
        return Response.ok(result, MediaType.APPLICATION_JSON).build();
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    @RolesAllowed("admin")
    public Response getOther() {
        String result = "{\"employee\":[{\"email\":\"jdoe@example.com\",\"firstName\":\"John\",\"lastName\":\"Doe\"},{\"email\":\"mmajor@example.com\",\"firstName\":\"Mary\",\"lastName\":\"Major\"}]}";
        return Response.ok(result, MediaType.APPLICATION_JSON).build();
    }
}

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    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>org.examples.secure</display-name>
    <welcome-file-list>
        <welcome-file>index.xhtml</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>JAX-RS Servlet</servlet-name>
        <url-pattern>/rs/*</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>DBRealm</realm-name>
    </login-config>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>users</web-resource-name>
            <url-pattern>/rs/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <description/>
            <role-name>admin</role-name>
        </auth-constraint>
        <!-- <user-data-constraint>
            <description>SSL</description>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
        </user-data-constraint> -->
    </security-constraint>

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

</web-app>

sun-web.xml 代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 Servlet 2.5//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
<sun-web-app error-url="">
  <context-root>/org.examples.secure</context-root>
  <security-role-mapping>
    <role-name>admin</role-name>
    <group-name>admin</group-name>
  </security-role-mapping>
  <class-loader delegate="true"/>
  <jsp-config>
    <property name="keepgenerated" value="true">
      <description>Keep a copy of the generated servlet class java code.</description>
    </property>
  </jsp-config>
</sun-web-app>

任何帮助,将不胜感激

4

1 回答 1

0

You need to add RolesAllowedResourceFilterFactory to your web application (see the linked javadoc on how) for the @RolesAllowed annotations to work.

于 2012-05-04T22:53:21.130 回答