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

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

Introduction

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

Prototype

boolean isAuthenticated();

Source Link

Document

Used to indicate to AbstractSecurityInterceptor whether it should present the authentication token to the AuthenticationManager.

Usage

From source file:org.opendatakit.security.spring.UserServiceImpl.java

private boolean isAnonymousUser(Authentication auth) {
    if (auth == null) {
        throw new NullPointerException("Unexpected null pointer from authentication retrieval");
    } else if (!auth.isAuthenticated()) {
        throw new IllegalStateException(
                "Unexpected unauthenticated user from authentication retrieval (expect anonymous authentication)");
    } else if ((auth.getPrincipal() instanceof String)
            && ((String) auth.getPrincipal()).equals("anonymousUser")) {
        return true;
    } else {//from w  w  w  .j  av a2 s .  co m
        return false;
    }
}

From source file:security.LoginPasswordSecurityService.java

/**
 * {@inheritDoc}//from   w  ww  .ja v a  2 s  .c  om
 */

public boolean authenticate(String username, String password) {

    Authentication aut = new UsernamePasswordAuthenticationToken(username, password);
    try {
        aut = authenticationManager.authenticate(aut);
        SecurityContextHolder.getContext().setAuthentication(aut);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return aut.isAuthenticated();
}

From source file:org.artifactory.ui.rest.service.admin.security.auth.login.LoginService.java

/**
 * update session and DB with authentication data
 * @param artifactoryContext - artifactory web context
 * @param userName - login user name/*from  w w  w . j av  a2s .c o  m*/
 * @param authenticationToken - login authentication token
 * @param authentication - spring authentication
 * @param artifactoryRestRequest - encapsulate data related to request
 * @return if true  data save successfully
 */
private boolean updateSessionAndDB(ArtifactoryContext artifactoryContext, String userName,
        UsernamePasswordAuthenticationToken authenticationToken, Authentication authentication,
        ArtifactoryRestRequest artifactoryRestRequest) {
    boolean isAuthenticate = true;
    try {
        if (authentication.isAuthenticated()) {
            SecurityContext securityContext = SecurityContextHolder.getContext();
            securityContext.setAuthentication(authenticationToken);
            setLoginDataToSessionAndDB(securityContext, userName, artifactoryContext, authentication,
                    artifactoryRestRequest.getServletRequest());
        }
    } catch (AuthenticationException e) {
        isAuthenticate = false;
        AccessLogger.loginDenied(authenticationToken);
        if (log.isDebugEnabled()) {
            log.debug("Failed to authenticate " + userName, e);
        }
    }
    return isAuthenticate;
}

From source file:org.vaadin.spring.security.AbstractVaadinSecurity.java

@Override
public boolean isFullyAuthenticated() {
    final Authentication authentication = getAuthentication();
    return authentication != null && !(authentication instanceof AnonymousAuthenticationToken)
            && !(authentication instanceof RememberMeAuthenticationToken) && authentication.isAuthenticated();
}

From source file:de.blizzy.documentr.access.DocumentrPermissionEvaluator.java

private boolean hasRoleOnBranch(Authentication authentication, String projectName, String branchName,
        String roleName) throws IOException {

    if (authentication.isAuthenticated()) {
        List<RoleGrantedAuthority> authorities = userStore.getUserAuthorities(authentication.getName());
        for (RoleGrantedAuthority rga : authorities) {
            if (rga.getRoleName().equals(roleName)) {
                GrantedAuthorityTarget target = rga.getTarget();
                switch (target.getType()) {
                case APPLICATION:
                    return true;
                case PROJECT:
                    if (target.getTargetId().equals(projectName)) {
                        return true;
                    }//from   ww  w. j a  v a  2  s.  co  m
                    break;
                case BRANCH:
                    if (target.getTargetId().equals(projectName + "/" + branchName)) { //$NON-NLS-1$
                        return true;
                    }
                    break;
                }
            }
        }
    }
    return false;
}

From source file:nl.surfnet.coin.api.oauth.ConfigurableTokenServicesUserApprovalHandler.java

@Override
public boolean isApproved(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
    String clientId = authorizationRequest.getClientId();
    ClientDetails clientDetails = clientDetailsService.loadClientByClientId(clientId);
    if (clientDetails instanceof OpenConextClientDetails) {
        ClientMetaData clientMetaData = ((OpenConextClientDetails) clientDetails).getClientMetaData();
        if (!clientMetaData.isConsentRequired()) {
            return userAuthentication.isAuthenticated();
        }/*from   w  ww  .  j  a  v  a2 s.  c o  m*/
    }
    return super.isApproved(authorizationRequest, userAuthentication);
}

From source file:org.vaadin.spring.security.GenericVaadinSecurity.java

/**
 * {@inheritDoc}/*w w  w  .j  a v  a 2 s  .  c  o  m*/
 */
@Override
public boolean isAuthenticated() {
    final Authentication authentication = getAuthentication();
    return (authentication != null && authentication.isAuthenticated());
}

From source file:jedai.business.JAuthorizationService.java

/**
 * authenticates a client// w w w . j a v  a 2 s . com
 * 
 * @param authvo
 *          AuthVO the object being authenticated
 * 
 * @return val
 *          Boolean the returned boolean value
 */
public boolean authenticate(AuthVO authvo) {
    Authentication token = new UsernamePasswordAuthenticationToken(authvo.getUserName(), authvo.getPassword());

    try {
        token = authenticationManager.authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(token);

    } catch (AuthenticationException ex) {
        // report error
        System.out.println("ERROR: " + ex);
    }

    if (!token.isAuthenticated()) {
        return false;
    }

    return true;
}

From source file:org.vaadin.spring.security.GenericVaadinSecurity.java

/**
 * {@inheritDoc}//  w w  w .  j  a v a 2s.  c om
 */
@Override
public boolean hasAuthority(String authority) {
    final Authentication authentication = getAuthentication();
    if (authentication == null || !authentication.isAuthenticated()) {
        return false;
    }

    for (GrantedAuthority grantedAuthority : authentication.getAuthorities()) {
        if (authority.equals(grantedAuthority.getAuthority())) {
            return true;
        }
    }

    return false;
}