可以在tomcat 8中使用RolesAllowed注释吗?

时间:2018-12-04 18:54:11

标签: java security tomcat jersey jax-rs

我有一个像这样的球衣使用的小型休息API

@Path("/noticias")
@DeclareRoles({"user"})
public class ServicioNoticias {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @RolesAllowed({ "user" })
    public List<Noticia> getNoticias() {

        List<Noticia> noticias = new ArrayList<Noticia>();
        noticias.add(new Noticia("1", "noticia1"));
        noticias.add(new Noticia("2", "noticia2"));

        return noticias;

    }


}

我使用基本身份验证进行访问,首先将用户和角色添加到 tomcat-users.xml

<role rolename="administrador" />
<role rolename="user" />
<user username="cecilio" password="cecilio" roles="administrador" />
<user username="yhorell" password="hello" roles="user" />

,在web.xml中,我声明了

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>JaxRxJackson</display-name>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>recurso noticias</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>GET</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>administrador</role-name>
            <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>

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

    <security-role>
        <role-name>administrador</role-name>
    </security-role>

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

我的想法是拒绝getNoticias方法仅由用户角色使用,所以这可行吗?

注意:基本身份验证工作正常,但我也想使用@RolesAllowed过滤访问权限。

Tomcat版本:8.0 Jax rs的实现:泽西岛

谢谢...

0 个答案:

没有答案