Example usage for org.springframework.security.core.userdetails UserDetails toString

List of usage examples for org.springframework.security.core.userdetails UserDetails toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:com.jevontech.wabl.security.AuthenticationTokenFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    //log.debug("doFilter");

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String authToken = httpRequest.getHeader(this.tokenHeader);
    String username = this.tokenUtils.getUsernameFromToken(authToken);

    log.debug("doFilter: this.tokenHeader=" + this.tokenHeader);

    log.debug("doFilter: authToken=" + authToken + " , username=" + username);

    if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
        UserDetails userDetails = this.userDetailsService.loadUserByUsername(username);

        log.debug("doFilter: userDetails=" + userDetails.toString());
        if (this.tokenUtils.validateToken(authToken, userDetails)) {
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    userDetails, null, userDetails.getAuthorities());
            authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpRequest));
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }//ww  w. ja  v  a  2 s .c o m
    }

    chain.doFilter(request, response);
}

From source file:com.gs.config.SessionExpirationFilter.java

@Override
public void onApplicationEvent(HttpSessionDestroyedEvent event) {
    List<org.springframework.security.core.context.SecurityContext> lstSecurityContext = event
            .getSecurityContexts();// w ww. ja  v  a2 s.  co m
    UserDetails ud;
    for (org.springframework.security.core.context.SecurityContext securityContext : lstSecurityContext) {
        ud = (UserDetails) securityContext.getAuthentication().getPrincipal();
        System.out.println("SESIN DESTRUIDA: " + ud.toString());

        HttpSession httpSession = event.getSession();
        //            long createdTime = httpSession.getCreationTime();
        long lastAccessedTime = httpSession.getLastAccessedTime();
        int maxInactiveTime = httpSession.getMaxInactiveInterval();
        long currentTime = System.currentTimeMillis();

        //            System.out.println("Session Id :"+httpSession.getId() );
        //            System.out.println("Created Time : " + createdTime);
        //            System.out.println("Last Accessed Time : " + lastAccessedTime);
        //            System.out.println("Current Time : " + currentTime);
        boolean possibleSessionTimeout = (currentTime - lastAccessedTime) >= (maxInactiveTime * 1000);
        if (possibleSessionTimeout) {
            //Se realiza la peticin al Node JS
            //                requestNodeJS.sendRequestWithUsernameAndMethod(ud.getUsername(), "/session-close");
        }
        //            System.out.println("Possbile Timeout : " + possibleSessionTimeout);
    }
}

From source file:com.cruz.sec.config.SessionExpirationFilter.java

@Override
public void onApplicationEvent(HttpSessionDestroyedEvent event) {
    List<org.springframework.security.core.context.SecurityContext> lstSecurityContext = event
            .getSecurityContexts();//from ww w.  j  ava 2  s .com
    UserDetails ud;
    for (org.springframework.security.core.context.SecurityContext securityContext : lstSecurityContext) {
        ud = (UserDetails) securityContext.getAuthentication().getPrincipal();
        System.out.println("OBJECTO SESION DESTRUIDO: " + ud.toString());

        HttpSession httpSession = event.getSession();
        //            long createdTime = httpSession.getCreationTime();
        long lastAccessedTime = httpSession.getLastAccessedTime();
        int maxInactiveTime = httpSession.getMaxInactiveInterval();
        long currentTime = System.currentTimeMillis();

        //            System.out.println("Session Id :"+httpSession.getId() );
        //            System.out.println("Created Time : " + createdTime);
        //            System.out.println("Last Accessed Time : " + lastAccessedTime);
        //            System.out.println("Current Time : " + currentTime);
        boolean possibleSessionTimeout = (currentTime - lastAccessedTime) >= (maxInactiveTime * 1000);
        if (possibleSessionTimeout) {
            System.out.println("SESIN CERRADA POR INACTIVIDAD");
            //Se realiza la peticin al Node JS
            //                requestNodeJS.sendRequestWithUsernameAndMethod(ud.getUsername(), "/session-close");
        }
    }
}

From source file:cn.org.once.cstack.aspects.SecurityAnnotationAspect.java

