Example usage for javax.servlet.http HttpServletRequest getSession

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

Introduction

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

Prototype

public HttpSession getSession();

Source Link

Document

Returns the current session associated with this request, or if the request does not have a session, creates one.

Usage

From source file:com.shared.rides.controller.SignupUserController.java

@RequestMapping(value = "/register.do", method = RequestMethod.POST)
public @ResponseBody String signupUser(@RequestParam("organization") long organization,
        @RequestParam("personalId") String personalId, @RequestParam("pw") String pw,
        @RequestParam("name") String name, @RequestParam("surname") String surname,
        @RequestParam("email") String email, @RequestParam("phone") long phoneNumber,
        @RequestParam("street") String street, @RequestParam("number") int numberStreet,
        @RequestParam("neighborhood") String neighborhood, @RequestParam("shift") String shift,
        @RequestParam("userType") String userType, @RequestParam("brand") String brand,
        @RequestParam("modelVehicle") String modelVehicle, @RequestParam("plateLetters") String plateLetters,
        @RequestParam("plateNumbers") String plateNumbers, @RequestParam("numberSeats") int numberSeats,
        @RequestParam("days") String days, HttpServletRequest r) {
    String pic = r.getSession().getAttribute("picName").toString();
    String licensePlate = plateLetters + " " + plateNumbers;
    signupUserService.signupUser(organization, personalId, pw, name, surname, phoneNumber, email, street,
            numberStreet, neighborhood, shift, userType, brand, modelVehicle, licensePlate, numberSeats, days,
            pic);//  w w  w.  ja  v a  2  s.  co m
    return null;
}

From source file:com.github.fedorchuck.webstore.web.controllers.HomeController.java

@RequestMapping(method = GET)
public ModelAndView home(HttpServletRequest request) {
    request.getSession().setAttribute("session", session);

    ModelAndView model = new ModelAndView("index");
    model.addObject(new Commodity());
    model.addObject("userActions", new UserActions());

    List<Category> categories = commodityRepository.findByCategory();
    model.addObject("categories", categories);

    return model;
}

From source file:com.MyHistory.Controller.UsuarioController.java

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public ModelAndView cerrarSesion(HttpServletRequest pRequest) {
    HttpSession sesion = pRequest.getSession();
    sesion.invalidate();/*from   w ww .  ja  va 2 s.c om*/
    ModelAndView model = new ModelAndView();
    model.setViewName("redirect:/login.htm");
    return model;
}

From source file:it.cspnet.beneficium.web.UtenteController.java

@RequestMapping(value = "logout", method = RequestMethod.GET)

public @ResponseBody JsonResult logout(HttpServletRequest request) throws Exception {
    request.getSession().invalidate();

    JsonResult jR = new JsonResult();
    jR.setStatus(true);/*ww  w  .  ja va2  s  .c o  m*/

    return jR;

}

From source file:com.platform.learn.controller.LoginController.java

@RequestMapping("/logout")
public String logout(HttpServletRequest request) {
    request.getSession().removeAttribute(Global.USER_SESSION_KEY);
    return "redirect:/";
}

From source file:net.sf.jsfcomp.chartcreator.Chartlet.java

private void emptySession(HttpServletRequest request, String id) {
    request.getSession().removeAttribute(id);
}

From source file:springku.BelajarController.java

@RequestMapping(value = "/simpan")
public String simpan(@ModelAttribute("hallo") Hallo h, ModelMap map, HttpServletRequest request) {
    HttpSession session = request.getSession();
    map.addAttribute("pesan", session.getAttribute("username"));
    return "learning";
}

From source file:com.aurel.track.prop.LoginBL.java

/**
 * This method controls entire login procedure.
 *
 * @param isInTestMode/*from  w w  w.  j a va 2s.c o  m*/
 * @param isMobileApplication
 * @param username
 * @param usingContainerBasedAuthentication
 * @param password
 * @param forwardUrl
 * @param springAuthenticated
 * @param mobileApplicationVersionNo
 * @param locale
 * @return
 */
