Example usage for org.springframework.security.core Authentication getPrincipal

List of usage examples for org.springframework.security.core Authentication getPrincipal

Introduction

In this page you can find the example usage for org.springframework.security.core Authentication getPrincipal.

Prototype

Object getPrincipal();

Source Link

Document

The identity of the principal being authenticated.

Usage

From source file:com.github.sshw.security.SSHAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    SSHSession sshSession = new SSHSessionImpl();
    //UsernamePasswordAuthenticationToken request = (UsernamePasswordAuthenticationToken) authentication;
    String username = authentication.getPrincipal().toString();
    String password = authentication.getCredentials().toString();
    //log.info("{}:{}", username, password);
    boolean success = sshSession.login(username, password);
    log.info("SSH login: {}={}", username, success);
    Authentication result = new UsernamePasswordAuthenticationToken(username, password, authorities);
    //result.setAuthenticated(success);
    log.info("adding SSH session for {}", username);
    sshSessionManager.sessionsByUsername.put(username, sshSession);
    return result;
}

From source file:cz.swi2.mendeluis.service.facade.UserFacade.java

@Override
public UserDTO getLoggedUser() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (auth == null) {
        return null;
    }/*from   w ww  .  j a v  a2 s .  com*/

    String username = ((org.springframework.security.core.userdetails.User) auth.getPrincipal()).getUsername();

    if (username == null) {
        return null;
    }

    UserDTO user = this.getUserByUsername(username);

    if (user == null) {
        return null;
    }

    return user;
}

From source file:org.openeos.services.security.internal.SecurityManagerServiceImpl.java

@Override
public Principal getAuthenticatedPrincipal() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null || !auth.isAuthenticated()) {
        // TODO/*  w w w.  j  ava2 s.c  o m*/
        throw new RuntimeException("Actually not user is logged in");
    }

    Object principal = auth.getPrincipal();
    if (principal instanceof InternalUser) {
        InternalUser userDetails = (InternalUser) principal;
        return new PrincipalImpl(userDetails.getId());
    } else {
        throw new RuntimeException("The class of principal is unknown");
    }
}

From source file:org.appverse.web.framework.backend.api.services.presentation.impl.live.AuthenticationServiceFacadeImpl.java

/**
 * Checks if the current user is authenticated by evaluating the
 * SecurityContext/*  ww w.j  a  v  a2 s . c  om*/
 * 
 * @return Boolean true if the current user is authenticated and false
 *         elsewise
 */
@Override
public boolean isPrincipalAuthenticated() {
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication != null) {
        final UserDetails userPrincipal = (UserDetails) authentication.getPrincipal();
        return userPrincipal != null;
    } else {
        return false;
    }

}

From source file:org.wallride.web.support.DefaultModelAttributeInterceptor.java

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
        ModelAndView mv) throws Exception {
    if (mv == null)
        return;//w  w w  . j  a  v  a 2 s . co  m
    if (mv.getView() instanceof RedirectView)
        return;
    if (mv.getViewName().startsWith("redirect:"))
        return;

    Blog blog = blogService.getBlogById(Blog.DEFAULT_ID);
    mv.addObject("BLOG", blog);

    List<String> languages = new ArrayList<>();
    if (blog != null) {
        for (BlogLanguage blogLanguage : blog.getLanguages()) {
            languages.add(blogLanguage.getLanguage());
        }
    }

    String currentLanguage = LocaleContextHolder.getLocale().getLanguage();

    mv.addObject("LANGUAGES", languages.toArray(new String[languages.size()]));
    mv.addObject("LANGUAGE_LINKS", buildLanguageLinks(currentLanguage, languages, request));

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    AuthorizedUser authorizedUser = null;
    if (authentication != null && authentication.getPrincipal() instanceof AuthorizedUser) {
        authorizedUser = (AuthorizedUser) authentication.getPrincipal();
    }
    mv.addObject("USER", authorizedUser);

    mv.addObject("WEBSITE_TITLE", blog != null ? blog.getTitle(currentLanguage) : null);
    mv.addObject("WEBSITE_LINK", buildGuestLink());
    mv.addObject("WEBSITE_PATH", buildGuestPath(currentLanguage, languages));

    mv.addObject("ADMIN_LINK", buildAdminLink());
    mv.addObject("ADMIN_PATH", buildAdminPath(currentLanguage));
}

From source file:com.bisone.saiku.security.replace.SessionService.java

public void authenticate(HttpServletRequest req, String username, String password) {
    try {/*w w  w .  ja  v a 2 s.  co m*/
        PreAuthenticatedAuthenticationToken token = new PreAuthenticatedAuthenticationToken(username, password);
        token.setDetails(new WebAuthenticationDetails(req));
        Authentication authentication = this.authenticationManager.authenticate(token);
        log.debug("Logging in with [{}]", authentication.getPrincipal());
        SecurityContextHolder.getContext().setAuthentication(authentication);
    } catch (BadCredentialsException bd) {
        throw new RuntimeException("Authentication failed for: " + username, bd);
    }

}

From source file:gmc.gestaxi.controller.UserServiceImpl.java

@Override
public UserApi getLoggedInUser() {
    Authentication userAuth = SecurityContextHolder.getContext().getAuthentication();
    if (userAuth != null) {
        return (UserApi) userAuth.getPrincipal();
    } else {//  ww  w  .j ava 2  s.  c  om
        return null;
    }
}

From source file:org.jasig.schedassist.web.register.delegate.DelegateRegistrationHelper.java

/**
 * Returns the value of the name field if {@link IDelegateCalendarAccount#getLocation()} does not return null; otherwise
 * returns the default value of {@link Preferences#LOCATION}.
 * /*from   w  w w  .j av a  2 s . c om*/
 * @return a {@link String} containing the current authenticated {@link IDelegateCalendarAccount} location name
 */
public String currentDelegateLocation() {
    SecurityContext context = SecurityContextHolder.getContext();
    Authentication authentication = context.getAuthentication();
    DelegateCalendarAccountUserDetailsImpl currentUser = (DelegateCalendarAccountUserDetailsImpl) authentication
            .getPrincipal();
    String accountLocation = currentUser.getDelegateCalendarAccount().getLocation();
    if (StringUtils.isNotBlank(accountLocation)) {
        return accountLocation;
    } else {
        return Preferences.LOCATION.getDefaultValue();
    }
}

From source file:com.trenako.web.security.SpringUserContext.java

@Override
public AccountDetails getCurrentUser() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    // throw away anonymous tokens
    if (authentication instanceof AnonymousAuthenticationToken) {
        return null;
    }//www . j a va  2  s.  com
    return authentication == null ? null : (AccountDetails) authentication.getPrincipal();
}

From source file:org.appverse.web.framework.backend.persistence.services.integration.helpers.EntityListener.java

public String getUsername() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String username = null;// w  w  w.j ava  2  s. co  m
    if (authentication == null) {
        username = "batch_process";
    } else {
        UserDetails user = (UserDetails) authentication.getPrincipal();
        username = user.getUsername();
    }
    return username;
}