Skip to content

Commit 8fa3d67

Browse files
committed
Merge pull request javaee-samples#347 from arjantijms/master
Added JASPIC tests for EJB propagation after register session
2 parents 345b8a7 + fd8bb4d commit 8fa3d67

File tree

11 files changed

+575
-11
lines changed

11 files changed

+575
-11
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,10 @@ local.properties
9494

9595
# Testing environment specific
9696
derby.log
97+
98+
99+
######################
100+
# Liberty tools
101+
######################
102+
103+
.factorypath

jaspic/common/pom.xml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
<version>1.0-SNAPSHOT</version>
1313
<relativePath>../pom.xml</relativePath>
1414
</parent>
15-
<groupId>org.javaee7</groupId>
15+
1616
<artifactId>jaspic-common</artifactId>
17-
<version>1.0-SNAPSHOT</version>
17+
1818
<packaging>jar</packaging>
1919
<name>Java EE 7 Sample: jaspic - common</name>
2020

@@ -36,5 +36,10 @@
3636
<version>2.13</version>
3737
<scope>provided</scope>
3838
</dependency>
39+
<dependency>
40+
<groupId>org.jsoup</groupId>
41+
<artifactId>jsoup</artifactId>
42+
<version>1.9.1</version>
43+
</dependency>
3944
</dependencies>
4045
</project>

jaspic/common/src/main/java/org/javaee7/jaspic/common/ArquillianBase.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
package org.javaee7.jaspic.common;
22

33
import static java.lang.Boolean.getBoolean;
4+
import static java.util.logging.Level.SEVERE;
45
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
6+
import static org.jsoup.Jsoup.parse;
7+
import static org.jsoup.parser.Parser.xmlParser;
58

69
import java.io.File;
710
import java.io.IOException;
811
import java.net.URL;
12+
import java.util.logging.Logger;
913

1014
import org.jboss.arquillian.test.api.ArquillianResource;
1115
import org.jboss.shrinkwrap.api.Archive;
1216
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
1317
import org.jboss.shrinkwrap.api.spec.WebArchive;
1418
import org.junit.After;
1519
import org.junit.Before;
20+
import org.junit.Rule;
21+
import org.junit.rules.TestWatcher;
22+
import org.junit.runner.Description;
1623

