我正在运行 Glassfish 4 和 Jersey 作为 JAX-RS 实现。我已经像这样保护了我的 EJB:
@Stateless
@DeclareRoles({"Authentication_Reader"})
@RolesAllowed({"Authentication_Reader"})
public class AuthenticationServiceBean {
public void foo() {
...
}
}
我在 glassfish-web.xml 中创建了一个安全角色映射条目,并且还在 web.xml 中创建了一个安全角色声明。
以下工作来自 servlet:
@WebServlet(name = "TestServlet", urlPatterns = {"/test.do"})
@RunAs("Authentication_Reader")
public class TestServlet extends HttpServlet {
@Inject
private AuthenticationServiceBean authenticationService;
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
authenticationService.foo();
.. etc ...
}
}
但是,如果我从 JAX-RS 资源中执行此操作,例如这个:
@RequestScoped
@RunAs("Authentication_Reader")
@Path("test")
public class TestResource {
@Inject
private AuthenticationServiceBean authenticationServiceBean;
@GET
public String test() {
int x = 123; // This code executes fine
authenticationServiceBean.foo(); // This gets an AccessLocalException
return "I never returned this";
}
}
Glassfish 服务器日志基本上说: javax.ejb.AccessLocalException: Client not authorized for this invocation
我不明白为什么这适用于 servlet,而不适用于 REST 资源。对我来说,这似乎应该工作得很好。