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

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

Introduction

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

Prototype

Collection<? extends GrantedAuthority> getAuthorities();

Source Link

Document

Set by an AuthenticationManager to indicate the authorities that the principal has been granted.

Usage

From source file:org.ff4j.security.SpringSecurityAuthorisationManager.java

/** {@inheritDoc} */
public Set<String> getCurrentUserPermissions() {
    Set<String> listOfRoles = new LinkedHashSet<String>();
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (!(auth instanceof AnonymousAuthenticationToken)) {
        for (GrantedAuthority grantedAuthority : auth.getAuthorities()) {
            listOfRoles.add(grantedAuthority.getAuthority());
        }//w  ww.  j a  v a2 s  .  c  o  m
    }
    return listOfRoles;
}

From source file:com.datapine.aop.LoggingAspect.java

/**
 * Logs a successful attempt to login by a user.
 * @param join Method//  w  w w .  j  av a2s  . c om
 *  SimpleUrlAuthenticationSuccessHandler#onAuthenticationSuccess(...).
 * @checkstyle LineLengthCheck (2 lines)
 */
@After("execution(* org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler.onAuthenticationSuccess(..))")
public final void loginSucceeded(final JoinPoint join) {
    final Authentication auth = (Authentication) join.getArgs()[2];
    Logger.info(this, "Login of the user '%s' succeeds with authorities %s", auth.getName(),
            auth.getAuthorities());
}

From source file:sequrity.SecurityConfig.java

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/faces/javax.faces.resource/**").permitAll().anyRequest()
            .authenticated().and().formLogin().loginPage("/faces/prijava.xhtml").permitAll()
            .failureUrl("/faces/prijava.xhtml?error").loginProcessingUrl("/perform_login")
            .successHandler(new AuthenticationSuccessHandler() {

                @Override/*  w w  w. j av  a2  s.co m*/
                public void onAuthenticationSuccess(HttpServletRequest hsr, HttpServletResponse hsr1,
                        Authentication a) throws IOException, ServletException {
                    RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
                    if (a.getAuthorities().contains(new SimpleGrantedAuthority(LoginService.ROLE_PROFESOR))) {
                        redirectStrategy.sendRedirect(hsr, hsr1, "/faces/schedule_profesor.xhtml");
                    } else {
                        redirectStrategy.sendRedirect(hsr, hsr1, "/faces/schedule.xhtml");
                    }
                }
            })
            //                .defaultSuccessUrl("/faces/schedule.xhtml", true)
            .usernameParameter("username").passwordParameter("password").and().logout()
            .logoutSuccessUrl("/faces/prijava.xhtml");

}

From source file:com.olegchir.flussonic_userlinks.auth.AuthSuccessHandler.java

/** Builds the target URL according to the logic defined in the main class Javadoc. */
protected String determineTargetUrl(Authentication authentication) {
    boolean isUser = false;
    boolean isAdmin = false;
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    for (GrantedAuthority grantedAuthority : authorities) {
        if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
            isUser = true;/*from  w w w  .j  av  a2 s .  c o  m*/
            break;
        } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
            isAdmin = true;
            break;
        }
    }

    if (isUser) {
        return "/dashboard";
    } else if (isAdmin) {
        return "/admin_dashboard";
    } else {
        throw new IllegalStateException();
    }
}

From source file:com.traffitruck.WebSecurityConfig.java

