Skip to main content

Testing Basic Authentication in Glassfish Embedded with Arquillian

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
lusabo
Offline
Joined: 2013-09-19

Hi all,

I'm trying to create a servlet to test Basic Authentication. I'm using Arquillian with Glassfish Embedded.

I have some questions:

1. I created a user in a separate Glassfish, I saw that it put my user in a file named keyfile. Where should I put this file in my project?

2. I put this in my web.xml (the file is in src/test/resources/security), is it correct?



Exemplo
/AuthenticationServlet


user



BASIC
file

3. I create a file name sun-web.xml (the file is in src/test/resources/security) with the code below, is it correct?



user
users

4. I copy the file domain.xml from the separeted Glassfish and put it in the directory src/test/resources/security, is it correct?

Below, the code from my AuthenticationServlet.

public class AuthenticationServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

@Inject
private SecurityContext securityContext;

@Inject
private Credentials credentials;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
credentials.setUsername("asdrubal");
credentials.setPassword("asdrubal");
securityContext.login();
response.setStatus(HttpStatus.SC_OK);
}
}

Below, my keyfile content.

asdrubal;{SSHA256}xYbabe0zKCOUrsH4SNQ+MK75W7FxJujcDcAJ9iXruHm1uT5mn+yktw==;users

Below, my test class:

@RunWith(Arquillian.class)
public class SecurityTest {

private static final String PATH = "src/test/resources/security";

@ArquillianResource
private URL deploymentUrl;

@Deployment(testable = false)
public static WebArchive createDeployment() {
return Tests.createDeployment().addClass(AuthenticationServlet.class)
.add(Tests.createFileAsset(PATH + "/keyfile"), "keyfile")
.addAsWebInfResource(Tests.createFileAsset(PATH + "/domain.xml"), "domain.xml")
.addAsWebInfResource(Tests.createFileAsset(PATH + "/sun-web.xml"), "sun-web.xml")
.addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml");
}

@Test
public void authentication() throws Exception {
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(deploymentUrl + "/AuthenticationServlet");
try {
int status = client.executeMethod(method);
assertEquals(HttpStatus.SC_OK, status);
} catch (Exception e) {
fail();
}
}
}

Where I'm going wrong?