Package org.springframework.security

Examples of org.springframework.security.Authentication


     */
    public User getActiveUser() {
        HttpServletRequest request = RequestUtils.getActiveRequest();
        User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER);
        if (user instanceof UserImpl) return user;
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null) {
            if (auth.getPrincipal() instanceof UserDetails) {
                user = securityDAO.findUser(((UserDetails) auth.getPrincipal()).getUsername());
                WebUtils.setSessionAttribute(request, SESSION_USER, user);
                return user;
            }
        }
        if (user == null) {
View Full Code Here


    }

    protected User getUser(HttpServletRequest request) {
        User user = (User) WebUtils.getSessionAttribute(request, SESSION_USER);
        if (user instanceof GuestUser) {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            if ((auth != null) && (auth.getPrincipal() instanceof UserDetails)) {
                user = securityDAO.findUser(((UserDetails) auth.getPrincipal()).getUsername());
                if (logger.isDebugEnabled()) logger.debug("Setting domain user [" + user.getName() + "] in session");
                WebUtils.setSessionAttribute(request, SESSION_USER, user);
                return user;
            }
        }
View Full Code Here

    public void changePassword(String oldPassword, String newPassword) {

        User user = getCurrentUser();

        Authentication oldAuth = new UsernamePasswordAuthenticationToken(
                user.getUsername(), oldPassword);
        authMgr.authenticate(oldAuth);

        createPassWord(user, newPassword);

        log.debug("password changed, saving");
        save(user);

        log.debug("remove from cache");
        userCache.removeUserFromCache(user.getUsername());

        log.debug("change security context");
        Authentication newAuthentication = new UsernamePasswordAuthenticationToken(
                user.getUsername(), newPassword);
        authMgr.authenticate(newAuthentication);
        SecurityContextHolder.getContext().setAuthentication(
                newAuthentication);
View Full Code Here

    public User getCurrentUser(boolean useCache)
            throws UsernameNotFoundException {

        log.debug("getCurrentUser");

        Authentication auth = SecurityContextHolder.getContext()
                .getAuthentication();

        if (null == auth) {
            throw new UsernameNotFoundException("No Authentications");
        }

        Object obj = auth.getPrincipal();
        String username = "";

        if (obj instanceof UserDetails) {
            username = ((UserDetails) obj).getUsername();
        } else {
View Full Code Here

        }

        // We have to fill something into the security context regardless of whether the user is anonymous
        GaeUserAuthenticationToken token = new GaeUserAuthenticationToken(gaeUser);
        token.setDetails(this.authenticationDetailsSource.buildDetails(request));
        Authentication authentication = this.authenticationManager.authenticate(token);
        this.getSecurityContext().setAuthentication(authentication);

        // Execute the rest of the filter chain now that we have a valid security context
        chain.doFilter(request, response);
    }
View Full Code Here

        when(session.getAttribute(CLIENT_ROLES_ATTRIBUTE)).thenReturn(clientRoles);
        when(request.getSession(false)).thenReturn(session);

        filter.doFilter(request, response, chain);

        Authentication authentication = filter.getMockSecurityContext().getAuthentication();
        assertNotNull(authentication);
        assertTrue(authentication instanceof StubbedAuthentication);
        GaeUserAuthenticationToken token = ((StubbedAuthentication) authentication).getToken();
        assertNotNull(token.getDetails())// don't care exactly what's in it, just that it's filled in
        assertTrue(token.getPrincipal() instanceof GaeUser);
View Full Code Here

    }

    /** Test the authenticate() method. */
    @Test public void testAuthenticate() {
        GaeUser gaeUser = new GaeUser();
        Authentication authentication = mock(Authentication.class);
        when(authentication.getPrincipal()).thenReturn(gaeUser);
        GaeAuthenticationProvider provider = new GaeAuthenticationProvider();
        Authentication token = provider.authenticate(authentication);
        assertTrue(token instanceof GaeUserAuthenticationToken);
        GaeUserAuthenticationToken gaeToken = (GaeUserAuthenticationToken) token;
        assertEquals(gaeUser, gaeToken.getPrincipal());
        assertEquals(1, gaeToken.getAuthorities().length);
        assertContains(GaeRole.ROLE_ANONYMOUS, gaeToken.getAuthorities());
View Full Code Here

      if(needCredential)
        logger.debug("Credential is required.");
      logger.debug("Current ACL:{}", needCheck ? acl : "");
    }
   
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
   
    if(needCredential && authentication == null){
      logger.error("Session timeout.");
      throw new BusinessException(PreserveErrorCode.SESSION_TIMEOUT);
    }
   
    if(needCheck){
      BaseProfileAction act = (BaseProfileAction)action;
                 
      LoxiaUserDetails userDetails = (LoxiaUserDetails)authentication.getPrincipal();
      logger.debug("Current Principal:" + userDetails);
      String entryAcl = act.getAcl();
      if(entryAcl != null){
        userDetails.setCurrentOu(null);
        logger.debug("Function Entrance... Organization need to repick");
       
        for(GrantedAuthority auth: userDetails.getAuthorities()){
          LoxiaGrantedAuthority lauth = (LoxiaGrantedAuthority)auth;
          if(lauth.getAuthority().equals(entryAcl)){
            userDetails.setCurrentAuthority(lauth);
            break;
          }
        }
        if(userDetails.getCurrentAuthority() == null ||
            userDetails.getCurrentAuthority().getOuIds().size() == 0){
          logger.error("No sufficicent privilege.");
          throw new BusinessException(PreserveErrorCode.NO_SUFFICICENT_PRIVILEGE);
        }else{
          if(userDetails.getCurrentAuthority().
              getOuIds().size() == 1){
            userDetails.setCurrentOu(operatingUnitDao.getByPrimaryKey(
                userDetails.getCurrentAuthority().getOuIds().iterator().next()));
          }else{
            logger.debug("Redirect Invocation");
           
            String url = request.getRequestURI();
            Enumeration<String> paramNames = request.getParameterNames();
            StringBuffer paramsSb = new StringBuffer();
            while (paramNames.hasMoreElements()) {
              String name = (String) paramNames.nextElement();
              if (!"acl".equalsIgnoreCase(name)){
                paramsSb.append(name + "=" + request.getParameter(name) + "&");
              }
            }
            if (paramsSb.length() > 0){
              paramsSb.deleteCharAt(paramsSb.length()-1);
              url = url + "?" + paramsSb.toString();
            }
            request.getSession().setAttribute(BaseAction.FOLLOWING_URL_AFTER_OPERATING_UNIT_PICKUP, url);
            response.sendRedirect(request.getContextPath() + "/operatingunitpickup.do");
            return null;
          }
        }
      }else{
        if(act.getSelectedOuId() != null){
          //set Current OperatingUint in up
          userDetails.setCurrentOu(operatingUnitDao.getByPrimaryKey(act.getSelectedOuId()));
        }else{
          if(!userDetails.checkAuthority(acl.value())){
            logger.error("No sufficicent privilege.");
            throw new BusinessException(PreserveErrorCode.NO_SUFFICICENT_PRIVILEGE);
          }
        }         
      }
           
    }
   
    if(authentication != null && authentication.getPrincipal()instanceof LoxiaUserDetails){   
      LoxiaUserDetails userDetails = (LoxiaUserDetails)authentication.getPrincipal();
      if(action instanceof LoxiaUserDetailsAware){
        LoxiaUserDetailsAware aware = (LoxiaUserDetailsAware)action;
        aware.setLoxiaUserDetails(userDetails);
      }
      if(userDetails.getCurrentOu() == null){
View Full Code Here

  public boolean preHandle(HttpServletRequest request,
      HttpServletResponse response, Object handler) throws Exception {
    if(handler instanceof LoxiaUserDetailsAware){
      LoxiaUserDetailsAware aware = (LoxiaUserDetailsAware)handler;
     
      Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
      if(authentication != null && authentication.getPrincipal()instanceof LoxiaUserDetails){
        LoxiaUserDetails userDetails = (LoxiaUserDetails)authentication.getPrincipal();
        aware.setLoxiaUserDetails(userDetails);
      }     
    }
    return true;
  }
View Full Code Here

                return true;
            }

            user = userService.createUser(fullName, email, username, password1);

            Authentication token = new UsernamePasswordAuthenticationToken(username, password1);
            Authentication result = authenticationManager.authenticate(token);
            SecurityContext securityContext = new SecurityContextImpl();
            securityContext.setAuthentication(result);
            SecurityContextHolder.setContext(securityContext);

            String path = redirectField.getValue();
View Full Code Here

TOP

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