Package org.springframework.security.core

Examples of org.springframework.security.core.Authentication


        return user;
    }

    @Override
    public User getAuthenticatedUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null && authentication.getPrincipal() instanceof User) {
            return (User) authentication.getPrincipal();
        } else {
            throw new SecurityException("Could not get the authenticated user!");
        }
    }
View Full Code Here


        for (Entitlement entitlement : entitlementDAO.findAll()) {
            authorities.add(new SimpleGrantedAuthority(entitlement.getName()));
        }

        UserDetails userDetails = new User(adminUser, "FAKE_PASSWORD", true, true, true, true, authorities);
        Authentication authentication = new TestingAuthenticationToken(userDetails, "FAKE_PASSWORD", authorities);
        SecurityContextHolder.getContext().setAuthentication(authentication);
    }
View Full Code Here

    @Test
    public void testGetAuthenticatedUser() throws Exception {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
        grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
        UserDetails userDetails = new UserImpl("1", "canonical");
        Authentication authentication = new TestingAuthenticationToken(userDetails, "canonical", grantedAuthorities);
        SecurityContextHolder.getContext().setAuthentication(authentication);

        User returnedUser = userService.getAuthenticatedUser();
        assertEquals(returnedUser, userDetails);
    }
View Full Code Here

        }
    }

    @Override
    public User getAuthenticatedUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null && authentication.getPrincipal() instanceof User) {
            return (User) authentication.getPrincipal();
        } else {
            throw new SecurityException("Could not get the authenticated user!");
        }
    }
View Full Code Here

    @Autowired(required = false)
    @Qualifier("authenticationManager")
    AuthenticationManager authenticationManager;

    public LoginStatus getStatus() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null && !auth.getName().equals("anonymousUser") && auth.isAuthenticated()) {
            return new LoginStatus(true, auth.getName(), auth.getAuthorities());
        } else {
            return new LoginStatus(false, null, auth.getAuthorities());
        }
    }
View Full Code Here

    public LoginStatus login(String username, String password) {
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
        logger.debug("Attempting login.");
        try {
            Authentication auth = authenticationManager.authenticate(token);
            logger.debug("Login succeeded!");
            SecurityContextHolder.getContext().setAuthentication(auth);
            Collection<? extends GrantedAuthority> authorities = null;
            if (auth.getAuthorities().isEmpty()){
              authorities = new ArrayList<GrantedAuthority>();
            } else {
              authorities = auth.getAuthorities();
            }
            return new LoginStatus(auth.isAuthenticated(), auth.getName(), authorities);
        } catch (BadCredentialsException e) {
          Collection<? extends GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            return new LoginStatus(false, null, authorities);
        }
    }
View Full Code Here

        public boolean validate(PasswordValidationCallback.Request request)
                throws PasswordValidationCallback.PasswordValidationException {
            PasswordValidationCallback.PlainTextPasswordRequest plainTextRequest =
                    (PasswordValidationCallback.PlainTextPasswordRequest) request;
            try {
                Authentication authResult = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
                        plainTextRequest.getUsername(), plainTextRequest.getPassword()));
                if (logger.isDebugEnabled()) {
                    logger.debug("Authentication success: " + authResult.toString());
                }
                SecurityContextHolder.getContext().setAuthentication(authResult);
                return true;
            }
            catch (AuthenticationException failed) {
View Full Code Here

public class SpringUsernamePasswordCallbackHandler extends AbstractCallbackHandler {

    @Override
    protected void handleInternal(Callback callback) throws IOException, UnsupportedCallbackException {
        if (callback instanceof UsernameCallback) {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.getName() != null) {
                UsernameCallback usernameCallback = (UsernameCallback) callback;
                usernameCallback.setUsername(authentication.getName());
                return;
            }
            else {
                logger.warn(
                        "Cannot handle UsernameCallback: Spring Security SecurityContext contains no Authentication");
            }
        }
        else if (callback instanceof PasswordCallback) {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            if (authentication != null && authentication.getName() != null) {
                PasswordCallback passwordCallback = (PasswordCallback) callback;
                passwordCallback.setPassword(authentication.getCredentials().toString());
                return;
            }
            else {
                logger.warn(
                        "Canot handle PasswordCallback: Spring Security SecurityContext contains no Authentication");
View Full Code Here

       
      }
     
      @Override
      public Authentication getAuthentication() {
        return new Authentication() {
         
          @Override
          public String getName() {
            // TODO Auto-generated method stub
            return null;
View Full Code Here

       
      }
     
      @Override
      public Authentication getAuthentication() {
        return new Authentication() {
         
          @Override
          public String getName() {
            // TODO Auto-generated method stub
            return null;
View Full Code Here

TOP

Related Classes of org.springframework.security.core.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.