如何通过JAX-RS应用程序中的注释从基于web.xml的授权切换到授权

时间:2013-05-23 23:09:19

标签: java jboss authorization security resteasy

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

但这对我不起作用:我收到错误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><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")正常工作。

2 个答案:

答案 0 :(得分:1)

我相信您的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();
  }
}

答案 1 :(得分:0)

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

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