【问题标题】:jax-rs service RolesAllowed Annotation throwing exceptionjax-rs 服务 RolesAllowed 注解抛出异常
【发布时间】:2013-11-20 17:08:14
【问题描述】:

如果我不添加以下注释行,我有以下 jax-rs Restful API 可以正常工作

@RolesAllowed("AdminRole")

以上 GET 注释

package service;

import entities.Booking;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.annotation.security.DeclareRoles;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;


@PermitAll
@Stateless
@Path("entities.booking")
public class BookingFacadeREST extends AbstractFacade<Booking> {
    @PersistenceContext(unitName = "ofserverDBPU")

    private EntityManager em;

    public BookingFacadeREST() {
        super(Booking.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Booking entity) {
        super.create(entity);
    }

    @PUT
    @Override
    @Consumes({"application/xml", "application/json"})
    public void edit(Booking entity) {
        super.edit(entity);
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Booking find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @RolesAllowed("AdminRole")
    @GET
    @Override
    @Produces({"application/xml", "application/json"})        
    public List<Booking> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Booking> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

}

通过放置上面的注释给出以下错误。

HTTP Status 500 - Internal Server Error

type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.ejb.EJBAccessException
root cause

javax.ejb.EJBAccessException
root cause

javax.ejb.AccessLocalException: Client not authorized for this invocation
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.0 logs.

GlassFish Server Open Source Edition 4.0

我哪里做错了?

【问题讨论】:

  • 我使用 localhost localhost:8080/ofserverDB/webresources/entities.booking 没有 RoleAllow Annotation 它从 db 返回 xml 文件。哪个是对的。但是带有注释,它假设要求基本身份验证来验证用户名和密码,而不是返回异常。
  • 我已经在 web.xml 和 glassfish-web.xml 中完成了配置,并在基本 Web 应用程序上对其进行了测试,这些对 http 基本身份验证工作正常。所以我认为这些文件没有问题。我必须在 java 服务文件中遗漏一些东西(即注释)
  • 你好@Developer,我知道你的问题已经有一年了,但我有一个类似的问题,似乎无法让 RolesAllowed 注释工作。你能告诉我你是否找到解决您的问题?

标签: jakarta-ee jax-rs java-ee-7


【解决方案1】:

有点老了,但我还是有同样的问题(注意我也有一个自定义的SecurityContext。徘徊了一段时间后,我终于通过编写自己的“@RolesAllowed注解”使其工作。

我是这样处理的:

首先,新建一个注解:

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Secured {
    String role() default "all";
}

然后,使用在 身份验证 阶段运行的ContainerRequestFilter 并检查权限:

@Provider
@Priority(Priorities.AUTHENTICATION)
public class SecurityFilter implements ContainerRequestFilter {

    @Context
    private ResourceInfo resourceInfo;

     @Override
     public void filter(ContainerRequestContext crc) throws IOException {
         // get annotation (only for secured method)
        Secured security = resourceInfo.getResourceMethod().getAnnotation(Secured.class);
        if(security == null){
            // no security on method: check the class 
            security = resourceInfo.getResourceClass().getAnnotation(Secured.class);
            if(security == null) return;
        }

        // check the security, for example through the header:
        //   crc.getHeaderString("token") 
        // You can also inject a PersistenceContext and query your db

        // if the security check fails, use crc.abort() method
        // else, set the security context
        crc.setSecurityContext(new AppSecurityContext(userId, security.role()));
     }
 }

此过滤器必须通过web.xml 或在Application 构造函数中使用register 方法注册。

对于AppSecurityContext,请查看this tutorial 。稍后可以通过以下方式在您的服务中访问它:

@Context
SecurityContext sctx;

最后,使用 @Secure(role = "somerole") 注释您的类或方法(或两者)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多