【问题标题】:How to switch from web.xml based authorization to authorization via annotations in a JAX-RS application如何通过 JAX-RS 应用程序中的注释从基于 web.xml 的授权切换到授权
【发布时间】:2013-05-19 11:41:48
【问题描述】:

我有一个有效的(基于 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中定义或不在&lt;context-param&gt;&lt;listener&gt;之后

这是我的旧(现有)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")工作,还需要哪些配置项,需要添加哪些配置项。

【问题讨论】:

    标签: java jboss authorization security resteasy


    【解决方案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();
      }
    }
    

    【讨论】:

    • 非常感谢您的回答。您建议的参数名称也对我不起作用,现在我再次重新检查它,不幸的是没有成功。你是对的,使用 SecurityContext 的方法也是可能的。但是,如果我可以使用安全注释来做到这一点,那就太好了。还有其他想法吗?提前致谢。
    • 您可以将其添加到 web.xml:&lt;listener&gt;&lt;listener-class&gt;org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap&lt;/listener-class&gt;&lt;/listener&gt;
    • 仍然没有成功,我尝试结合&lt;servlet-class&gt;org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher&lt;/servlet-class&gt;,然后我得到一个错误404。如果没有HttpServletDispatcher的监听器,我仍然可以在没有任何身份验证的情况下访问资源。 (&lt;context-param&gt; 条目也存在)
    • 好吧,当你得到 401(未经授权)时,你就走在了正确的轨道上。这意味着您未通过身份验证(例如,忘记输入密码),或者您的领域配置已损坏,当然,这不是您的角色。我建议你做一个测试方法,使用SecurityContext.getUserPrincipal()来确认你的领域/角色/用户配置没有被破坏。
    • 现在我尝试更改所有变体的配置,但仍然没有运气。你是对的,我在问题中提到,我收到错误 401,这意味着安全注释按预期工作。不幸的是我无法再次重现这种行为,所以我很确定这是我在写问题时犯的一个错误,因此我相应地更新了它。最后一小时的结果是我可以在没有身份验证的情况下访问受保护的资源,或者我收到错误 404
    【解决方案2】:

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

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

    【讨论】:

    • 这对我有用。顺便说一句,当您想修改注释的默认行为时,这非常有用。
    猜你喜欢
    • 1970-01-01
    • 2018-12-15
    • 2014-08-08
    • 1970-01-01
    • 2011-07-04
    • 2015-01-22
    • 2015-03-02
    • 1970-01-01
    • 2022-12-05
    相关资源
    最近更新 更多