Skip to content

Commit 038b3a8

Browse files
committed
Merge pull request javaee-samples#290 from arjantijms/master
Added test to see if a logout from the web propagates to EJB
2 parents 9692eb7 + 4cc2ccc commit 038b3a8

File tree

3 files changed

+119
-2
lines changed

3 files changed

+119
-2
lines changed

jaspic/ejb-propagation/pom.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
<version>1.0-SNAPSHOT</version>
99
<relativePath>../pom.xml</relativePath>
1010
</parent>
11-
<groupId>org.javaee7</groupId>
11+
1212
<artifactId>jaspic-ejb-propagation</artifactId>
13-
<version>1.0-SNAPSHOT</version>
1413
<packaging>war</packaging>
1514
<name>Java EE 7 Sample: jaspic - ejb-propagation</name>
1615

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.javaee7.jaspic.ejbpropagation.servlet;
2+
3+
import java.io.IOException;
4+
5+
import javax.ejb.EJB;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.annotation.WebServlet;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
import javax.servlet.http.HttpSession;
12+
13+
import org.javaee7.jaspic.ejbpropagation.ejb.PublicEJB;
14+
15+
/**
16+
*
17+
* @author Arjan Tijms
18+
*
19+
*/
20+
@WebServlet(urlPatterns = "/public/servlet-public-ejb-logout")
21+
public class PublicServletPublicEJBLogout extends HttpServlet {
22+
23+
private static final long serialVersionUID = 1L;
24+
25+
@EJB
26+
private PublicEJB publicEJB;
27+
28+
@Override
29+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
30+
31+
String webName = null;
32+
if (request.getUserPrincipal() != null) {
33+
webName = request.getUserPrincipal().getName();
34+
}
35+
36+
String ejbName = publicEJB.getUserName();
37+
38+
request.logout();
39+
HttpSession session = request.getSession(false);
40+
if (session != null) {
41+
session.invalidate();
42+
}
43+
44+
String webNameAfterLogout = null;
45+
if (request.getUserPrincipal() != null) {
46+
webNameAfterLogout = request.getUserPrincipal().getName();
47+
}
48+
49+
String ejbNameAfterLogout = publicEJB.getUserName();
50+
51+
response.getWriter().write("web username: " + webName + "\n" + "EJB username: " + ejbName + "\n");
52+
response.getWriter().write("web username after logout: " + webNameAfterLogout + "\n" + "EJB username after logout: " + ejbNameAfterLogout + "\n");
53+
54+
}
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.javaee7.jaspic.ejbpropagation;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.assertFalse;
5+
6+
import java.io.IOException;
7+
8+
import org.javaee7.jaspic.common.ArquillianBase;
9+
import org.jboss.arquillian.container.test.api.Deployment;
10+
import org.jboss.arquillian.junit.Arquillian;
11+
import org.jboss.shrinkwrap.api.spec.WebArchive;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
import org.xml.sax.SAXException;
15+
16+
/**
17+
* This tests that the established authenticated identity propagates correctly
18+
* from the web layer to a "public" EJB (an EJB without declarative role
19+
* checking) and that after logging out but still within the same request this
20+
* identity is cleared.
21+
*
22+
* @author Arjan Tijms
23+
*
24+
*/
25+
@RunWith(Arquillian.class)
26+
public class PublicEJBPropagationLogoutTest extends ArquillianBase {
27+
28+
@Deployment(testable = false)
29+
public static WebArchive createDeployment() {
30+
return defaultArchive();
31+
}
32+
33+
@Test
34+
public void testProtectedServletWithLoginCallingEJB() throws IOException, SAXException {
35+
36+
String response = getFromServerPath("public/servlet-public-ejb-logout?doLogin");
37+
38+
System.out.println(response);
39+
40+
// Both the web (HttpServletRequest) and EJB (EJBContext) should see the
41+
// same
42+
// user name.
43+
44+
assertTrue(response.contains("web username: test"));
45+
assertTrue("Web has user principal set, but EJB not.", response.contains("EJB username: test"));
46+
47+
48+
// After logging out, both the web and EJB should no longer see the user
49+
// name
50+
51+
assertFalse(
52+
"Web module did not clear authenticated identity after logout",
53+
response.contains("web username after logout: test")
54+
);
55+
assertFalse(
56+
"EJB did not clear authenticated identity after logout",
57+
response.contains("EJB username after logout: test")
58+
);
59+
60+
}
61+
62+
}

0 commit comments

Comments
 (0)