javax.security.auth


1. Authenticating a User with JAAS

LoginContext lc = new LoginContext("MyLoginModule", new CallbackHandler() {
  @Override
  public void handle(Callback[] callbacks) throws IOException {
    for (Callback callback : callbacks) {
      if (callback instanceof NameCallback) {
        ((NameCallback) callback).setName("username");
      } else if (callback instanceof PasswordCallback) {
        ((PasswordCallback) callback).setPassword("password".toCharArray());
      }
    }
  }
});
lc.login();

2. Authorizing a User with JAAS

Subject subject = LoginContext.getCurrent().getSubject();
boolean authorized = subject.isPermitted(new Permission("my.permission"));

3. Creating a Custom Login Module

public class MyLoginModule implements LoginModule {
  @Override
  public boolean login() {
    // Implement user authentication logic here
    return true;
  }

  @Override
  public boolean commit() {
    // Implement user authorization logic here
    return true;
  }

  @Override
  public boolean abort() {
    // Implement cleanup logic here
    return true;
  }
}

4. Using OAuth2 with JAAS

5. Using SAML with JAAS

6. Creating a Credential

7. Getting the Principal from a Credential

8. Creating a Subject

9. Adding a Credential to a Subject

10. Getting the Credentials from a Subject

11. Creating a LoginContext with Default Configuration

12. Creating a LoginContext with a Specific Configuration

13. Logging in with a LoginContext

14. Getting the Subject from a LoginContext

15. Logout from a LoginContext

16. Creating a PrivilegedAction

17. Executing a PrivilegedAction with a LoginContext

18. Creating an AccessController

19. Checking Permission with an AccessController

20. Creating a Policy

21. Setting a Policy with an AccessController

22. Creating a ProtectionDomain

23. Getting the ProtectionDomain for a Class

24. Getting the Permissions from a ProtectionDomain

25. Creating a CodeSource

26. Getting the CodeSource for a Class

27. Creating a Policy File

28. Installing a Policy File

29. Creating a Private Key

30. Creating a PublicKey

31. Creating a PrivateKey

32. Creating a Signature

33. Verifying a Signature

34. Creating a Message Digest

35. Creating a Message Digest for a File

36. Creating a Key Store

37. Getting a Key from a Key Store

38. Adding a Key to a Key Store

39. Creating a Certificate

40. Creating a Trust Manager

41. Creating a Secure Socket Factory

42. Creating a Key Pair Generator

43. Creating a Cipher

44. Encrypting Data with a Cipher

45. Decrypting Data with a Cipher

46. Creating a Secure Random

47. Creating a Random Number

48. Creating a SignedObject

49. Verifying a SignedObject

50. Creating a Certificate Authority