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

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

Introduction

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

Prototype

public String getName();

Source Link

Document

Returns the name of this principal.

Usage

From source file:io.github.azige.bbs.service.AccountService.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();

    Account account = accountRepository.findByAccountName(username);
    if (account == null) {
        throw new UsernameNotFoundException("User name not found");
    }//from w w w.  jav  a  2  s. c o m
    password = encryptPassword(password, account.getSalt());
    if (password.equals(account.getPassword())) {
        return new UsernamePasswordAuthenticationToken(account.getProfile(), password, Account.AUTHORITYS);
    }
    return authentication;
}

From source file:org.jblogcms.core.security.service.SecurityServiceImpl.java

@Override
public AccountDetails getCurrentAccount() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    try {/* w  w w  .  j  a v a 2s.c  o m*/
        if (!auth.getName().equals("anonymousUser")) {
            return ((AccountDetails) auth.getPrincipal());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.jblogcms.core.security.service.SecurityServiceImpl.java

@Override
public Long getCurrentAccountId() {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    try {//from www.j av  a 2 s .com
        if (!auth.getName().equals("anonymousUser")) {
            return ((AccountDetails) auth.getPrincipal()).getId();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:de.itsvs.cwtrpc.sample1.server.service.LoginServiceImpl.java

public String login(String userName, String password) throws AuthenticationException {
    final StringBuilder roleNames = new StringBuilder();
    final Authentication auth;

    auth = SecurityContextHolder.getContext().getAuthentication();
    log.info("Login of user '" + auth.getName() + "' (session ID "
            + RemoteServiceContextHolder.getContext().getServletRequest().getSession().getId() + ")");

    for (GrantedAuthority ga : auth.getAuthorities()) {
        if (roleNames.length() > 0) {
            roleNames.append(", ");
        }// w ww .j  a  v a 2s . com
        roleNames.append(ga.getAuthority());
    }

    return roleNames.toString();
}

From source file:com.ar.dev.tierra.api.config.security.CustomAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication auth) throws AuthenticationException {
    String username = String.valueOf(auth.getName());
    String password = String.valueOf(auth.getCredentials().toString());

    Usuarios us = null;//from   w  w  w  .ja  va  2s. c  o m
    boolean success = false;
    try {
        us = user.findUsuarioByUsername(username);
        success = passwordEncoder.matches(password, us.getPassword());
    } catch (Exception ex) {
    }
    if (success == true) {
        final List<GrantedAuthority> grantedAuths = new ArrayList<>();
        String authority;
        switch (us.getRoles().getNombreRol()) {
        case "ADMINISTRADOR":
            authority = "ROLE_ADMIN";
            break;
        case "VENDEDOR":
            authority = "ROLE_VENDEDOR";
            break;
        default:
            authority = "ROLE_NONE";
            break;
        }
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority);
        grantedAuths.add(grantedAuthority);
        final UserDetails principal = new User(username, password, grantedAuths);
        final Authentication authentication = new UsernamePasswordAuthenticationToken(principal, password,
                grantedAuths);
        us = null;
        return authentication;
    } else {
        throw new BadCredentialsException("Bad Credentials");
    }
}

From source file:com.devnexus.ting.security.DefaultSecurityFacade.java

@Override
public String getUsername() {
    Authentication authentication = this.getAuthentication();
    if (!(authentication instanceof AnonymousAuthenticationToken)) {
        String currentUserName = authentication.getName();
        return currentUserName;
    }/*from   w  ww . j  av a  2 s  .  c o  m*/
    return null;
}

From source file:fr.univrouen.poste.web.candidat.CandidatAddPosteCandidatureController.java

@RequestMapping(method = RequestMethod.GET, produces = "text/html")
@PreAuthorize("hasRole('ROLE_CANDIDAT')")
public String postesForm(Model uiModel) {

    if (!AppliConfig.getCacheCandidatCanSignup()) {
        return "redirect:/postecandidatures";
    }/*w  w  w.j a  va2 s.  c  o m*/

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String emailAddress = auth.getName();
    User candidat = User.findUsersByEmailAddress(emailAddress, null, null).getSingleResult();

    List<PosteAPourvoirAvailableBean> posteapourvoirs = posteAPourvoirService
            .getPosteAPourvoirAvailables(candidat);
    uiModel.addAttribute("posteapourvoirs", posteapourvoirs);
    return "addpostecandidatures/index";
}

From source file:sample.session.SpringSessionPrincipalNameSuccessHandler.java

public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {

    HttpSession session = request.getSession();
    String currentUsername = authentication.getName();

    // tag::set-username[]
    session.setAttribute(Session.PRINCIPAL_NAME_ATTRIBUTE_NAME, currentUsername);
    // end::set-username[]
}

From source file:md.ibanc.rm.sessions.CustomAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

    String username = authentication.getName();
    String password = authentication.getCredentials().toString();

    LoginFormValidator loginFormValidator = new LoginFormValidator();

    if (!loginFormValidator.isLoginValid(username)) {
        throw new BadCredentialsException("Numele de utilizator contine simboluri interzise");
    }/*www .  j  a  va 2s . co m*/

    if (!loginFormValidator.isValidPassword(password)) {
        throw new BadCredentialsException("Parola contine simboluri interzise");
    }

    Users users = usersService.findUserByEmail(username);

    if (users == null) {
        throw new UsernameNotFoundException("Asa utilizator nu exista in baza de date");
    }

    String hashPassword = UtilHashMD5.createMD5Hash(FormatPassword.CreatePassword(users.getId(), password));

    if (!hashPassword.equals(users.getPassword())) {
        throw new BadCredentialsException("Parola este gresita");
    }

    if (!users.getActive()) {
        throw new AccountExpiredException("Nu aveti drept sa accesati acest serviciu");
    }

    Collection<? extends GrantedAuthority> authoritiesRoles = getAuthorities(users);

    return new UsernamePasswordAuthenticationToken(username, hashPassword, authoritiesRoles);

}

From source file:org.n52.oss.ui.services.OSSAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication arg0) throws AuthenticationException {
    String username = arg0.getName();
    String password = arg0.getCredentials().toString();

    AuthToken token = authenticateOSS(username, password);

    if (token.auth_token != null) {
        if (!token.isValid)
            throw new UsernameNotFoundException(
                    "Username is not validated please contact site administration!");

        final List<GrantedAuthority> grantedAuths = new ArrayList<GrantedAuthority>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_SCRIPT_AUTHOR"));

        if (token.isAdmin)
            grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        final UserDetails principal = new User(username, token.auth_token, grantedAuths);
        final Authentication auth = new UsernamePasswordAuthenticationToken(principal, token.auth_token,
                grantedAuths);/* ww w  .  java2  s  .c om*/
        return auth;

    } else
        throw new UsernameNotFoundException("Wrong username/password combination");
}