JAASAuthenticationService extends GroupAdapterAuthorizationSource implements AuthenticationService { @Override public User login(final String username, final String password) { final SecurityManager jsm = System.getSecurityManager(); if (jsm != null) { final ClassLoader tccl = Thread.currentThread().getContextClassLoader(); final ClassLoader cl = this.getClass().getClassLoader(); try { Thread.currentThread().setContextClassLoader(cl); return executeLogin(username, password); } catch (final LoginException ex) { throw new FailedAuthenticationException(); } finally { if (tccl != null) { Thread.currentThread().setContextClassLoader(tccl); } } } else { try { return executeLogin(username, password); } catch (final LoginException ex) { throw new FailedAuthenticationException(); } } } JAASAuthenticationService(String domain); @Override User login(final String username,
                      final String password); @Override void logout(); @Override User getUser(); @Override boolean isLoggedIn(); static final String DEFAULT_DOMAIN; }
@Test public void testLogin() throws Exception { String username = "user1"; String password = "password1"; Set<Principal> principals = mockPrincipals( "admin", "role1", "group1" ); Subject subject = new Subject(); subject.getPrincipals().addAll( principals ); LoginContext loginContext = mock( LoginContext.class ); when( loginContext.getSubject() ).thenReturn( subject ); doReturn( loginContext ).when( tested ).createLoginContext( anyString(), anyString() ); User user = tested.login( username, password ); assertNotNull( user ); assertEquals( username, user.getIdentifier() ); assertEquals( 2, user.getRoles().size() ); assertTrue( user.getRoles().contains( new RoleImpl( "admin" ) ) ); assertTrue( user.getRoles().contains( new RoleImpl( "role1" ) ) ); assertEquals( 1, user.getGroups().size() ); assertTrue( user.getGroups().contains( new GroupImpl( "group1" ) ) ); }