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:com.github.javarch.jsf.tags.security.SpringSecurityELLibrary.java

/**
 * Method checks if the user is anonymous.
 * Returns <code>true</code> if the user <b>is</b> anonymous.
 * Returns <code>false</code> if the user is <b>not</b> anonymous.
 * @return/*from   w  w w  .  j  a v a 2 s  . c om*/
 */
public static boolean isAnonymous() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
        return true;
    }
    return !authentication.isAuthenticated();
}

From source file:br.com.suricattus.surispring.spring.security.util.SecurityUtil.java

/**
 * Method checks if the user is authenticated. Returns <code>true</code> if
 * the user is <b>not</b> anonymous. Returns <code>false</code> if the user
 * <b>is</b> anonymous.//w  w w.j  ava2s. com
 * 
 * @return
 */
public static boolean isAuthenticated() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
        return false;
    }
    return authentication.isAuthenticated();
}

From source file:com.epam.ta.reportportal.auth.permissions.AssignedToProjectPermission.java

/**
 * Check whether user assigned to project<br>
 * Or user is ADMIN who is GOD of ReportPortal
 *///from  www.  ja v a 2 s.c  om
@Override
public boolean isAllowed(Authentication authentication, Object projectName) {
    return authentication.isAuthenticated()
            && projectRepository.get().isAssignedToProject((String) projectName, authentication.getName());
}

From source file:oauth2.authentication.MyAuthenticationSuccessHandler.java

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
        Authentication authentication) throws ServletException, IOException {

    if (authentication.isAuthenticated()) {
        for (Map.Entry<String, String> entry : redirectsByAuthority.entrySet()) {
            if (authentication.getAuthorities().stream()
                    .anyMatch(authority -> Objects.equal(entry.getKey(), authority.getAuthority()))) {
                getRedirectStrategy().sendRedirect(request, response, entry.getValue());
                return;
            }//from   w  ww .j  ava  2 s. c o m
        }
    }
    super.onAuthenticationSuccess(request, response, authentication);
}

From source file:com.epam.ta.reportportal.auth.permissions.BaseProjectPermission.java

/**
 * Validates project exists and user assigned to project. After that
 * delegates permission check to subclass
 *///w  w  w . j a v  a  2 s . c  o  m
@Override
public boolean isAllowed(Authentication authentication, Object projectName) {
    if (!authentication.isAuthenticated()) {
        return false;
    }

    String project = (String) projectName;
    Project p = projectRepository.get().findOne(project);
    BusinessRule.expect(p, Predicates.notNull()).verify(ErrorType.PROJECT_NOT_FOUND, project);

    BusinessRule.expect(p.getUsers(), Preconditions.containsKey(authentication.getName()))
            .verify(ErrorType.ACCESS_DENIED);
    return checkAllowed(authentication, p);
}

From source file:com.gsr.myschool.server.security.AdminAuthenticationServiceImpl.java

@Override
public Boolean authenticate(String username, String password) {
    Authentication authentication = new UsernamePasswordAuthenticationToken(username, password);
    try {/*from  w w  w. ja v  a 2 s  .c o m*/
        Authentication authenticated = authenticationManager.authenticate(authentication);
        return authenticated.isAuthenticated();
    } catch (Exception e) {
        return false;
    }
}

From source file:org.trustedanalytics.metadata.parser.AuthenticationDisabler.java

private String authenticateEverything(InvocationOnMock invocation) throws IOException, ServletException {

    ServletRequest req = (ServletRequest) invocation.getArguments()[0];
    ServletResponse res = (ServletResponse) invocation.getArguments()[1];
    FilterChain filterChain = (FilterChain) invocation.getArguments()[2];

    Authentication authentication = mock(Authentication.class);
    when(authentication.isAuthenticated()).thenReturn(true);
    SecurityContextHolder.getContext().setAuthentication(authentication);

    filterChain.doFilter(req, res);/*from   w  w  w.j  a v a 2 s.c o  m*/

    return "method called";
}

From source file:fr.mycellar.interfaces.web.security.CurrentUserService.java

/**
 * @return//from   ww  w .  j  av a 2s.  c om
 */
public String getCurrentUserEmail() {
    String email = null;
    SecurityContext context = SecurityContextHolder.getContext();
    if (context != null) {
        Authentication auth = context.getAuthentication();
        if ((auth != null) && auth.isAuthenticated() && !(auth instanceof AnonymousAuthenticationToken)) {
            email = auth.getName();
        }
    }
    return email;
}

From source file:org.moserp.common.security.SpringSecurityAuditorAware.java

public String getCurrentAuditor() {
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (authentication == null || !authentication.isAuthenticated()) {
        return null;
    }//w  w  w . j a  v a  2s  . com
    if (authentication.getPrincipal() instanceof String) {
        return ((String) authentication.getPrincipal());
    }
    if (authentication.getPrincipal() instanceof User) {
        return ((User) authentication.getPrincipal()).getUsername();
    }
    return authentication.getPrincipal().toString();
}

From source file:org.parancoe.plugins.securityevolution.AuthenticationManagerTest.java

/**
 * Test method for//from   w  w  w.j av  a  2 s.  co m
 * {@link org.springframework.security.authentication.AuthenticationManager#authenticate(org.springframework.security.core.Authentication)}.
 */
public void testAuthenticate() {
    authentication = new UsernamePasswordAuthenticationToken("parancoe", "parancoe");
    Authentication result = authenticationManager.authenticate(authentication);
    assertTrue(result.isAuthenticated());
}