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.cruz.sec.config.ItemIdBasedAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws IOException, ServletException {
    User user = (User) authentication.getPrincipal();
    //        request.getSession().setAttribute("TOOLS", obtenerHerrDptosByUser.getDptoByUsername(user.getUsername()));
    //        for (Iterator iterator = user.getAuthorities().iterator(); iterator.hasNext();) {
    //            String autority = iterator.next().toString();
    //Obtengo el contexto de la direccin IP y la agrego al objeto session
    //            request.getSession().setAttribute("ENVIRONMENT", request.getParameter("environment"));
    //            if(autority.equalsIgnoreCase("ROLE_COORDINADOR") || autority.equalsIgnoreCase("ROLE_TECNICO")){
    //Agrego el nmero de mensajes nos ledos a un atributo sesin.
    //                String rol = (autority.equalsIgnoreCase("ROLE_COORDINADOR")? "ROLE_COORDINADOR": "ROLE_TECNICO");
    //                request.getSession().setAttribute("NUM_MSJ_N_L", mensajesBandejaDaoImp.getMensajesNoLeidosUser(user.getUsername(), rol));
    //                break;
    //            }
    //        }/*from  w  w  w.j  a v a 2  s .  co m*/
    super.onAuthenticationSuccess(request, response, authentication);
    //            String redirectUrl = request.getContextPath() + "/";
    System.out.println("-----------------------------INICIO DE SESIN EXITOSO-----------------------------");
    //            System.out.println("informacin: " + authentication.getDetails().toString());
    //            response.sendRedirect(redirectUrl);
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaUserTokenConverter.java

@Override
public Map<String, ?> convertUserAuthentication(Authentication authentication) {
    Map<String, Object> response = new LinkedHashMap<String, Object>();
    if (authentication.getPrincipal() instanceof UaaPrincipal) {
        UaaPrincipal principal = (UaaPrincipal) authentication.getPrincipal();
        response.put(USER_ID, principal.getId());
        response.put(USER_NAME, principal.getName());
        response.put(EMAIL, principal.getEmail());
    }//from   ww w. j  a v a  2s .  c  o m
    return response;
}

From source file:org.fon.documentmanagementsystem.controllers.HomeController.java

@RequestMapping(method = RequestMethod.GET)
public ModelAndView home(Authentication authentication) {
    //return new ModelAndView("admin_home");
    UserDto userDto = (UserDto) authentication.getPrincipal();
    switch (userDto.getRola().getNazivRole()) {
    case "ADMIN":
        return new ModelAndView("admin_home");
    case "USER":
        return new ModelAndView("user_home");
    case "SUPERADMIN":
        return new ModelAndView("superadmin_home");
    default:/*from  ww  w  .  j a  va  2 s .co m*/
        return new ModelAndView("login");
    }
}

From source file:org.appverse.web.framework.backend.api.helpers.security.PreAuthUserDetailsService.java

@SuppressWarnings("unchecked")
@Override//from w w  w.ja v a2 s. c om
public UserDetails loadUserDetails(Authentication token) throws UsernameNotFoundException {

    String username = (String) token.getPrincipal();
    List<String> externalAuthorities = null;
    if (token.getDetails() instanceof String) {
        externalAuthorities = new ArrayList<String>();
        externalAuthorities.add((String) token.getDetails());
    } else {
        externalAuthorities = (List<String>) token.getDetails();
    }

    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

    for (String externalAuthority : externalAuthorities) {
        GrantedAuthority authority = new SimpleGrantedAuthority(externalAuthority);
        authorities.add(authority);
    }

    User user = new User(username, "", true, true, true, true, authorities);
    return user;
}

From source file:bookpub.security.AuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {
    User user = (User) authentication.getPrincipal();
    logger.info(user.getUsername() + " signed in.");

    RequestCache requestCache = new HttpSessionRequestCache();
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    if (savedRequest != null) {
        super.onAuthenticationSuccess(request, response, authentication);
        return;/*from  w ww . ja v  a 2  s  . c om*/
    }

    if (request.getParameter("mobile") != null)
        objectMapper.writeValue(response.getWriter(), new ServiceResponse());
    else
        getRedirectStrategy().sendRedirect(request, response, "/");
}

From source file:de.msg.security.CustomPermissionEvaluator.java

@Override
public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
    return authorize((User) authentication.getPrincipal(), (String) targetDomainObject);
}

