8

我有一个工作(基于 web.xml)的容器身份验证和授权。由于<url-pattern>我需要switch to javax.annotation.security注释的限制。我发现我需要在 web.xml 中进行额外配置才能打开基于角色的安全注释。在 RESTEasy 用户指南中描述

但这对我不起作用:我收到错误 404(找不到相关资源:/services/customers/1),具体取决于是否

<servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

在 web.xml 中定义或不在<context-param>and之后<listener>

这是我的旧(现有)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_2_5.xsd"
    version="2.5">

    <display-name>Store_Service</display-name>

    <session-config>
        <session-timeout>10</session-timeout>
    </session-config>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>SSL Secured WebService</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
        </user-data-constraint>
    </security-constraint>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Authenticated customers only</web-resource-name>
            <url-pattern>/services/customers/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>CUST</role-name>
        </auth-constraint>
        <user-data-constraint>
            <transport-guarantee>CONFIDENTIAL</transport-guarantee> 
        </user-data-constraint>
    </security-constraint>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>Authentication-REALM</realm-name>
    </login-config>

    <security-role>
        <role-name>CUST</role-name>
    </security-role>

    <security-role>
        <role-name>ADMIN</role-name>
    </security-role>

    <welcome-file-list>
        <welcome-file>/index.xhtml</welcome-file>
    </welcome-file-list>

    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

</web-app>

哪些配置项仍然需要,哪些需要添加才能@RolesAllowed("CUST")正常工作。

4

2 回答 2

2

我相信您的 web.xml 需要以下内容:

<context-param>
   <param-name>resteasy.role.based.security</param-name>
   <param-value>true</param-value>
</context-param>

如果一切都失败了,您可以尝试使用标准方式,使用 SecurityContext:

@Context SecurityContext myContext;

@GET
@javax.ws.rs.Produces("text/html")
public Response doGet() {
  if(myContext.isUserInRole("CUST") == false) {
    return Response.status(Response.Status.UNAUTHORIZED).build();
  }
}
于 2013-07-05T19:17:27.580 回答
1

我不确定 RESTEasy 但如果你想在 RESTful 中做同样的事情,你有下面的链接可能会给你答案。您需要使用资源配置注册安全过滤器,然后将此资源配置添加到 web.xml 文件中。

http://howtodoinjava.com/jersey/jersey-rest-security/

于 2016-06-14T10:00:58.953 回答