@Before("@annotation(CloudUnitSecurable) && args(applicationName)")
public void verifyRelationBetweenUserAndApplication(JoinPoint joinPoint, String applicationName) {

    UserDetails principal = null;
    JsonInput jsonInput = null;// www  . j a  va2 s  . c  o m
    try {
        principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userService.findByLogin(principal.getUsername());

        if (joinPoint.getArgs() == null) {
            logger.error("Error on annotation aspect : " + joinPoint.getStaticPart().getSignature());
        } else {
            if (joinPoint.getArgs()[0] instanceof JsonInput) {
                jsonInput = (JsonInput) joinPoint.getArgs()[0];
                applicationName = jsonInput.getApplicationName();
            }
            Application application = applicationService.findByNameAndUser(user, applicationName);
            if (application == null) {
                throw new IllegalArgumentException(
                        "This application does not exist on this account : " + applicationName + "," + user);
            }
        }

    } catch (ServiceException | CheckException e) {
        logger.error(principal.toString() + ", " + jsonInput, e);
    }

}

From source file:fr.treeptik.cloudunit.aspects.SecurityAnnotationAspect.java

@Before("@annotation(fr.treeptik.cloudunit.aspects.CloudUnitSecurable)")
public void verifyRelationBetweenUserAndApplication(JoinPoint joinPoint) {

    UserDetails principal = null;
    JsonInput jsonInput = null;/*from w  ww  .j  a  va2 s  .  c o  m*/
    try {
        principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        User user = userService.findByLogin(principal.getUsername());

        if (joinPoint.getArgs() == null) {
            logger.error("Error on annotation aspect : " + joinPoint.getStaticPart().getSignature());
        } else {
            String applicationName = null;
            if (joinPoint.getArgs()[0] instanceof JsonInput) {
                jsonInput = (JsonInput) joinPoint.getArgs()[0];
                applicationName = jsonInput.getApplicationName();
            } else {
                // The first parameter must be always be the applicationName
                applicationName = (String) joinPoint.getArgs()[0];
            }
            Application application = applicationService.findByNameAndUser(user, applicationName);
            if (application == null) {
                throw new IllegalArgumentException("This application does not exist on this account");
            }
        }

    } catch (ServiceException | CheckException e) {
        logger.error(principal.toString() + ", " + jsonInput, e);
    }

}

From source file:ph.fingra.statisticsweb.security.FingraphAnthenticationProvider.java

@SuppressWarnings("unused")
@Override/*w w w.  j a  va  2  s. c  o m*/
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {

    // admin.properties values
    logger.debug("[adminEmail] {}", adminEmail);
    logger.debug("[adminName] {}", adminName);
    logger.debug("[adminPassword] {}", adminPassword);

    UserDetails loadedUser = null;
    try {
        logger.debug("retrieveUser {}", username);

        Member member = null;
        if (username.equals(adminEmail)) {
            member = new Member();
            member.setEmail(adminEmail);
            member.setName(adminName);
            member.setPassword(adminPassword);
            member.setStatus(MemberStatus.ACTIVE.getValue());
            member.setJoinstatus(MemberJoinstatus.APPROVAL.getValue());
            member.setRole(MemberRole.ROLE_ADMIN.getValue());
        } else {
            member = memberService.get(username);
        }
        if (member == null) {
            throw new UsernameNotFoundException("Not found user id");
        }

        // lastlogin update
        if (member.getRole() == MemberRole.ROLE_USER.getValue()) {
            memberService.updateMemberLastLoginTime(member);
        }

        if (member.getRole() == MemberRole.ROLE_ADMIN.getValue()) {
            //logger.debug("passwordEncoder {}", adminPasswordEncoder);
            loadedUser = new FingraphUser(member, adminPasswordEncoder);
        } else {
            loadedUser = new FingraphUser(member);
        }
        logger.debug("userDetail is {}", loadedUser.toString());
    } catch (UsernameNotFoundException notFound) {
        if (authentication.getCredentials() != null) {
            String presentedPassword = authentication.getCredentials().toString();
            passwordEncoder.isPasswordValid(userNotFoundEncodedPassword, presentedPassword, null);
        }
        throw notFound;
    } catch (Exception repositoryProblem) {
        throw new AuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem);
    }

    if (loadedUser == null) {
        throw new AuthenticationServiceException(
                "UserDetailsService returned null, which is an interface contract violation");
    }

    return loadedUser;
}