public static String login(String isInTestMode, boolean isMobileApplication, String username,
        boolean usingContainerBasedAuthentication, String password, String forwardUrl,
        boolean springAuthenticated, Integer mobileApplicationVersionNo, Locale _locale) {
    Boolean ready = (Boolean) ServletActionContext.getServletContext().getAttribute(ApplicationStarter.READY);
    if (ready == null || !ready.booleanValue()) {
        return "loading";
    }
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpSession httpSession = request.getSession();
    String nonce = (String) httpSession.getAttribute("NONCE");

    if ("true".equals(isInTestMode)) {
        nonce = null; // accept clear text passwords
    }

    httpSession.setAttribute(ISMOBILEAPP, isMobileApplication);

    Locale locale = _locale;
    if (locale == null) {
        locale = Locale.getDefault();
        LOGGER.debug("Requested locale is null. Using default:" + locale.getDisplayName());
    } else {
        LOGGER.debug("Requested locale " + locale.getDisplayName());
    }
    httpSession.setAttribute("localizationJSON", LocalizeJSON.encodeLocalization(locale));
    TMotdBean motd = MotdBL.loadMotd(locale.getLanguage());

    if (motd == null) {
        motd = MotdBL.loadMotd("en");
    }
    // if already logged in forward to home page
    if (SessionUtils.getCurrentUser(httpSession) != null) {

        String redirectMapEntry = "itemNavigator";
        TPersonBean personBean = (TPersonBean) httpSession.getAttribute(Constants.USER_KEY);
        if (personBean != null && personBean.getHomePage() != null
                && personBean.getHomePage().trim().length() > 0) {
            redirectMapEntry = personBean.getHomePage();
        }
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        JSONUtility.appendBooleanValue(sb, JSONUtility.JSON_FIELDS.SUCCESS, true);
        sb.append(DATABRACE);
        JSONUtility.appendStringValue(sb, "jsonURL", redirectMapEntry + DOTACTION, true);
        sb.append("}");
        sb.append("}");
        return LoginBL.writeJSONResponse(sb); // The redirect is done by the
        // client JavaScript
    }
    // if Container Based Authentication is enabled and we can get a remote
    // user we use that one, no more questions asked. However, a local login
    // always overrules.

    if ((username == null || "".equals(username) || password == null || "".equals(password))
            && (request.getRemoteUser() != null
                    && ApplicationBean.getInstance().getSiteBean().getIsCbaAllowed())) {
        username = request.getRemoteUser();
        usingContainerBasedAuthentication = true;
    }

    List<LabelValueBean> errors = new ArrayList<LabelValueBean>();
    StringBuilder sb = new StringBuilder();
    String redirectMapEntry = "";
    sb = LoginBL.createLoginResponseJSON(username, password, nonce, usingContainerBasedAuthentication,
            springAuthenticated, request, errors, httpSession, forwardUrl, motd, isMobileApplication, locale,
            mobileApplicationVersionNo, redirectMapEntry);

    if (errors != null && errors.size() > 0 && usingContainerBasedAuthentication) {
        return "forwardToLogin"; // could not verify container registered
        // user with Genji
    }

    if (usingContainerBasedAuthentication && !isMobileApplication) {
        ACCESSLOGGER.info("User was authenticated via container.");
        if (redirectMapEntry.isEmpty())
            return SUCCESS;
        return redirectMapEntry;
    }
    return writeJSONResponse(sb); // The redirect is done by the client
    // JavaScript
}

From source file:io.muic.ooc.webapp.service.SecurityService.java

public boolean isAuthorized(HttpServletRequest request) throws Exception {
    String username = (String) request.getSession().getAttribute("username");
    // do checking
    System.out.println(username);
    MySQLService sql = new MySQLService();
    return (username != null && sql.findOne(username));
}

From source file:ch.cern.security.saml2.utils.UrlUtils.java

/**
 * Generates the URL response (IdP)//from  ww  w . j a v  a2  s . c  om
 * 
 * @param request
 * @param isDebugEnabled
 * @return The logout URL:
 *         https://login.cern.ch/adfs/ls/?SAMLResponse=value&
 *         SigAlg=value&Signature=value
 * @throws DataFormatException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 * @throws UnrecoverableKeyException
 * @throws InvalidKeyException
 * @throws KeyStoreException
 * @throws NoSuchProviderException
 * @throws NoSuchAlgorithmException
 * @throws CertificateException
 * @throws SignatureException
 * @throws XMLStreamException
 */
