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:com.github.iexel.fontus.web.security.CustomAuthenticationProvider.java

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

    if ((name.equals("admin")) && (password.equals("admin"))) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMINISTRATOR"));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        return auth;
    }/*  w ww . j ava  2 s  . c  o m*/

    if ((name.equals("user")) && (password.equals("user"))) {
        List<GrantedAuthority> grantedAuths = new ArrayList<>();
        grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
        Authentication auth = new UsernamePasswordAuthenticationToken(name, password, grantedAuths);
        return auth;
    }

    return null;
}

From source file:net.thewaffleshop.passwd.security.AccountAuthenticationProvider.java

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

    Account user = accountService.authenticateUser(userName, password);
    SecretKey sk = accountAPI.getSecretKey(user, password);

    List<SimpleGrantedAuthority> auths = Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
    Authentication auth = new AccountAuthenticationToken(userName, password, auths, user, sk);
    return auth;/*w w w  .ja  v a2 s . c om*/
}

From source file:br.com.sicva.seguranca.ProvedorAutenticacao.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();
    UsuariosDao usuariosDao = new UsuariosDao();
    Usuarios usuario = usuariosDao.PesquisarUsuario(name);

    if (usuario == null || !usuario.getUsuariosCpf().equalsIgnoreCase(name)) {
        throw new BadCredentialsException("Username not found.");
    }/*from  w  w  w  . j  av  a2  s  .  co m*/

    if (!GenerateMD5.generate(password).equals(usuario.getUsuarioSenha())) {
        throw new LockedException("Wrong password.");
    }
    if (!usuario.getUsuarioAtivo()) {
        throw new DisabledException("User is disable");
    }
    List<GrantedAuthority> funcoes = new ArrayList<>();
    funcoes.add(new SimpleGrantedAuthority("ROLE_" + usuario.getFuncao().getFuncaoDescricao()));
    Collection<? extends GrantedAuthority> authorities = funcoes;
    return new UsernamePasswordAuthenticationToken(name, password, authorities);

}

From source file:tomekkup.helenos.security.web.authentication.JsonLogoutSuccesHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    logger.debug(String.format("user '%s' logged out", authentication.getName()));
    writeResult(response, null);/*from  w w  w  .j  a  va 2  s.  co  m*/
}

From source file:pl.altkom.gemalto.spring.ws.CrmWebServiceImpl.java

private void bySpringScurity() {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName(); //get logged in username

    Collection<? extends GrantedAuthority> roles = SecurityContextHolder.getContext().getAuthentication()
            .getAuthorities();/*  www  . j a  va  2 s.  c o m*/

    System.out.println(roles);

    System.out.println("logged user by spring = " + name);
}

From source file:SpringSecurity.LogoutHandler.java

@Override
public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    Students student = StudentDAO.getStudent(authentication.getName());
    if (student != null) {
        StudentDAO.setLoggedOut(authentication.getName());
    } else {//from  w w w.j  a va2  s.  c o m
        AdminDAO.setLoggedOut(authentication.getName());
    }

    super.onLogoutSuccess(request, response, authentication);
}

From source file:com.jiwhiz.TestAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    log.debug("Try to authenticate " + authentication.getName());

    TestAuthenticationToken authToken = (TestAuthenticationToken) authentication;
    UserDetails user = userDetailsService.loadUserByUsername(authToken.getName());
    log.debug("Found user " + user.getUsername() + " with authorities " + user.getAuthorities());
    Authentication authenticationToken = new TestAuthenticationToken(user.getUsername(), user.getAuthorities());
    return authenticationToken;
}

From source file:fr.gael.dhus.spring.security.handler.LoginSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) {
    String name = authentication.getName();
    try {/*from w w w  . ja  v  a2  s .  co  m*/
        ValidityAuthentication auth = (ValidityAuthentication) authentication;

        name = EncryptPassword.encrypt(name, PasswordEncryption.MD5);
        Cookie authCookie = new Cookie(CookieKey.AUTHENTICATION_COOKIE_NAME, name);
        authCookie.setPath("/");
        authCookie.setHttpOnly(true);
        authCookie.setMaxAge(-1);

        String validity = auth.getValidity();
        //         Cookie validityCookie = new Cookie (CookieKey.VALIDITY_COOKIE_NAME,
        //             validity);
        //         validityCookie.setPath ("/");
        //         validityCookie.setHttpOnly (true);

        String integrity = EncryptPassword.encrypt(name + validity, PasswordEncryption.SHA1);
        Cookie integrityCookie = new Cookie(CookieKey.INTEGRITY_COOKIE_NAME, integrity);
        integrityCookie.setPath("/");
        integrityCookie.setHttpOnly(true);
        integrityCookie.setMaxAge(-1);

        response.addCookie(authCookie);
        //         response.addCookie (validityCookie);
        response.addCookie(integrityCookie);
        request.getSession().setAttribute("integrity", integrity);
        SecurityContextProvider.saveSecurityContext(integrity, SecurityContextHolder.getContext());
    } catch (Exception e) {
        LOGGER.warn("Authentication process failed ! No cookie was generated", e);
    }
}

From source file:org.trustedanalytics.user.current.CurrentUserController.java

@ApiOperation(value = "Returns current user.", notes = "Privilege level: Any consumer of this endpoint must have a valid access token")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = UserModel.class),
        @ApiResponse(code = 500, message = "Internal server error, e.g. error connecting to CloudController") })
@RequestMapping(method = RequestMethod.GET)
public UserModel getUser(Authentication auth) {
    UserModel user = new UserModel();
    user.setEmail(auth.getName());
    user.setRole(detailsFinder.getRole(auth));

    return user;// ww w .  j  a va 2  s .c om
}

From source file:com.companyname.services.PlatUserAuthenticationCache.java

private String getHashKey(Authentication authentication) {
    String auth = authentication.getName() + ":" + (String) authentication.getCredentials();
    byte[] encodedAuth = Base64.encode(auth.getBytes(Charset.forName("US-ASCII")));
    String key = "Key-" + new String(encodedAuth);

    logger.info("authentication caching operation uses a key = " + key);

    return key;/* w ww.j a va2 s .  c  o m*/
}