我有一个 REST-API,其中包含三个资源。第一个资源名为 PublicResource,该资源中的方法应该对任何人(即匿名访问者)都是可访问的;第二个资源名为 SecretResource,该资源中的方法仅限特定用户组访问;最后一个资源名为 MixedResource,这个资源设置了混合访问权限,其中一些方法需要受保护,一些方法对公众开放。
虽然 PublicResource 使用了 @PermitAll 标注,但是我仍然被要求进行授权才能访问它。同样的情况出现在 MixedResource 中使用 @PermitAll 标注的方法上。实际上,在我的所有资源中,包括应该允许匿名访问的地方,我都被要求授权。
我正在 Payara 4.1 上运行,非常困惑,因为我在另一个运行于 WebLogic 12.1.3 上的应用程序中拥有非常相似的设置,并且那里的注释按照预期工作。我错过了什么或做错了什么?请看下面的完整代码。
PublicResource.java:
虽然 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";
}
}
SecretResource.java:
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";
}
}
MixedResource.java:
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";
}
}
JAXRSConfiguration.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>
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>
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>
@PermitAll
将允许访问资源,当资源在 web.xml 中没有受到限制时。因此,如果您不限制资源,则不需要用户。@PermitAll
然后将允许运行该资源。 - thehpi