使用Java Restful Service进行身份验证

时间:2012-05-04 19:24:46

标签: java rest jersey java-ee-6 basic-authentication

我正在尝试使用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>

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

您需要将RolesAllowedResourceFilterFactory添加到您的Web应用程序(请参阅链接的javadoc,了解如何),以便@RolesAllowed注释生效。