Java JAX-RS(泽西岛2)安全,@PermitAll和@RolesAllowed未按预期工作

Java JAX-RS(泽西岛2)安全,@PermitAll和@RolesAllowed未按预期工作,java,rest,security,jersey,jax-rs,Java,Rest,Security,Jersey,Jax Rs,我有一个包含三个资源的REST-API。第一种方法称为PublicResource,任何人都可以访问(即匿名访问)。第二个方法称为SecretResource,应该只对特定的用户组可访问。最后,第三个资源称为MixedResource,它有一个混合的设置,其中一些方法受到保护,另一些方法开放供公众访问 尽管@PermitAll和@RolesAllowed的注释不能像我期望的那样工作。尽管PublicResource用@PermitAll注释,但在尝试访问它时,仍然会要求授权。MixedResou

我有一个包含三个资源的REST-API。第一种方法称为PublicResource,任何人都可以访问(即匿名访问)。第二个方法称为SecretResource,应该只对特定的用户组可访问。最后,第三个资源称为MixedResource,它有一个混合的设置,其中一些方法受到保护,另一些方法开放供公众访问

尽管@PermitAll和@RolesAllowed的注释不能像我期望的那样工作。尽管PublicResource用@PermitAll注释,但在尝试访问它时,仍然会要求授权。MixedResource中使用@PermitAll注释的方法也是如此。所以基本上,在我所有的资源中,到处都有人要求我授权,即使我应该匿名访问

我在Payara 4.1上运行,我非常困惑,因为我在WebLogic 12.1.3上运行的另一个应用程序中有一个非常类似的设置,并且在那里注释按预期工作。我错过了什么或错了什么?请参阅下面的完整代码

PublicResource.java:

    import javax.annotation.security.PermitAll;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("public")
    @PermitAll
    public class PublicResource {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String itsPublic() {
            return "public";
        }

    }
    import javax.annotation.security.RolesAllowed;
    import javax.ws.rs.GET;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;

    @Path("secret")
    @RolesAllowed({ "SECRET" })
    public class SecretResource {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String itsSecret() {
            return "secret";
        }

    }
    import javax.annotation.security.PermitAll;
    import javax.annotation.security.RolesAllowed;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("mixed")
    @PermitAll
    public class MixedResource {

        @GET
        @Path("public")
        @Produces(MediaType.TEXT_PLAIN)
        public String itsPublic() {
            return "public";
        }

        @GET
        @Path("secret")
        @RolesAllowed({ "SECRET" })
        @Produces(MediaType.TEXT_PLAIN)
        public String itsSecret() {
            return "secret";
        }

    }
SecretResource.java:

    import javax.annotation.security.PermitAll;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("public")
    @PermitAll
    public class PublicResource {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String itsPublic() {
            return "public";
        }

    }
    import javax.annotation.security.RolesAllowed;
    import javax.ws.rs.GET;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;

    @Path("secret")
    @RolesAllowed({ "SECRET" })
    public class SecretResource {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String itsSecret() {
            return "secret";
        }

    }
    import javax.annotation.security.PermitAll;
    import javax.annotation.security.RolesAllowed;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("mixed")
    @PermitAll
    public class MixedResource {

        @GET
        @Path("public")
        @Produces(MediaType.TEXT_PLAIN)
        public String itsPublic() {
            return "public";
        }

        @GET
        @Path("secret")
        @RolesAllowed({ "SECRET" })
        @Produces(MediaType.TEXT_PLAIN)
        public String itsSecret() {
            return "secret";
        }

    }
MixedResource.java:

    import javax.annotation.security.PermitAll;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("public")
    @PermitAll
    public class PublicResource {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String itsPublic() {
            return "public";
        }

    }
    import javax.annotation.security.RolesAllowed;
    import javax.ws.rs.GET;
    import javax.ws.rs.core.MediaType;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;

    @Path("secret")
    @RolesAllowed({ "SECRET" })
    public class SecretResource {

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String itsSecret() {
            return "secret";
        }

    }
    import javax.annotation.security.PermitAll;
    import javax.annotation.security.RolesAllowed;
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;

    @Path("mixed")
    @PermitAll
    public class MixedResource {

        @GET
        @Path("public")
        @Produces(MediaType.TEXT_PLAIN)
        public String itsPublic() {
            return "public";
        }

        @GET
        @Path("secret")
        @RolesAllowed({ "SECRET" })
        @Produces(MediaType.TEXT_PLAIN)
        public String itsSecret() {
            return "secret";
        }

    }
jaxrconfiguration.java:

    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;

    @ApplicationPath("resources")
    public class JAXRSConfiguration extends Application {

    }
web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
        <session-config>
            <session-timeout>30</session-timeout>
        </session-config>
        <security-constraint>
            <web-resource-collection>
                <web-resource-name>Basic Authorization</web-resource-name>
                <description/>
                <url-pattern>/resources/*</url-pattern>
            </web-resource-collection>
            <auth-constraint>
                <role-name>SECRET</role-name>
            </auth-constraint>
        </security-constraint>
        <login-config>
            <auth-method>BASIC</auth-method>
            <realm-name>file</realm-name>
        </login-config>
        <error-page>
            <exception-type>java.lang.Throwable</exception-type>
            <location>/error/internal</location>
        </error-page>
        <security-role>
            <role-name>SECRET</role-name>
        </security-role>
    </web-app>

30
基本审批权限
/资源/*
秘密
基本的
文件
java.lang.Throwable
/错误/内部
秘密
glassfish-web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
    <glassfish-web-app error-url="">
        <class-loader delegate="true"/>
        <security-role-mapping>
            <role-name>SECRET</role-name>
            <group-name>cia</group-name>
        </security-role-mapping>
        <jsp-config>
            <property name="keepgenerated" value="true">
                <description>Keep a copy of the generated servlet class' java code.</description>
            </property>
        </jsp-config>
    </glassfish-web-app>

秘密
中情局
保留生成的servlet类的java代码的副本。
beans.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
   bean-discovery-mode="all">
    </beans>

从表面上看,这不是球衣的问题。您配置的唯一真正的安全性是在servlet容器级别。您的Jersey安全注释都没有任何效果,因为您甚至还没有为其配置Jersey特定的支持

首先看一下web.xml配置

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Basic Authorization</web-resource-name>
        <description/>
        <url-pattern>/resources/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>SECRET</role-name>
    </auth-constraint>
</security-constraint>

基本审批权限
/资源/*
,所有操作都使用Jersey过滤器完成


最后要注意的是,您甚至还没有为Jersey配置授权支持。您仍然需要向应用程序注册
RolesAllowedDynamicFeature
。由于您正在为JAX-RS配置使用类路径扫描(空应用程序类),因此您需要从
功能注册它,如中所述,感谢您提供了非常详尽的答案。非常感谢。是的,将SecretResource和PublicResource放在不同的路径中是有效的。这是我尝试的第一件事,即具有受保护访问的“localhost:8080/resources/secret/*”和具有匿名/公共访问的“localhost:8080/resources/public/*”。是混合资源让我绊倒了,它们都在同一条基本路径上,而且PermitAll在我以前的项目中似乎以不同的方式工作。无论如何,感谢您的指示。
@PermitAll
将允许在web.xml中不限制资源时访问该资源。因此,如果不约束资源,则不需要用户
@PermitAll
将允许运行该资源。因此web.xml中的约束是始终需要指定的角色之一。由于您通常总是需要一个用户来确定角色,因此在这种情况下,您总是有一个用户(但您可以创建一个仅设置组/角色的安全提供程序)。我不知道是否可以将容器配置为需要用户,但不一定需要任何角色。