Java security is a critical aspect of building secure applications, especially in environments where sensitive data is handled. Below are some common interview questions related to Java Security:
Basic Concepts
- What is Java Security?
- Java Security refers to the mechanisms and practices used to protect Java applications from vulnerabilities, unauthorized access, and malicious attacks.
- What is the Java Security Manager?
- The Java Security Manager is a class that defines the security policy for a Java application, controlling access to system resources.
- What is the difference between authentication and authorization?
- Authentication verifies the identity of a user, while authorization determines what actions a user is allowed to perform.
- What is a security policy in Java?
- A security policy defines the permissions granted to code running in the Java runtime environment.
Cryptography
- What is cryptography?
- Cryptography is the practice of securing communication and data through encryption and decryption techniques.
- What is the difference between symmetric and asymmetric encryption?
- Symmetric encryption uses the same key for encryption and decryption, while asymmetric encryption uses a pair of keys (public and private).
- What is the Java Cryptography Architecture (JCA)?
- JCA is a framework that provides APIs for cryptographic operations, including encryption, digital signatures, and key management.
- What is a digital signature?
- A digital signature is a cryptographic technique used to verify the authenticity and integrity of a message or document.
- What is a KeyStore in Java?
- A KeyStore is a repository for storing cryptographic keys and certificates.
Secure Coding Practices
- What are some common security vulnerabilities in Java applications?
- SQL injection, cross-site scripting (XSS), insecure deserialization, and improper error handling.
- How do you prevent SQL injection in Java?
- Use prepared statements or parameterized queries.
String query = "SELECT * FROM users WHERE username = ?"; PreparedStatement pstmt = connection.prepareStatement(query); pstmt.setString(1, username); ResultSet rs = pstmt.executeQuery();
- How do you prevent cross-site scripting (XSS) in Java?
- Validate and sanitize user input, and use frameworks like OWASP ESAPI.
- What is secure deserialization?
- Secure deserialization involves validating and sanitizing data before deserializing it to prevent malicious attacks.
- How do you handle sensitive data in Java?
- Use encryption, avoid storing sensitive data in plaintext, and follow secure coding practices.
Authentication and Authorization
- What is OAuth2?
- OAuth2 is an authorization framework that allows third-party applications to access resources on behalf of a user.
- What is JWT (JSON Web Token)?
- JWT is a compact, URL-safe token format for securely transmitting information between parties as a JSON object.
- How do you implement authentication in a Java web application?
- Use frameworks like Spring Security or Java EE Security API.
- What is the difference between role-based and attribute-based access control?
- Role-based access control (RBAC) grants permissions based on roles, while attribute-based access control (ABAC) grants permissions based on attributes.
Java Security APIs
- What is the Java Authentication and Authorization Service (JAAS)?
- JAAS is a framework for user authentication and authorization in Java applications.
- What is the Java Secure Socket Extension (JSSE)?
- JSSE provides APIs for secure communication using SSL/TLS protocols.
- What is the Java Security API?
- The Java Security API provides classes and interfaces for cryptographic operations, key management, and secure random number generation.
Real-World Scenarios
- How would you secure a REST API in Java?
- Use HTTPS, implement authentication (e.g., OAuth2, JWT), and validate input.
- How would you handle password storage in a Java application?
- Use hashing algorithms like bcrypt or PBKDF2 with salt.
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); String hashedPassword = encoder.encode("password");
- How would you secure a Java web application against CSRF attacks?
- Use CSRF tokens and frameworks like Spring Security.
- How would you implement SSL/TLS in a Java application?
- Configure the server to use SSL/TLS and use the
javax.net.ssl
package for secure communication.
- Configure the server to use SSL/TLS and use the
Tools and Libraries
- What is OWASP?
- OWASP (Open Web Application Security Project) is a community that provides resources and tools for improving software security.
- What is the OWASP Top 10?
- The OWASP Top 10 is a list of the most critical security risks to web applications.
- What is Spring Security?
- Spring Security is a framework for securing Spring-based applications, providing features like authentication, authorization, and CSRF protection.
- What is Apache Shiro?
- Apache Shiro is a security framework that provides authentication, authorization, cryptography, and session management.
Coding Questions
- Write a Java program to encrypt and decrypt a string using AES.
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.util.Base64; public class AESExample { public static void main(String[] args) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(128); SecretKey secretKey = keyGen.generateKey(); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encrypted = cipher.doFinal("Hello, World!".getBytes()); System.out.println("Encrypted: " + Base64.getEncoder().encodeToString(encrypted)); cipher.init(Cipher.DECRYPT_MODE, secretKey); byte[] decrypted = cipher.doFinal(encrypted); System.out.println("Decrypted: " + new String(decrypted)); }}
- Write a Java program to generate a digital signature.
import java.security.*; import java.util.Base64; public class DigitalSignatureExample { public static void main(String[] args) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair keyPair = keyGen.generateKeyPair(); Signature signature = Signature.getInstance("SHA256withRSA"); signature.initSign(keyPair.getPrivate()); signature.update("Hello, World!".getBytes()); byte[] digitalSignature = signature.sign(); System.out.println("Signature: " + Base64.getEncoder().encodeToString(digitalSignature)); signature.initVerify(keyPair.getPublic()); signature.update("Hello, World!".getBytes()); boolean verified = signature.verify(digitalSignature); System.out.println("Verified: " + verified); }}