【发布时间】:2017-09-09 12:07:45
【问题描述】:
我有一个包含三个资源的 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";
}
}
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>
【问题讨论】:
标签: java rest security jersey jax-rs