public static String generateSamlResponse(HttpServletRequest request, boolean isDebugEnabled)
        throws DataFormatException, ParserConfigurationException, SAXException, IOException,
        UnrecoverableKeyException, InvalidKeyException, KeyStoreException, NoSuchProviderException,
        NoSuchAlgorithmException, CertificateException, SignatureException, XMLStreamException {

    // Get the request as an XML
    String xmlLogoutRequest = XMLUtils.xmlDecodeAndInflate(request.getParameter(Constants.SAML_REQUEST),
            isDebugEnabled);

    // Parse the xml request. Encapsulate the data in a SamlVO object
    SamlVO samlVO = XMLUtils.parseXMLmessage(xmlLogoutRequest, isDebugEnabled);

    // Add the destination: context-param in web.xml. This info is not in the logoutRequest
    samlVO.setDestination(
            (String) request.getSession().getServletContext().getAttribute(Constants.IDP_ENDPOINT));

    // Generates the ID
    byte[] buf = new byte[16];
    SecureRandom.getInstance(RANDOM_ALGORITHM).nextBytes(buf);
    samlVO.setId("_".concat(new String(Hex.encode(buf))));

    // Set the issueInstant
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
    samlVO.setIssueInstant(simpleDateFormat.format(new Date()));

    // Get the entityID!!!
    samlVO.setIssuer((String) request.getSession().getServletContext().getAttribute(Constants.ENTITY_ID));

    // Generate the LogoutResponse
    String samlResponse = XMLUtils.createXMLresponse(samlVO, isDebugEnabled);

    // Deflate and encode the LogoutResponse
    String base64response = XMLUtils.xmlDeflateAndEncode(samlResponse, isDebugEnabled);

    // URL-encode the deflatedResponse
    String urlEncodedresponse = URLEncoder.encode(base64response, Constants.CHARACTER_ENCODING);

    if (isDebugEnabled)
        nc.notice("Data to sign: " + Constants.SAML_RESPONSE + EQUAL + urlEncodedresponse + AMPERSAND
                + Constants.SIG_ALG + EQUAL
                + URLEncoder.encode(
                        (String) request.getSession().getServletContext().getAttribute(Constants.SIG_ALG),
                        Constants.CHARACTER_ENCODING));

    // Sign the SAMLResponse=value&SigAlg=value
    String signature = SignatureUtils.sign(
            Constants.SAML_RESPONSE + EQUAL + urlEncodedresponse + AMPERSAND + Constants.SIG_ALG + EQUAL
                    + URLEncoder.encode(
                            (String) request.getSession().getServletContext().getAttribute(Constants.SIG_ALG),
                            Constants.CHARACTER_ENCODING),
            (PrivateKey) request.getSession().getServletContext().getAttribute(Constants.SP_PRIVATE_KEY),
            (String) request.getSession().getServletContext().getAttribute(Constants.ALGORITHM),
            isDebugEnabled);

    // URL-encode the signature
    String urlEncodedSignature = URLEncoder.encode(signature, Constants.CHARACTER_ENCODING);

    if (isDebugEnabled)
        nc.notice("Final URL: "
                + (String) request.getSession().getServletContext().getAttribute(Constants.IDP_ENDPOINT)
                + QUESTION_MARK + Constants.SAML_RESPONSE + EQUAL + urlEncodedresponse + AMPERSAND
                + Constants.SIG_ALG + EQUAL
                + URLEncoder.encode(
                        (String) request.getSession().getServletContext().getAttribute(Constants.SIG_ALG),
                        Constants.CHARACTER_ENCODING)
                + AMPERSAND + SIGNATURE + EQUAL + urlEncodedSignature);

    // Constructs the final URL
    // https://endpoint/?SAMLResponse=value&SigAlg=value&Signature=value
    return (String) request.getSession().getServletContext().getAttribute(Constants.IDP_ENDPOINT)
            + QUESTION_MARK + Constants.SAML_RESPONSE + EQUAL + urlEncodedresponse + AMPERSAND
            + Constants.SIG_ALG + EQUAL
            + URLEncoder.encode(
                    (String) request.getSession().getServletContext().getAttribute(Constants.SIG_ALG),
                    Constants.CHARACTER_ENCODING)
            + AMPERSAND + SIGNATURE + EQUAL + urlEncodedSignature;
}