use of javax.ejb.EJBAccessException in project tomee by apache.
the class MovieTest method testAsEmployee.
@Test
public void testAsEmployee() throws Exception {
final Context context = getContext("eddie", "jump");
try {
movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
List<Movie> list = movies.getMovies();
Assert.assertEquals("List.size()", 3, list.size());
for (Movie movie : list) {
try {
movies.deleteMovie(movie);
Assert.fail("Employees should not be allowed to delete");
} catch (EJBAccessException e) {
// Good, Employees cannot delete things
}
}
// The list should still be three movies long
Assert.assertEquals("Movies.getMovies()", 3, movies.getMovies().size());
} finally {
context.close();
}
}
use of javax.ejb.EJBAccessException in project tomee by apache.
the class ContractTest method missingCredentials.
@Test
public void missingCredentials() throws NamingException {
try {
hi(new Properties() {
{
setProperty(Context.INITIAL_CONTEXT_FACTORY, RemoteInitialContextFactory.class.getName());
setProperty(Context.PROVIDER_URL, String.format("http://localhost:%s/tomee/ejb", base.getPort()));
}
});
fail();
} catch (final EJBAccessException eae) {
// no-op
}
}
use of javax.ejb.EJBAccessException in project wildfly by wildfly.
the class ServerSecurityInterceptor method aroundInvoke.
@AroundInvoke
public Object aroundInvoke(final InvocationContext invocationContext) throws Exception {
Principal desiredUser = null;
RealmUser connectionUser = null;
Map<String, Object> contextData = invocationContext.getContextData();
if (contextData.containsKey(DELEGATED_USER_KEY)) {
desiredUser = new SimplePrincipal((String) contextData.get(DELEGATED_USER_KEY));
Connection con = RemotingContext.getConnection();
if (con != null) {
SecurityIdentity localIdentity = con.getLocalIdentity();
if (localIdentity != null) {
connectionUser = new RealmUser(localIdentity.getPrincipal().getName());
}
} else {
throw new IllegalStateException("Delegation user requested but no user on connection found.");
}
}
SecurityContext cachedSecurityContext = null;
boolean contextSet = false;
try {
if (desiredUser != null && connectionUser != null && (desiredUser.getName().equals(connectionUser.getName()) == false)) {
try {
// The final part of this check is to verify that the change does actually indicate a change in user.
// We have been requested to switch user and have successfully identified the user from the connection
// so now we attempt the switch.
cachedSecurityContext = SecurityContextAssociation.getSecurityContext();
final SecurityContext nextContext = SecurityContextFactory.createSecurityContext(desiredUser, new CurrentUserCredential(connectionUser.getName()), new Subject(), "fooSecurityDomain");
SecurityContextAssociation.setSecurityContext(nextContext);
// keep track that we switched the security context
contextSet = true;
RemotingContext.clear();
} catch (Exception e) {
LOGGER.error("Failed to switch security context for user", e);
// Don't propagate the exception stacktrace back to the client for security reasons
throw new EJBAccessException("Unable to attempt switching of user.");
}
}
return invocationContext.proceed();
} finally {
// switch back to original security context
if (contextSet) {
SecurityContextAssociation.setSecurityContext(cachedSecurityContext);
}
}
}
use of javax.ejb.EJBAccessException in project wildfly by wildfly.
the class WhoAmIServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Writer writer = resp.getWriter();
String method = req.getParameter("method");
String username = req.getParameter("username");
String password = req.getParameter("password");
String role = req.getParameter("role");
if ("whoAmI".equals(method)) {
LoginContext lc = null;
try {
if (username != null && password != null) {
lc = getCLMLoginContext(username, password);
lc.login();
}
try {
writer.write(bean.whoAmI());
} finally {
if (lc != null) {
lc.logout();
}
}
} catch (LoginException le) {
throw new IOException("Unexpected failure", le);
}
} else if ("doubleWhoAmI".equals(method)) {
String[] response;
try {
if (username != null && password != null) {
response = bean.doubleWhoAmI(username, password);
} else {
response = bean.doubleWhoAmI();
}
} catch (EJBAccessException e) {
resp.sendError(HttpServletResponse.SC_FORBIDDEN, e.toString());
return;
} catch (LoginException e) {
throw new ServletException("Unexpected failure", e);
}
writer.write(response[0] + "," + response[1]);
} else if ("doIHaveRole".equals(method)) {
LoginContext lc = null;
try {
if (username != null && password != null) {
lc = getCLMLoginContext(username, password);
lc.login();
}
try {
writer.write(String.valueOf(bean.doIHaveRole(role)));
} finally {
if (lc != null) {
lc.logout();
}
}
} catch (LoginException le) {
throw new IOException("Unexpected failure", le);
}
} else if ("doubleDoIHaveRole".equals(method)) {
try {
boolean[] response = null;
if (username != null && password != null) {
response = bean.doubleDoIHaveRole(role, username, password);
} else {
response = bean.doubleDoIHaveRole(role);
}
writer.write(String.valueOf(response[0]) + "," + String.valueOf(response[1]));
} catch (Exception e) {
throw new ServletException("Unexpected Failure", e);
}
} else {
throw new IllegalArgumentException("Parameter 'method' either missing or invalid method='" + method + "'");
}
}
use of javax.ejb.EJBAccessException in project wildfly by wildfly.
the class EJBSecurityTestCase method testSecurityOnBeanInAbsenceOfExplicitSecurityDomain.
/**
* Tests that a bean which doesn't explicitly have a security domain configured, but still has EJB security related
* annotations on it, is still considered secured and the security annotations are honoured
*
* @throws Exception
*/
@Test
public void testSecurityOnBeanInAbsenceOfExplicitSecurityDomain() throws Exception {
final Context ctx = new InitialContext();
// lookup the bean which doesn't explicitly have any security domain configured
final Restriction restrictedBean = (Restriction) ctx.lookup("java:module/" + BeanWithoutExplicitSecurityDomain.class.getSimpleName() + "!" + Restriction.class.getName());
try {
// try invoking a method annotated @DenyAll (expected to fail)
restrictedBean.restrictedMethod();
Assert.fail("Call to restrictedMethod() method was expected to fail");
} catch (EJBAccessException ejbae) {
// expected
}
// lookup the bean which doesn't explicitly have any security domain configured
final FullAccess fullAccessBean = (FullAccess) ctx.lookup("java:module/" + BeanWithoutExplicitSecurityDomain.class.getSimpleName() + "!" + FullAccess.class.getName());
// invoke a @PermitAll method
fullAccessBean.doAnything();
// lookup the bean which doesn't explicitly have any security domain configured
final BeanWithoutExplicitSecurityDomain specificRoleAccessBean = (BeanWithoutExplicitSecurityDomain) ctx.lookup("java:module/" + BeanWithoutExplicitSecurityDomain.class.getSimpleName() + "!" + BeanWithoutExplicitSecurityDomain.class.getName());
try {
// invoke a method which only a specific role can access.
// this is expected to fail since we haven't logged in as any user
specificRoleAccessBean.allowOnlyRoleTwoToAccess();
Assert.fail("Invocation was expected to fail since only a specific role was expected to be allowed to access the bean method");
} catch (EJBAccessException ejbae) {
// expected
}
}
Aggregations