Example usage for javax.servlet.http HttpServletRequest getUserPrincipal

List of usage examples for javax.servlet.http HttpServletRequest getUserPrincipal

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getUserPrincipal.

Prototype

public java.security.Principal getUserPrincipal();

Source Link

Document

Returns a java.security.Principal object containing the name of the current authenticated user.

Usage

From source file:nl.b3p.kaartenbalie.service.servlet.GeneralServlet.java

protected static User checkLoginAlreadyLoggedIn(HttpServletRequest request, EntityManager em, String pcode) {

    /* Gebruiker al ingelogd en persoonlijke code hetzelfde ? */
    User user = (User) request.getUserPrincipal();

    if (user != null) {
        String userCode = user.getPersonalURL();
        if (pcode != null && userCode != null && !pcode.equals(userCode)) {
            user = null;//from w w w .j  a  v a  2  s.c  o  m
        }
    }

    if (user != null) {
        log.debug("Gebruiker " + user.getName() + " al ingelogd via cookie.");
    }

    return user;
}

From source file:org.jahia.bin.errors.ErrorLoggingFilter.java

protected static String getUserInfo(HttpServletRequest request) {

    JahiaUser user = JCRSessionFactory.getInstance().getCurrentUser();
    if (user == null) {
        try {// w w w .  j a va2s  . c  o  m
            HttpSession session = request.getSession(false);
            if (session != null) {
                user = (JahiaUser) session.getAttribute(Constants.SESSION_USER);
            }
        } catch (IllegalStateException ex) {
            // ignore it
        }
    }
    String info = user != null ? user.getUsername() : null;

    // last chance: request's user principal
    info = info != null ? info : String.valueOf(request.getUserPrincipal());

    return info;
}

From source file:net.skhome.roborace.web.controller.SessionController.java

@RequestMapping(method = RequestMethod.GET)
@ResponseBody//w  w w  . j  ava 2s .c  om
public boolean isLoggedIn(final HttpServletRequest request) {
    return (request.getUserPrincipal() != null);
}

From source file:info.magnolia.cms.security.Authenticator.java

/**
 * Authenticate authorization request using JAAS login module as configured
 * @param request as received by the servlet engine
 * @return boolean//from w  w w .  j  a  v a  2  s.  c  o m
 */
public static boolean authenticate(HttpServletRequest request) {
    String credentials = request.getHeader("Authorization");
    String userid;
    String pswd;
    CredentialsCallbackHandler callbackHandler;
    String loginModuleToInitialize = "magnolia"; // default login module

    if (StringUtils.isEmpty(credentials) || credentials.length() <= 6) {
        // check for form based login request
        if (StringUtils.isNotEmpty(request.getParameter(PARAMETER_USER_ID))) {
            userid = request.getParameter(PARAMETER_USER_ID);
            pswd = StringUtils.defaultString(request.getParameter(PARAMETER_PSWD));
            callbackHandler = new PlainTextCallbackHandler(userid, pswd.toCharArray());
        } else {
            // select login module to use if user is authenticated against the container
            if (request.getUserPrincipal() != null) {
                loginModuleToInitialize = "magnolia_authorization";
                callbackHandler = new PlainTextCallbackHandler(request.getUserPrincipal().getName(),
                        "".toCharArray());
            } else {
                // invalid auth request
                return false;
            }
        }
    } else {
        // its a basic authentication request
        callbackHandler = new Base64CallbackHandler(credentials);
    }

    Subject subject;
    try {
        LoginContext loginContext = new LoginContext(loginModuleToInitialize, callbackHandler);
        loginContext.login();
        subject = loginContext.getSubject();
        // ok, we NEED a session here since the user has been authenticated
        HttpSession httpsession = request.getSession(true);
        httpsession.setAttribute(ATTRIBUTE_JAAS_SUBJECT, subject);
    } catch (LoginException le) {
        if (log.isDebugEnabled())
            log.debug("Exception caught", le);

        HttpSession httpsession = request.getSession(false);
        if (httpsession != null) {
            httpsession.invalidate();
        }
        return false;
    }

    return true;
}

