@RolesAllowed 对我的 RESTful 服务没有影响

@RolesAllowed has no effect on my RESTful Service

我在设置安全 REST 服务时遇到了一些问题。 我想创建一个简单的 login/logout 服务并使用它。

我正在学习本教程。我跳过了登录表单部分,并将用户名和密码硬编码到服务中。 (登录()) http://www.blog.btbw.pl/java/wildfly-9-login-form-simple-example/

一切正常,直到我想使用注释@RolesAllowed。 所以我用这个注释创建了一个新方法(adminInfo())。但我得出的结论是注释没有任何区别。我能够成功调用它而无需使用角色 "ADMIN".

登录

也许你们外面的聪明人中有一个知道我做错了什么并且能够帮助我。对不起我的语法不好,我不习惯用英文写这么多。

谢谢。

这些是我的文件:

我正在使用如下所示的简单服务:

@Context
private HttpServletRequest request;

@GET
@Path("/hello")
public Response hello() {
    return Response.ok().entity("Hello, World!").build();
}

@GET
@Path("/logout")
@RolesAllowed("ADMIN")
public Response adminInfo() {
    return Response.ok().entity("hello " + request.getUserPrincipal().getName()).build();
}

@POST
@Path("/login")
public Response login() {
    try {
        request.login("admin", "admin");
        return Response.ok().entity("login successful").build();

    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).entity("login failed").build();
    }
}

@GET
@Path("/logout")
@RolesAllowed("ADMIN")
public Response logout() {
    try {
        request.logout();
        return Response.ok().entity("logout successful").build();

    } catch (Exception e) {
        return Response.status(Status.BAD_REQUEST).entity("logout failed").build();
    }
}

我的 jboss-web.xml 看起来像这样:

<jboss-web>
    <context-root>/</context-root>
    <security-domain>jaas-realm</security-domain>
</jboss-web>

我的 web.xml 如下所示:

<web-app>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Authentication</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>ADMIN</role-name>
        </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>jaas-realm</realm-name>
    </login-config>

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

我的 Wildfly standalone.xml 配置如下:

<security-domain name="jaas-realm" cache-type="default">
    <authentication>
        <login-module name="login-module" code="Database" flag="required">
            <module-option name="dsJndiName" value="java:/datasource"/>
            <module-option name="principalsQuery" value="select password from users where username=?"/>
            <module-option name="rolesQuery" value="select rolename, 'Roles' from roles where username=?"/>
            <module-option name="hashAlgorithm" value="SHA-256"/>
            <module-option name="hashEncoding" value="base64"/>
            <module-option name="unauthenticatedIdentity" value="guest"/>
        </login-module>
    </authentication>
</security-domain>

您定义了两个与 @Path("/logout") 具有相同路径的资源。资源由其 PATH 唯一标识。请有不同的路径并尝试

我终于成功了。我必须将注释 @Stateless 添加到我的服务中。 感谢 5 年前制作的 post: JAX-WS webservice and @rolesAllowed

我无法在 Google 上找到解决方案,但我只需要在 Whosebug 上花 5 分钟:)