Example usage for org.springframework.security.core.userdetails User getUsername

List of usage examples for org.springframework.security.core.userdetails User getUsername

Introduction

In this page you can find the example usage for org.springframework.security.core.userdetails User getUsername.

Prototype

public String getUsername() 

Source Link

Usage

From source file:com.orchestra.portale.externalauth.FbAuthenticationManager.java

private static User fbUserCheck(String userFbId, String userFbMail, UserRepository userRepository)
        throws UserNotFoundException {
    // Recupera i dati dell'utente userServiceId dal repository 
    com.orchestra.portale.persistence.sql.entities.User domainUser = userRepository
            .findByFbEmailOrFbUser(userFbMail, userFbId);

    if (domainUser == null || domainUser.getUsername() == null || "".equals(domainUser.getUsername())) {
        throw new UserNotFoundException();
    }//  w  w  w.j a v a  2 s  .c om

    boolean enabled = true;
    boolean accountNonExpired = true;
    boolean credentialsNonExpired = true;
    boolean accountNonLocked = true;

    User user = new User(domainUser.getUsername(), domainUser.getPassword().toLowerCase(), enabled,
            accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(domainUser.getRoles()));

    return user;
}

From source file:org.runway.utils.AuthenticationUtils.java

public static void autoLogin(User user, HttpServletRequest request,
        AuthenticationManager authenticationManager) {

    //           GrantedAuthority[] grantedAuthorities = new GrantedAuthority[] { new GrantedAuthorityImpl(
    //             user.getAuthority()) };

    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user.getUsername(),
            user.getPassword(), user.getAuthorities());

    // generate session if one doesn't exist
    HttpSession session = request.getSession();

    token.setDetails(new WebAuthenticationDetails(request));
    Authentication authenticatedUser = authenticationManager.authenticate(token);

    SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
    // setting role to the session
    session.setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY,
            SecurityContextHolder.getContext());

}

From source file:net.shibboleth.idp.oidc.client.userinfo.authn.SpringSecurityAuthenticationTokenFactory.java

/**
 * Build authentication authentication.//from w  ww  .  j  av a 2 s .c  o m
 *
 * @param profileRequestContext the profile request context
 * @return the authentication
 */
public static Authentication buildAuthentication(final ProfileRequestContext profileRequestContext) {
    final SubjectContext principal = profileRequestContext.getSubcontext(SubjectContext.class);

    if (principal == null || principal.getPrincipalName() == null) {
        throw new OIDCException("No SubjectContext found in the profile request context");
    }

    /**
     * Grab the authentication context class ref and classify it as an authority to be used later
     * by custom token services to generate acr and amr claims.
     *
     * MitreID connect can only work with SimpleGrantedAuthority. So here we are creating specific authority
     * instances first and then converting them to SimpleGrantedAuthority. The role could be parsed later to
     * locate and reconstruct the actual instance.
     */
    final Set<GrantedAuthority> authorities = new LinkedHashSet<>();
    authorities.add(new SimpleGrantedAuthority(OIDCConstants.ROLE_USER));

    final AuthenticationContext authCtx = profileRequestContext.getSubcontext(AuthenticationContext.class);
    if (authCtx != null) {
        LOG.debug("Found an authentication context in the profile request context");

        final RequestedPrincipalContext principalContext = authCtx
                .getSubcontext(RequestedPrincipalContext.class);
        if (principalContext != null && principalContext.getMatchingPrincipal() != null) {
            LOG.debug("Found requested principal context context with matching principal {}",
                    principalContext.getMatchingPrincipal().getName());

            final AuthenticationClassRefAuthority authority = new AuthenticationClassRefAuthority(
                    principalContext.getMatchingPrincipal().getName());

            LOG.debug("Adding authority {}", authority.getAuthority());
            authorities.add(new SimpleGrantedAuthority(authority.toString()));
        }
        if (authCtx.getAuthenticationResult() != null) {
            final AuthenticationMethodRefAuthority authority = new AuthenticationMethodRefAuthority(
                    authCtx.getAuthenticationResult().getAuthenticationFlowId());
            LOG.debug("Adding authority {}", authority.getAuthority());
            authorities.add(new SimpleGrantedAuthority(authority.toString()));
        }
    }

    /**
     * Note that Spring Security loses the details object when it attempts to grab onto the authentication
     * object that is combined, when codes are asking to create access tokens.
     */
    final User user = new User(principal.getPrincipalName(), UUID.randomUUID().toString(),
            Collections.singleton(new SimpleGrantedAuthority(OIDCConstants.ROLE_USER)));

    LOG.debug("Created user details object for {} with authorities {}", user.getUsername(),
            user.getAuthorities());

    final SpringSecurityAuthenticationToken authenticationToken = new SpringSecurityAuthenticationToken(
            profileRequestContext, authorities);
    LOG.debug("Final authentication token authorities are {}", authorities);

    authenticationToken.setAuthenticated(true);
    authenticationToken.setDetails(user);
    return authenticationToken;
}

From source file:sample.mvc.AuthenticationPrincipalController.java

@RequestMapping("/principal")
public String principal(@AuthenticationPrincipal User principal) {
    return principal.getUsername();
}

From source file:sample.AuthenticationPrincipalController.java

@RequestMapping(value = "/principal", method = RequestMethod.GET)
public String principal(@AuthenticationPrincipal User principal) {
    return principal.getUsername();
}

From source file:com.sentinel.config.TokenHandler.java

public String createTokenForUser(User user) {
    return Jwts.builder().setSubject(user.getUsername()).signWith(SignatureAlgorithm.HS512, secret).compact();
}

From source file:net.seedboxer.web.security.SeedBoxerUserDetails.java

public SeedBoxerUserDetails(net.seedboxer.core.domain.User user) {
    super(user.getUsername(), user.getPassword(), createAuthorities(user));
    this.user = user;
}

From source file:com.vdenotaris.spring.boot.security.saml.web.contollers.LandingController.java

@RequestMapping("/landing")
public String landing(@CurrentUser User user, Model model) {
    model.addAttribute("username", user.getUsername());
    return "landing";
}

From source file:jp.pigumer.sso.Application.java

/**
 * /hello./*from  w  ww .java 2s. c o  m*/
 * 
 * @param user User
 * @param model Model
 * @return hello
 */
@RequestMapping("/hello")
String hello(@CurrentUser User user, Model model) {
    model.addAttribute("user", user.getUsername());
    return "hello";
}

From source file:org.vaadin.spring.samples.mvp.ui.presenter.BannerPresenter.java

@EventBusListenerMethod(filter = StartupFilter.class)
public void onStartup(Action action) {
    if (security.isAuthenticated()) {
        Authentication auth = security.getAuthentication();
        User user = (User) auth.getPrincipal();
        getView().setUser(user.getUsername());
    }/*from   w w  w . ja  va  2  s .c o  m*/
}