From source file:edu.csula.squirrels.security.AuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {
    User user = (User) authentication.getPrincipal();
    logger.info(user.getUsername() + " signed in.");

    RequestCache requestCache = new HttpSessionRequestCache();
    SavedRequest savedRequest = requestCache.getRequest(request, response);
    if (savedRequest != null) {
        super.onAuthenticationSuccess(request, response, authentication);
        return;// w w w  .  ja  v  a  2  s  .  com
    }

    if (request.getParameter("mobile") != null)
        objectMapper.writeValue(response.getWriter(), new ServiceResponse("loggedIn"));
    else
        getRedirectStrategy().sendRedirect(request, response, "/");
}

From source file:com.cosw.productsmaster.authsec.UserAuthenticationProvider.java

public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String user = authentication.getPrincipal().toString();
    String pwd = authentication.getCredentials().toString();

    //PUT Auth Bean here

    boolean result = user.equals("myuser") && pwd.equals("mypassword");
    System.out.println("hola" + result);
    if (result) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        UserAuthenticationToken auth = new UserAuthenticationToken(authentication.getPrincipal(),
                authentication.getCredentials(), grantedAuthorities);

        return auth;
    } else {/*  w w  w  .  j a va  2s.  c o m*/
        throw new BadCredentialsException("Bad User Credentials.");
    }

}

From source file:com.javiermoreno.springboot.rest.CredentialsController.java

@RequestMapping(value = "/token", method = RequestMethod.GET)
@ApiOperation(value = "Generacin token", notes = "Retorna un token en Base64 con una duracin marcada por TOKEN_TTL.")
public Token createNewAuthToken(HttpServletRequest request) {
    long ttl = System.currentTimeMillis() + 1000 * TOKEN_TTL;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    Object principal = authentication.getPrincipal();
    String username = principal instanceof UserDetails ? ((UserDetails) principal).getUsername()
            : principal.toString();/*from  w  w  w  .ja  v  a  2  s. c  o m*/
    String ip = request.getHeader("X-Forwarded-For");
    if (ip == null) {
        ip = request.getRemoteAddr();
    }
    Token token = new Token(cryptoService, username, ip, ttl);

    return token;
}

From source file:com.mycompany.apps.oauth2.authentication.security.CustomUserAuthenticationProvider.java

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

    if (authentication.getPrincipal().equals("user") && authentication.getCredentials().equals("user")) {

        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        CustomUserPasswordAuthenticationToken auth = new CustomUserPasswordAuthenticationToken(
                authentication.getPrincipal(), authentication.getCredentials(), grantedAuthorities);

        return auth;

    } else if (authentication.getPrincipal().equals("admin")
            && authentication.getCredentials().equals("admin")) {

        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        CustomUserPasswordAuthenticationToken auth = new CustomUserPasswordAuthenticationToken(
                authentication.getPrincipal(), authentication.getCredentials(), grantedAuthorities);

        return auth;

    } else if (authentication.getPrincipal().equals("user1")
            && authentication.getCredentials().equals("user1")) {

        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        CustomUserPasswordAuthenticationToken auth = new CustomUserPasswordAuthenticationToken(
                authentication.getPrincipal(), authentication.getCredentials(), grantedAuthorities);
        return auth;

    } else {//from w  w w.  j  a v a 2  s  . c o m
        throw new BadCredentialsException("Bad User Credentials.");
    }
}