Jakarta EE Security | Test 2
Jakarta EE Security Tests are assessments designed to measure a developerโs expertise in securing enterprise applications built using the Jakartaย Enterprise Edition. These tests cover critical areas such as authentication, authorization, role-based access control, data encryption, secure communication, and the proper use of Jakarta EE security APIs like JAAS. By challenging developers with real-world scenarios and security problems, these tests ensure that they are equipped to implement robust security mechanisms, protect sensitive enterprise data, and defend against common vulnerabilities in complex, distributed environments. Ideal for Jakarta EE developers and security engineers, these tests enhance your capability to build secure and compliant enterprise applications.
1 / 12
1. What does the security-constraint element in web.xml define?
Correct Answer: The access control policies for web resources
The security-constraint element in the web.xml file defines the access control policies for web resources in a Java EE application. It specifies which resources (such as URLs or servlets) require authentication and defines the roles allowed to access those resources, helping to enforce security constraints on the application.
Explanation of other options:
2 / 12
2. What is the primary role of the login-config element in web.xml?
Correct Answer: To define the login mechanism used by the application.
The login-config element in the web.xml file is used to define the login mechanism employed by the Java EE application. This includes specifying how users are authenticated (such as using BASIC, FORM, DIGEST, or CLIENT-CERT authentication) when accessing protected resources.
3 / 12
3. What is the primary function of the javax.annotation.security package in Java EE?
Correct Answer: Provides annotations for declarative security in Java EE applications
The javax.annotation.security package in Java EE provides annotations for declarative security in Java EE applications. These annotations, such as @RolesAllowed, @PermitAll, and @DenyAll, allow developers to define security constraints directly in the code, making it easier to manage access control at the method level in a declarative manner.
4 / 12
4. In Java EE, how can you specify that a servlet requires SSL transport to be accessed?
In Java EE, you specify that a servlet requires SSL transport by configuring the transport-guarantee element inside the security-constraint element in the web.xml file. The transport-guarantee can be set to CONFIDENTIAL to ensure that the servlet can only be accessed over SSL (HTTPS), enforcing secure communication.
<security-constraint>
<web-resource-collection>
<web-resource-name>SecureServlet</web-resource-name>
<url-pattern>/secure/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
5 / 12
5. Which annotation in Java EE is used to indicate that a method can be accessed by any authenticated user?
The @PermitAll annotation in Java EE indicates that a method or class can be accessed by any authenticated user, regardless of their role. It allows unrestricted access to a resource for all users who have been authenticated.
@PermitAll
public void someMethod() {
// Method accessible by any authenticated user
}
The other annotations like @RolesAllowed restrict access to specific roles, and @DenyAll blocks access to everyone.
6 / 12
6. In Java EE, which framework can be integrated to provide additional security features such as single sign-on (SSO) and federation?
Spring Security is a framework that can be integrated with Java EE to provide additional security features, including single sign-on (SSO), federation, authentication, and authorization. It extends the security capabilities of Java EE, offering customizable security configurations for web applications and enterprise systems.
The other options:
7 / 12
7. Which of the following Java EE features allows you to specify the security roles for an EJB at runtime?
The @RunAs annotation in Java EE allows you to specify the security role that an Enterprise Java Bean (EJB) should assume at runtime when it calls another EJB. This is useful when an EJB needs to perform certain actions under a different security role than the one associated with the current user.
@RunAs(“admin”)
public class MyServiceBean implements MyService {
// This bean will execute with the “admin” role
The other annotations, like @RolesAllowed and @PermitAll, control access to methods based on the roles of the current user but do not change the runtime role of the EJB itself.
8 / 12
8. Which Java EE component can be used to perform custom authentication and authorization logic?
A Servlet Filter in Java EE can be used to perform custom authentication and authorization logic. Filters allow you to intercept and manipulate incoming requests and responses, making them ideal for handling security checks, logging, and modifying request data before it reaches the servlet or other components.
You can implement custom authentication and authorization by inspecting the request, validating credentials, and controlling access based on roles or permissions.
@WebFilter(“/secure/*”)
public class AuthenticationFilter implements Filter {
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
// Custom authentication logic here
if (isAuthenticated(httpRequest)) {
chain.doFilter(request, response);ย // User is authenticated, proceed to next filter or servlet
} else {
httpResponse.sendRedirect(“/login”);ย // Redirect to login page
private boolean isAuthenticated(HttpServletRequest request) {
// Check if the user is authenticated (e.g., based on session or token)
return request.getSession().getAttribute(“user”) != null;
9 / 12
9. Which method in the HttpServletRequest interface can be used to check if a user belongs to a specific role?
The isUserInRole() method in the HttpServletRequest interface is used to check if a user belongs to a specific role. This method returns a boolean value indicating whether the authenticated user has the specified role.
if (request.isUserInRole(“admin”)) {
// The user has the “admin” role
// The user does not have the “admin” role
The other methods mentioned do not exist in the HttpServletRequest interface:
10 / 12
10. In Java EE, how can you programmatically obtain the principal name of the authenticated user in a servlet?
In Java EE, you can programmatically obtain the principal name of the authenticated user in a servlet by using the getUserPrincipal() method of the HttpServletRequest interface, followed by the getName() method.
Eg:
String userName = request.getUserPrincipal().getName();
This code retrieves the authenticated user’s principal name (usually the username) associated with the current request.
The other options mentioned do not exist in the HttpServletRequest interface:
11 / 12
11. What is the role of a security realm in Java EE?
In Java EE, a security realm serves multiple roles, including:
By providing these functionalities, a security realm helps manage authentication and authorization effectively in Java EE applications.
12 / 12
12. What is the primary function of the javax.security.enterprise package in Java EE?
Correct Answer: Provides an API for enterprise security, including authentication and identity management in Java EE.
The javax.security.enterprise package in Java EE provides an API for enterprise security, which includes features like authentication, identity management, authorization, and security context management. It is part of the Java EE Security API, offering flexible and robust mechanisms for securing Java EE applications.
Your score is
The average score is 0%
Restart Test
Related challenges :