@Context injection in Stateless EJB used by JAX-RS

后端 未结 2 1507
梦谈多话
梦谈多话 2021-01-19 17:57

I have something like this setup below. This is a simplified version but I think it gets the basic idea across. I am using Jersey 2.16, Java 1.8, and Glassfish Open Source 4

2条回答
  •  情话喂你
    2021-01-19 19:01

    The problem is that you are using the SecurityContext in the wrong place. You have to use it inside your REST resource class.

    You can try the following:

    @POST
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    @Produces(MediaType.TEXT_PLAIN)
    @Override
    public String addNewReport(final Report report, @Context SecurityContext sc) {
       report.setUserName(sC.getUserPrincipal().getName());
       return service.addNewReport(report);
    }
    

    For more details have a look at the Jersey Documentation - Chapter 16. Security.

    Inside of EJBs you have to use the EJBContext (or the SessionContext).

提交回复
热议问题