Package org.acegisecurity

Examples of org.acegisecurity.Authentication


     * Obtain the current active <code>Authentication</code>
     *
     * @return the authentication object or <code>null</code>
     */
    private Authentication getAuthentication() {
        Authentication auth = SecurityContextHolder.getContext()
                                                   .getAuthentication();

        if (!authenticationTrustResolver.isAnonymous(auth)) {
            return auth;
        }
View Full Code Here


        return null;
    }

    private boolean isGranted(String role) {
        Authentication auth = getAuthentication();

        if ((auth == null) || (auth.getPrincipal() == null)
            || (auth.getauthorities() == null)) {
            return false;
        }

        for (int i = 0; i < auth.getauthorities().length; i++) {
            if (role.equals(auth.getauthorities()[i].getauthority())) {
                return true;
            }
        }

        return false;
View Full Code Here

     */
    public void before(Method method, Object[] args, Object target) throws Throwable {
        SecurityContext ctx = SecurityContextHolder.getContext();

        if (ctx.getAuthentication() != null) {
            Authentication auth = ctx.getAuthentication();
            boolean administrator = false;
            GrantedAuthority[] roles = auth.getauthorities();
            for (int i=0; i < roles.length; i++) {
                if (roles[i].getauthority().equals(Constants.ADMIN_ROLE)) {
                    administrator = true;
                    break;
                }
            }

            User user = (User) args[0];
            String username = user.getUsername();

            String currentUser = null;
            if (auth.getPrincipal() instanceof UserDetails) {
                currentUser = ((UserDetails) auth.getPrincipal()).getUsername();
            } else {
                currentUser = String.valueOf(auth.getPrincipal());
            }

            if (!username.equals(currentUser)) {
                AuthenticationTrustResolver resolver = new AuthenticationTrustResolverImpl();
                // allow new users to signup - this is OK b/c Signup doesn't allow setting of roles
View Full Code Here

            }
           
            userCache.removeUserFromCache(user.getUsername());
           
            // reset the authentication object if current user
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if (auth != null && auth.getPrincipal() instanceof UserDetails) {
                User currentUser = (User) auth.getPrincipal();
                if (currentUser.getUsername().equalsIgnoreCase(user.getUsername())) {
                    auth = new UsernamePasswordAuthenticationToken(user, user.getPassword(), user.getauthorities());
                    SecurityContextHolder.getContext().setAuthentication(auth);
                }
            }
View Full Code Here

                        "An Authentication object was not found in the SecurityContext"),
                    object, attr);
            }

            // Attempt authentication if not already authenticated, or user always wants reauthentication
            Authentication authenticated;

            if (!SecurityContextHolder.getContext().getAuthentication()
                                      .isAuthenticated()
                || alwaysReauthenticate) {
                try {
                    authenticated = this.authenticationManager.authenticate(SecurityContextHolder.getContext()
                                                                                                 .getAuthentication());
                } catch (AuthenticationException authenticationException) {
                    throw authenticationException;
                }

                // We don't authenticated.setAuthentication(true), because each provider should do that
                if (logger.isDebugEnabled()) {
                    logger.debug("Successfully Authenticated: "
                        + authenticated.toString());
                }

                SecurityContextHolder.getContext()
                                     .setAuthentication(authenticated);
            } else {
                authenticated = SecurityContextHolder.getContext()
                                                     .getAuthentication();

                if (logger.isDebugEnabled()) {
                    logger.debug("Previously Authenticated: "
                        + authenticated.toString());
                }
            }

            // Attempt authorization
            try {
                this.accessDecisionManager.decide(authenticated, object,
                    attr);
            } catch (AccessDeniedException accessDeniedException) {
                AuthorizationFailureEvent event = new AuthorizationFailureEvent(object,
                        attr, authenticated, accessDeniedException);
                this.eventPublisher.publishEvent(event);

                throw accessDeniedException;
            }

            if (logger.isDebugEnabled()) {
                logger.debug("authorization successful");
            }

            AuthorizedEvent event = new AuthorizedEvent(object, attr,
                    authenticated);
            this.eventPublisher.publishEvent(event);

            // Attempt to run as a different user
            Authentication runAs = this.runAsManager.buildRunAs(authenticated,
                    object, attr);

            if (runAs == null) {
                if (logger.isDebugEnabled()) {
                    logger.debug(
                        "RunAsManager did not change Authentication object");
                }

                return new InterceptorStatusToken(authenticated, false,
                    attr, object); // no further work post-invocation
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Switching to RunAs Authentication: "
                        + runAs.toString());
                }

                SecurityContextHolder.getContext().setAuthentication(runAs);

                return new InterceptorStatusToken(authenticated, true,
View Full Code Here

        context.setAuthentication(token);
        SecurityContextHolder.setContext(context);
    }

    public void testAddUserWithoutAdminRole() throws Exception {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        assertTrue(auth.isAuthenticated());
        UserManager userManager = (UserManager) makeInterceptedTarget();
        User user = new User("admin");

        try {
            userManager.saveUser(user);
View Full Code Here

        SecurityContext context = (SecurityContext) event.getSession().getAttribute(HttpSessionContextIntegrationFilter.ACEGI_SECURITY_CONTEXT_KEY);
        if (context == null) {
            log.debug("The destroyed session has no SecurityContext");
            return;
        }
        Authentication auth = context.getAuthentication();
        if ((auth != null) && (auth instanceof JaasAuthenticationToken)) {
            JaasAuthenticationToken token = (JaasAuthenticationToken) auth;
            try {
                LoginContext loginContext = token.getLoginContext();
                if (loginContext != null) {
View Full Code Here

     *         <code>Authentication</code> associated with this request.
     */
    protected Authentication attemptExitUser(HttpServletRequest request)
        throws AuthenticationCredentialsNotFoundException {
        // need to check to see if the current user has a SwitchUserGrantedAuthority
        Authentication current = SecurityContextHolder.getContext()
                                                      .getAuthentication();

        if (null == current) {
            throw new AuthenticationCredentialsNotFoundException(messages
                    .getMessage("SwitchUserProcessingFilter.noCurrentUser",
                        "No current user associated with this request"));
            }

            // check to see if the current user did actual switch to another user
            // if so, get the original source user so we can switch back
            Authentication original = getSourceAuthentication(current);

            if (original == null) {
                logger.error(
                    "Could not find original user Authentication object!");
                throw new AuthenticationCredentialsNotFoundException(messages
                        .getMessage(
                            "SwitchUserProcessingFilter.noOriginalAuthentication",
                            "Could not find original Authentication object"));
                }

                // get the source user details
                UserDetails originalUser = null;
                Object obj = original.getPrincipal();

                if ((obj != null) && obj instanceof UserDetails) {
                    originalUser = (UserDetails) obj;
                }

View Full Code Here

                        UserDetails targetUser) {
                        UsernamePasswordAuthenticationToken targetUserRequest;

                        // grant an additional authority that contains the original Authentication object
                        // which will be used to 'exit' from the current switched user.
                        Authentication currentAuth = SecurityContextHolder.getContext()
                                                                          .getAuthentication();
                        GrantedAuthority switchauthority = new SwitchUserGrantedAuthority(ROLE_PREVIOUS_ADMINISTRATOR,
                                currentAuth);

                        // get the original authorities
View Full Code Here

                        HttpServletResponse httpResponse = (HttpServletResponse) response;

                        // check for switch or exit request
                        if (requiresSwitchUser(httpRequest)) {
                            // if set, attempt switch and store original
                            Authentication targetUser = attemptSwitchUser(httpRequest);

                            // update the current context to the new target user
                            SecurityContextHolder.getContext()
                                                 .setAuthentication(targetUser);

                            // redirect to target url
                            httpResponse.sendRedirect(httpResponse
                                    .encodeRedirectURL(httpRequest
                                            .getContextPath() + targetUrl));

                                    return;
                                } else if (requiresExitUser(httpRequest)) {
                                    // get the original authentication object (if exists)
                                    Authentication originalUser = attemptExitUser(httpRequest);

                                    // update the current context back to the original user
                                    SecurityContextHolder.getContext()
                                                         .setAuthentication(originalUser);
View Full Code Here

TOP

Related Classes of org.acegisecurity.Authentication

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.