From source file:org.eclipse.skalli.view.LoginUtils.java

/**
 * Returns a <code>LoginUtil</code> instance that has been initialized
 * from the given servlet request. This constructor retrieves the unique identifier
 * of the authenticated user with {@link HttpServletRequest#getUserPrincipal()}.
 *//*from  w  ww  . j a  v a  2  s. c om*/
public LoginUtils(HttpServletRequest request) {
    Principal userPrincipal = request.getUserPrincipal();
    if (userPrincipal != null) {
        userName = userPrincipal.getName();
        if (StringUtils.isNotBlank(userName)) {
            userName = userName.toLowerCase(Locale.ENGLISH);
        }
    }
}

From source file:org.apache.cxf.fediz.spring.preauth.FederationPreAuthenticatedProcessingFilter.java

/**
 * Return the J2EE user name./*from  w  w w .ja v  a 2 s.  c om*/
 */
protected Object getPreAuthenticatedPrincipal(HttpServletRequest httpRequest) {
    Object principal = httpRequest.getUserPrincipal();
    if (logger.isDebugEnabled()) {
        logger.debug("PreAuthenticated J2EE principal: " + httpRequest.getUserPrincipal() == null ? null
                : httpRequest.getUserPrincipal().getName());
    }
    return principal;
}

From source file:de.punyco.thirtytwosquare.auth.GooglePreAuthenticationFilter.java

@Override
protected Object getPreAuthenticatedCredentials(HttpServletRequest request) {

    Principal principal = request.getUserPrincipal();

    if (principal != null) {
        return DUMMY_CREDENTIALS;
    } else {//from  ww w.ja v  a 2  s .  c  o  m
        LOG.info("No pre-authenticated principal available. Returning anonymous (null) credentials.");

        return ANONYMOUS;
    }
}

From source file:org.apache.cxf.fediz.spring.preauth.FederationPreAuthenticatedProcessingFilter.java

/**
 * For J2EE container-based authentication there is no generic way to
 * retrieve the credentials, as such this method returns a fixed dummy
 * value.//from w ww  . ja  va2 s.co m
 */
protected Object getPreAuthenticatedCredentials(HttpServletRequest httpRequest) {
    Object principal = httpRequest.getUserPrincipal() == null ? null : httpRequest.getUserPrincipal();
    if (principal instanceof FedizPrincipal) {
        Object obj = httpRequest.getSession(false).getAttribute(SECURITY_TOKEN_ATTR);
        if (obj != null) {
            return obj;
        } else {
            logger.error("Session must contain Federation response");
            throw new IllegalStateException("Session must contain Federation response");
        }
    } else {
        logger.error("Principal must be instance of FedizPrincipal: " + principal.toString());
        throw new IllegalStateException("Principal must be instance of FedizPrincipal");
    }
    //return "N/A";
}

From source file:org.smigo.user.UserSetLocaleResolver.java

@Override
public Locale resolveLocale(HttpServletRequest req) {
    final Principal userPrincipal = req.getUserPrincipal();
    if (userPrincipal != null) {
        User user = userDao.getUsersByUsername(userPrincipal.getName()).get(0);
        if (user.getLocale() != null) {
            return user.getLocale();
        }/* w w  w  .j  a v a2s  . c  om*/
    }

    final String subDomain = req.getServerName().split("\\.")[0];
    for (Language language : Language.values()) {
        if (language.getLocale().getLanguage().equals(subDomain)) {
            return new Locale(subDomain);
        }
    }

    return req.getLocale() == null ? Locale.ENGLISH : req.getLocale();
}

From source file:de.punyco.thirtytwosquare.auth.GooglePreAuthenticationFilter.java

@Override
protected Object getPreAuthenticatedPrincipal(HttpServletRequest request) {

    Principal principal = request.getUserPrincipal();

    if (principal != null) {
        LOG.info("Request has principal {}", principal);

        User currentUser = userService.getCurrentUser();

        return currentUser;
    } else {//from w  w w .j av a2  s  .co m
        LOG.info("No pre-authenticated principal available. Returning anonymous (null) principal.");

        return ANONYMOUS;
    }
}