@Test public void testNullPrincipal() { context.checking(new Expectations() { { oneOf(securityContext).getAuthentication(); will(returnValue(authentication)); oneOf(authentication).getPrincipal(); will(returnValue(null)); } }); assertNull(runTest(true)); }
private Principal getPrincipal() { try { SecurityContext securityCtx = SecurityContextHolder.getContext(); if (securityCtx == null) { throw new AuthorizationException("No security context available."); } Authentication auth = securityCtx.getAuthentication(); if (auth == null) { return null; } Object obj = auth.getPrincipal(); if (obj instanceof UserDetails) { UserDetails user = (UserDetails) obj; ExtendedUserDetails extUser = (ExtendedUserDetails) user; return new DefaultPrincipal(extUser.getUsername(), extUser.getPerson().getOpenSocialId(), extUser .getPerson().getId()); } else { return null; } } catch (Exception ex) { logger.error("Error occurred populating Principal object for current request.", ex); if (exceptionOnError) { throw new AuthorizationException("Error occurred populating Principal object for current request."); } else { return null; } } }
SpringSecurityContextPrincipalPopulator implements PrincipalPopulator,
        Transformer<Request, Principal> { private Principal getPrincipal() { try { SecurityContext securityCtx = SecurityContextHolder.getContext(); if (securityCtx == null) { throw new AuthorizationException("No security context available."); } Authentication auth = securityCtx.getAuthentication(); if (auth == null) { return null; } Object obj = auth.getPrincipal(); if (obj instanceof UserDetails) { UserDetails user = (UserDetails) obj; ExtendedUserDetails extUser = (ExtendedUserDetails) user; return new DefaultPrincipal(extUser.getUsername(), extUser.getPerson().getOpenSocialId(), extUser .getPerson().getId()); } else { return null; } } catch (Exception ex) { logger.error("Error occurred populating Principal object for current request.", ex); if (exceptionOnError) { throw new AuthorizationException("Error occurred populating Principal object for current request."); } else { return null; } } } }
SpringSecurityContextPrincipalPopulator implements PrincipalPopulator,
        Transformer<Request, Principal> { private Principal getPrincipal() { try { SecurityContext securityCtx = SecurityContextHolder.getContext(); if (securityCtx == null) { throw new AuthorizationException("No security context available."); } Authentication auth = securityCtx.getAuthentication(); if (auth == null) { return null; } Object obj = auth.getPrincipal(); if (obj instanceof UserDetails) { UserDetails user = (UserDetails) obj; ExtendedUserDetails extUser = (ExtendedUserDetails) user; return new DefaultPrincipal(extUser.getUsername(), extUser.getPerson().getOpenSocialId(), extUser .getPerson().getId()); } else { return null; } } catch (Exception ex) { logger.error("Error occurred populating Principal object for current request.", ex); if (exceptionOnError) { throw new AuthorizationException("Error occurred populating Principal object for current request."); } else { return null; } } } SpringSecurityContextPrincipalPopulator(final boolean inExceptionOnError); }
SpringSecurityContextPrincipalPopulator implements PrincipalPopulator,
        Transformer<Request, Principal> { private Principal getPrincipal() { try { SecurityContext securityCtx = SecurityContextHolder.getContext(); if (securityCtx == null) { throw new AuthorizationException("No security context available."); } Authentication auth = securityCtx.getAuthentication(); if (auth == null) { return null; } Object obj = auth.getPrincipal(); if (obj instanceof UserDetails) { UserDetails user = (UserDetails) obj; ExtendedUserDetails extUser = (ExtendedUserDetails) user; return new DefaultPrincipal(extUser.getUsername(), extUser.getPerson().getOpenSocialId(), extUser .getPerson().getId()); } else { return null; } } catch (Exception ex) { logger.error("Error occurred populating Principal object for current request.", ex); if (exceptionOnError) { throw new AuthorizationException("Error occurred populating Principal object for current request."); } else { return null; } } } SpringSecurityContextPrincipalPopulator(final boolean inExceptionOnError); @Override Principal getPrincipal(final String inAccountId, final String inSessionId); @Override Principal transform(final Request inTransformType); }
SpringSecurityContextPrincipalPopulator implements PrincipalPopulator,
        Transformer<Request, Principal> { private Principal getPrincipal() { try { SecurityContext securityCtx = SecurityContextHolder.getContext(); if (securityCtx == null) { throw new AuthorizationException("No security context available."); } Authentication auth = securityCtx.getAuthentication(); if (auth == null) { return null; } Object obj = auth.getPrincipal(); if (obj instanceof UserDetails) { UserDetails user = (UserDetails) obj; ExtendedUserDetails extUser = (ExtendedUserDetails) user; return new DefaultPrincipal(extUser.getUsername(), extUser.getPerson().getOpenSocialId(), extUser .getPerson().getId()); } else { return null; } } catch (Exception ex) { logger.error("Error occurred populating Principal object for current request.", ex); if (exceptionOnError) { throw new AuthorizationException("Error occurred populating Principal object for current request."); } else { return null; } } } SpringSecurityContextPrincipalPopulator(final boolean inExceptionOnError); @Override Principal getPrincipal(final String inAccountId, final String inSessionId); @Override Principal transform(final Request inTransformType);  }