Who*_*AmI 0 java rest web-services jax-rs jersey
我正在使用JAX-RS使用平针织实现.我正在尝试使用Tomcat 6使用BASIC身份验证来验证我的服务.
这是代码:
@Path("/authenticate")
@RolesAllowed({"Admin","Guest"})
public class BasicAuthenticationSecurity {
@GET
@Path("/wbiPing")
@Produces(MediaType.TEXT_PLAIN)
@RolesAllowed("Admin")
public Response wbiPing(){
System.out.println("Pinged!!!");
return Response.ok("Pinged!!!").build();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用注释我的方法时@RolesAllows,我收到编译错误:
@RolesAllows cannot be resolved to a type
请让我知道如何解决这个问题?这需要任何特定的罐子/ API?
编辑:
web.xml中
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>
com.security;
com.exception
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
<param-value>com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>BasicDemo</web-resource-name>
<url-pattern>/*</url-pattern>
<http-method>GET</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>Admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<!-- The realm name is typically displayed by the browser in the login dialog box. -->
<realm-name>Login</realm-name>
</login-config>
Run Code Online (Sandbox Code Playgroud)
请让我知道这个问题.
Abh*_*kar 11
几个小时之前,我在类似问题上苦苦挣扎,然而这篇 IBM文章中的一行开启了我的视线.令人惊讶的是,没有一本书或用户指南提到这个关键事实,没有它,认证就无法成功.
使用基于注释的安全性时,web.xml不是可选的 ; 恰恰相反,<security-constraint>元素必须存在; Web容器在JAX-RS执行之前检查安全性,如果没有<security-constraint>,则不设置正确的安全上下文.因此,当JAX-RS调用时isUserInRole(role),它总是返回false.
此外,必须存在<security-role>web.xml或@DeclareRoles注释中的任何元素.
最后,如果使用Jersey,则RolesAllowedDynamicFeature需要在Application类中注册以启用基于注释的安全性.
HTH其他人在那里挣扎于可怜的文件或缺乏文件.
你的代码中有导入吗?
import javax.annotation.security.RolesAllowed;
Run Code Online (Sandbox Code Playgroud)
还要确保annotations-api.jar在您的类路径中。该 jar 可以在 Tomcat 安装 lib 文件夹中找到。
| 归档时间: |
|
| 查看次数: |
8065 次 |
| 最近记录: |