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:de.uni_koeln.spinfo.maalr.login.LoginManager.java

public boolean loggedIn(Authentication user) {
    if (user == null)
        return false;
    // TODO: Find better way to identify anonymous user...
    return user.isAuthenticated() && !anonymous.equals(user.getName());

}

From source file:com.devicehive.auth.rest.HttpAuthenticationFilter.java

private void tryAuthenticate(Authentication requestAuth) {
    Authentication authentication = authenticationManager.authenticate(requestAuth);
    if (authentication == null || !authentication.isAuthenticated()) {
        throw new InternalAuthenticationServiceException(
                "Unable to authenticate user with provided credetials");
    }/*  ww w  .ja  v a  2 s . c  o m*/
    logger.debug("Successfully authenticated");
    SecurityContextHolder.getContext().setAuthentication(authentication);
}

From source file:org.cloudfoundry.identity.uaa.login.RemoteUaaAuthenticationManagerTests.java

@Test
public void testAuthenticate() throws Exception {
    responseHeaders.setLocation(new URI("https://uaa.cloudfoundry.com/"));
    Map<String, String> response = new HashMap<String, String>();
    response.put("username", "marissa");
    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> expectedResponse = new ResponseEntity<Map>(response, responseHeaders, HttpStatus.OK);
    when(restTemplate.exchange(endsWith("/authenticate"), eq(HttpMethod.POST), any(HttpEntity.class),
            eq(Map.class))).thenReturn(expectedResponse);
    Authentication result = authenticationManager
            .authenticate(new UsernamePasswordAuthenticationToken("marissa", "foo"));
    assertEquals("marissa", result.getName());
    assertTrue(result.isAuthenticated());
}

From source file:uk.co.threeonefour.ifictionary.web.user.service.DaoUserService.java

public uk.co.threeonefour.ifictionary.web.user.model.User getLoggedInUser() {

    // TODO use session
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth != null) {
        String username = auth.getName();
        if (auth.isAuthenticated() && username != null && !username.equals("anonymousUser")) {
            org.springframework.security.core.userdetails.User userDetails = (org.springframework.security.core.userdetails.User) auth
                    .getPrincipal();//from www . java2s  .  c o m
            User user = userDao.findUser(userDetails.getUsername());

            List<Role> roles = new ArrayList<Role>();
            for (GrantedAuthority authority : userDetails.getAuthorities()) {
                roles.add(Role.valueOf(authority.getAuthority()));
            }

            user.setRoles(roles);

            return user;
        }
    }
    return null;
}

From source file:mum.edu.sec.AuthenticationEventListener.java

@Override
public void onApplicationEvent(AbstractAuthenticationEvent authenticationEvent) {
    if (authenticationEvent instanceof InteractiveAuthenticationSuccessEvent) {
        // ignores to prevent duplicate logging with AuthenticationSuccessEvent
        return;/*from  ww  w  .j  av  a 2  s. co  m*/
    }
    Authentication authentication = authenticationEvent.getAuthentication();
    String auditMessage = "Login attempt with username: " + authentication.getName() + " pass: '"
            + authentication.getCredentials() + "'" + "\t\tSuccess: " + authentication.isAuthenticated();
    System.err.println(auditMessage);
    //logger.info(auditMessage);
}

From source file:com.netflix.spinnaker.fiat.shared.FiatPermissionEvaluator.java

private String getUsername(Authentication authentication) {
    String username = "anonymous";
    if (authentication.isAuthenticated() && authentication.getPrincipal() != null) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof User) {
            username = ((User) principal).getUsername();
        } else if (StringUtils.isNotEmpty(principal.toString())) {
            username = principal.toString();
        }/*from  ww  w.j  a  v a  2 s .co  m*/
    }
    return username;
}

From source file:it.scoppelletti.programmerpower.security.DefaultUserManager.java

@Transactional(readOnly = true)
public User loadLoggedUser() {
    User principal;/*w  w  w  .j a  v  a2  s  .  c  o m*/
    Authentication auth;
    SecurityContext secCtx = SecurityContextHolder.getContext();

    auth = secCtx.getAuthentication();
    if (auth == null || !auth.isAuthenticated() || auth instanceof AnonymousAuthenticationToken) {
        return null;
    }

    principal = (User) auth.getPrincipal();
    return loadUser(principal.getId());
}

From source file:org.glassmaker.spring.oauth.OAuth2AuthenticationProcessingFilter.java

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException, IOException, ServletException {

    // If the request is already authenticated we can assume that this
    // filter is not needed

    Authentication authentication = getAuthentication(request);
    if (authentication != null && authentication.isAuthenticated()) {
        return authentication;
    }//from  www.  ja va 2 s.c o  m

    if (response.isCommitted())
        logger.info("RESPONSE IS COMMITTED!");

    return createAuthentication(request);
}

From source file:org.openinfinity.sso.common.ss.sp.filters.PreAuthenticatedTokenAuthenticationFilter.java

private boolean nullOrUnauthorized(org.springframework.security.core.Authentication springAuthentication) {
    return springAuthentication == null || !springAuthentication.isAuthenticated();
}

From source file:info.raack.appliancelabeler.security.HttpSessionAndDatabaseOAuthRemeberMeServices.java

public Map<String, OAuthConsumerToken> loadRememberedTokens(HttpServletRequest request,
        HttpServletResponse response) {//from  www  . j  a  va  2 s.co  m
    // check httpsessionrememberme services first

    Map<String, OAuthConsumerToken> tokens = super.loadRememberedTokens(request, response);

    if (tokens != null) {
        logger.debug("Found existing oauth tokens in session");
        return tokens;
    } else {
        // haven't found any tokens yet - look in the database

        // ASSUMPTIONS - remember tokens is called with every token request (spring security oauth code), so any tokens in the session will also be in the database
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        String userId = null;
        if (auth != null && auth.isAuthenticated()) {
            if (auth instanceof RememberMeAuthenticationToken) {
                Object principal = auth.getPrincipal();
                if (principal instanceof OAuthUserDetails) {
                    logger.debug("Found existing oauth tokens in remember me persistence");
                    return ((OAuthUserDetails) principal).getOAuthTokens();
                } else if (principal instanceof String) {
                    logger.debug(
                            "Found user id in remember me persistence; grabbing oauth tokens from database");
                    return dataService.getOAuthTokensForUserId((String) principal);
                }
            } else if (auth instanceof OAuthAutomaticAuthenticationToken) {
                // user is already logged in via spring security
                logger.debug(
                        "Found user id in oauth automatic login token; grabbing oauth tokens from database");
                return dataService.getOAuthTokensForUserId((String) auth.getPrincipal());
            }
        }
        return null;
    }
}