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

My-*_*-Is 8 java security jboss authorization resteasy

我有一个工作(基于web.xml)容器身份验证和授权.由于<url-pattern>我需要switch to javax.annotation.security注释的限制.我发现我需要在web.xml中进行额外配置才能打开基于角色的安全注释.在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>
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

仍然需要哪些配置项以及需要添加哪些配置项才能使其@RolesAllowed("CUST")正常工作.

Mar*_*ski 2

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

<context-param>
   <param-name>resteasy.role.based.security</param-name>
   <param-value>true</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

如果一切失败,您可以尝试使用标准方法,使用 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();
  }
}
Run Code Online (Sandbox Code Playgroud)