@Bean
public AuthenticationSuccessHandler successHandler() {
    SavedRequestAwareAuthenticationSuccessHandler handler = new SavedRequestAwareAuthenticationSuccessHandler() {
        @Override//from  w  w  w .jav a 2 s .  com
        protected void handle(HttpServletRequest request, HttpServletResponse response,
                Authentication authentication) throws IOException, ServletException {
            if (resetPasswordFlow(authentication.getAuthorities())) {
                try {
                    getRedirectStrategy().sendRedirect(request, response, "/resetPassword");
                    return;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            String url = Role.valueOf(authentication.getAuthorities().iterator().next().getAuthority())
                    .getLandingUrl();
            getRedirectStrategy().sendRedirect(request, response, url);
        }

        private boolean resetPasswordFlow(Collection<? extends GrantedAuthority> authorities) {
            for (GrantedAuthority grantedAuthority : authorities) {
                if (grantedAuthority.getAuthority().startsWith("resetPassword-"))
                    return true;
            }
            return false;
        }
    };
    return handler;
}

From source file:org.mitre.oauth2.model.SavedUserAuthentication.java

/**
 * Create a Saved Auth from an existing Auth token
 *//*from www .ja  va  2s.  co m*/
public SavedUserAuthentication(Authentication src) {
    setName(src.getName());
    setAuthorities(src.getAuthorities());
    setAuthenticated(src.isAuthenticated());

    if (src instanceof SavedUserAuthentication) {
        // if we're copying in a saved auth, carry over the original class name
        setSourceClass(((SavedUserAuthentication) src).getSourceClass());
    } else {
        setSourceClass(src.getClass().getName());
    }
}

From source file:org.taverna.server.master.identity.AuthorityDerivedIDMapper.java

@Override
public String getUsernameForPrincipal(UsernamePrincipal user) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    if (auth == null || !auth.isAuthenticated())
        return null;
    for (GrantedAuthority authority : auth.getAuthorities()) {
        String token = authority.getAuthority();
        if (token == null)
            continue;
        if (token.startsWith(prefix))
            return token.substring(prefix.length());
    }//from w w  w . j ava  2s. c  o  m
    return null;
}

From source file:org.devgateway.toolkit.forms.wicket.SSAuthenticatedWebSession.java

/**
 * Adds effective roles to Wicket {@link Roles} object. It does so by
 * getting authorities from {@link Authentication#getAuthorities()} and
 * building effective roles list by taking in account role hierarchy.
 *
 * @param roles//from  w w w .  ja  va2 s .  c om
 * @param authentication
 */
private void addRolesFromAuthentication(final Roles roles, final Authentication authentication) {
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    for (GrantedAuthority authority : roleHierarchy.getReachableGrantedAuthorities(authorities)) {
        roles.add(authority.getAuthority());
    }
}

From source file:com.bigbang.iowaservices.handlers.CustomAuthenticationSuccessHandler.java

/**
 * Builds the target URL according to the logic defined in the main class
 * Javadoc.//from   w  w  w. ja  v  a 2 s. co m
 */
protected String determineTargetUrl(Authentication authentication) {
    boolean isUser = false;
    boolean isAdmin = false;
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    for (GrantedAuthority grantedAuthority : authorities) {
        if (grantedAuthority.getAuthority().equals("ROLE_USER")) {
            isUser = true;
            break;
        } else if (grantedAuthority.getAuthority().equals("ROLE_ADMIN")) {
            isAdmin = true;
            break;
        }
    }

    if (isUser) {
        return "/homepage.html";
    } else if (isAdmin) {
        return "/addSkill.xhtml";
    } else {
        throw new IllegalStateException();
    }
}

From source file:br.com.jreader.util.security.URLAuthenticationSuccessHandler.java

private String determineTargetUrl(Authentication authentication) {
    boolean isCommon = false;
    boolean isAdmin = false;
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    OUTER: for (GrantedAuthority grantedAuthority : authorities) {
        switch (grantedAuthority.getAuthority()) {
        case "ROLE_USER":
            isCommon = true;// w  w  w  .  jav  a2 s . c o m
            break;
        case "ROLE_ADMIN":
            isAdmin = true;
            break;
        }
    }

    if (isCommon) {
        return "/protected/main.xhtml";
    } else if (isAdmin) {
        return "/protected/main.xhtml?admin=true";
    } else {
        throw new IllegalStateException();
    }
}