1724
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
1825
import com.gargoylesoftware.htmlunit.WebClient;
@@ -25,8 +32,38 @@
2532
public class ArquillianBase {
2633

2734
private static final String WEBAPP_SRC = "src/main/webapp";
35+
private static final Logger logger = Logger.getLogger(ArquillianBase.class.getName());
36+
2837
private WebClient webClient;
38+
private String response;
39+
40+
@Rule
41+
public TestWatcher ruleExample = new TestWatcher() {
42+
@Override
43+
protected void failed(Throwable e, Description description) {
44+
super.failed(e, description);
45+
46+
logger.log(SEVERE,
47+
"\n\nTest failed: " +
48+
description.getClassName() + "." + description.getMethodName() +
49+
50+
"\nMessage: " + e.getMessage() +
51+
52+
"\nLast response: " +
53+
54+
"\n\n" + formatHTML(response) + "\n\n");
55+
56+
}
57+
};
2958

59+
public static String formatHTML(String html) {
60+
try {
61+
return parse(html, "", xmlParser()).toString();
62+
} catch (Exception e) {
63+
return html;
64+
}
65+
}
66+
3067
public static Archive<?> defaultArchive() {
3168
return tryWrapEAR(defaultWebArchive());
3269
}
@@ -48,6 +85,8 @@ public static Archive<?> tryWrapEAR(WebArchive webArchive) {
4885
create(EnterpriseArchive.class, "test.ear")
4986

5087
// Liberty needs to have the binding file in an ear.
88+
// TODO: this is no longer the case and this code can be removed (-bnd.xml
89+
// needs to be moved to correct place)
5190
.addAsManifestResource(resource("ibm-application-bnd.xml"))
5291

5392
// Web module
@@ -82,6 +121,8 @@ public void tearDown() {
82121
webClient.getCookieManager().clearCookies();
83122
webClient.closeAllWindows();
84123
}
124+
125+
85126

86127
protected WebClient getWebClient() {
87128
return webClient;
@@ -100,7 +141,9 @@ protected URL getBase() {
100141
*/
101142
protected String getFromServerPath(final String path) {
102143
try {
103-
return webClient.getPage(base + path).getWebResponse().getContentAsString();
144+
response = null;
145+
response = webClient.getPage(base + path).getWebResponse().getContentAsString();
146+
return response;
104147
} catch (FailingHttpStatusCodeException | IOException e) {
105148
throw new IllegalStateException(e);
106149
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.javaee7.jaspic.registersession.ejb;
2+
3+
import javax.annotation.Resource;
4+
import javax.annotation.security.DeclareRoles;
5+
import javax.annotation.security.PermitAll;
6+
import javax.annotation.security.RolesAllowed;
7+
import javax.ejb.EJBContext;
8+
import javax.ejb.Stateless;
9+
10+
/**
11+
* This is a "protected" EJB in the sense that there is role checking done prior to accessing (some) methods.
12+
* <p>
13+
* In JBoss EAP 6.1+ the use of any declarative security annotation switches the bean to a different mode, called "secured" in
14+
* JBoss terms.
15+
* <p>
16+
* GlassFish requires the <code>@DeclareRoles</code> annotation when programmatic role checking is done (making dynamic role
17+
* checking impossible).
18+
*
19+
* @author Arjan Tijms
20+
*/
21+
@Stateless
22+
//Required by GlassFish
23+
@DeclareRoles({ "architect" })
24+
//JBoss EAP 6.1+ defaults unchecked methods to DenyAll
25+
@PermitAll
26+
public class ProtectedEJB {
27+
28+
@Resource
29+
private EJBContext ejbContext;
30+
31+
@RolesAllowed("architect")
32+
public String getUserName() {
33+
try {
34+
return ejbContext.getCallerPrincipal() != null ? ejbContext.getCallerPrincipal().getName() : null;
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
return null;
39+
}
40+
41+
public boolean isUserArchitect() {
42+
try {
43+
return ejbContext.isCallerInRole("architect");
44+
} catch (Exception e) {
45+
e.printStackTrace();
46+
}
47+
return false;
48+
49+
}
50+
51+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.javaee7.jaspic.registersession.ejb;
2+
import javax.annotation.Resource;
3+
import javax.ejb.EJBContext;
4+
import javax.ejb.Stateless;
5+
6+
/**
7+
* This is a "public" EJB in the sense that all its methods should be accessible and there is no declarative role checking prior
8+
* to accessing a method.
9+
*
10+
* @author Arjan Tijms
11+
*
12+
*/
13+
@Stateless
14+
public class PublicEJB {
15+
16+
@Resource
17+
private EJBContext ejbContext;
18+
19+
public String getUserName() {
20+
try {
21+
return ejbContext.getCallerPrincipal() != null ? ejbContext.getCallerPrincipal().getName() : null;
22+
} catch (Exception e) {
23+
e.printStackTrace();
24+
}
25+
return null;
26+
}
27+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package org.javaee7.jaspic.registersession.servlet;
2+
import static java.util.logging.Level.SEVERE;
3+
4+
import java.io.IOException;
5+
import java.util.logging.Logger;
6+
7+
import javax.ejb.EJB;
8+
import javax.servlet.ServletException;
9+
import javax.servlet.annotation.WebServlet;
10+
import javax.servlet.http.HttpServlet;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
import org.javaee7.jaspic.registersession.ejb.ProtectedEJB;
15+
16+
17+
/**
18+
*
19+
* @author Arjan Tijms
20+
*
21+
*/
22+
@WebServlet(urlPatterns = "/public/servlet-protected-ejb")
23+
public class PublicServletProtectedEJB extends HttpServlet {
24+
25+
private static final long serialVersionUID = 1L;
26+
private final static Logger logger = Logger.getLogger(PublicServletProtectedEJB.class.getName());
27+
28+
@EJB
29+
private ProtectedEJB protectedEJB;
30+
31+
@Override
32+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33+
34+
String webName = null;
35+
if (request.getUserPrincipal() != null) {
36+
webName = request.getUserPrincipal().getName();
37+
}
38+
39+
String ejbName = "";
40+
try {
41+
ejbName = protectedEJB.getUserName();
42+
} catch (Exception e) {
43+
logger.log(SEVERE, "", e);
44+
}
45+
46+
response.getWriter().write("web username: " + webName + "\n" + "EJB username: " + ejbName + "\n");
47+
48+
boolean webHasRole = request.isUserInRole("architect");
49+
50+
boolean ejbHasRole = false;
51+
try {
52+
ejbHasRole = protectedEJB.isUserArchitect();
53+
} catch (Exception e) {
54+
logger.log(SEVERE, "", e);
55+
}
56+
57+
response.getWriter().write(
58+
"web user has role \"architect\": " + webHasRole + "\n" + "EJB user has role \"architect\": " + ejbHasRole
59+
+ "\n");
60+
61+
}
62+
63+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.javaee7.jaspic.registersession.servlet;
2+
import static java.util.logging.Level.SEVERE;
3+
4+
import java.io.IOException;
5+
import java.util.logging.Logger;
6+
7+
import javax.ejb.EJB;
8+
import javax.servlet.ServletException;
9+
import javax.servlet.annotation.WebServlet;
10+
import javax.servlet.http.HttpServlet;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
import org.javaee7.jaspic.registersession.ejb.PublicEJB;
15+
16+
17+
/**
18+
*
19+
* @author Arjan Tijms
20+
*
21+
*/
22+
@WebServlet(urlPatterns = "/public/servlet-public-ejb")
23+
public class PublicServletPublicEJB extends HttpServlet {
24+
25+
private static final long serialVersionUID = 1L;
26+
private final static Logger logger = Logger.getLogger(PublicServletPublicEJB.class.getName());
27+
28+
@EJB
29+
private PublicEJB publicEJB;
30+
31+
@Override
32+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
33+
34+
String webName = null;
35+
if (request.getUserPrincipal() != null) {
36+
webName = request.getUserPrincipal().getName();
37+
}
38+
39+
String ejbName = "";
40+
try {
41+
ejbName = publicEJB.getUserName();
42+
} catch (Exception e) {
43+
logger.log(SEVERE, "", e);
44+
}
45+
46+
response.getWriter().write("web username: " + webName + "\n" + "EJB username: " + ejbName + "\n");
47+
48+
}
49+
50+
}

0 commit comments

Comments
 (0)