Glassfish 4 と Jersey を JAX-RS 実装として実行しています。次のように EJB を保護しました。
@Stateless
@DeclareRoles({"Authentication_Reader"})
@RolesAllowed({"Authentication_Reader"})
public class AuthenticationServiceBean {
public void foo() {
...
}
}
私は、glassfish-web.xml に security-role-mapping エントリを作成し、web.xml に security-role 宣言も作成しました。
以下はサーブレットから機能します。
@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 サーバー ログには、基本的に次のように記載されています。
これがRESTリソースではなくサーブレットで機能する理由がわかりません。私には、これはうまくいくはずです。