/** * Method for presenting the default view. * * @param request * @return */ @RequestMapping(value = "/", method = RequestMethod.GET) public ModelAndView welcomePage(HttpServletRequest request) { HttpSession session = request.getSession(); // get allowed flag from session Boolean allowed = false; Object allowedObject = session.getAttribute(Constants.SESSION_ATTRIBUTE_ALLOWED); if (allowedObject != null && allowedObject instanceof Boolean) { allowed = (Boolean) allowedObject; } // get username from session String username = null; Object usernameObject = session.getAttribute(Constants.SESSION_ATTRIBUTE_USERNAME); if (usernameObject != null && usernameObject instanceof String) { username = (String) usernameObject; } // create model and view ModelAndView model = new ModelAndView(); model.addObject(Constants.APP_NAME, mavenProperties.get(Constants.APP_NAME)); model.addObject(Constants.APP_VERSION, mavenProperties.get(Constants.APP_VERSION)); model.addObject(Constants.SESSION_ATTRIBUTE_ALLOWED, allowed); model.addObject(Constants.SESSION_ATTRIBUTE_USERNAME, username); model.addObject(Constants.GOOGLE_AUTH_ENABLED, googleAuthenticatorService.isEnabled()); model.setViewName("index"); return model; }
/** * Method for user login. * * @param username * @param password * @param code * @param request * @param response * @return * @throws IOException */ @RequestMapping(value = "/login", method = RequestMethod.POST) public ModelAndView login( @RequestParam(value = "username", required = true) String username, @RequestParam(value = "password", required = true) String password, @RequestParam(value = "code", required = false) String code, HttpServletRequest request, HttpServletResponse response) throws IOException { boolean allowed = false; try { // check if user is allowed to use the application allowed = restProxyServlet.login(username, password) && (googleAuthenticatorService.isEnabled() ? googleAuthenticatorService.check(username, code) : true); // allowed -> set necessary session attributes if (allowed) { HttpSession session = request.getSession(); session.setAttribute(Constants.SESSION_ATTRIBUTE_ALLOWED, true); session.setAttribute(Constants.SESSION_ATTRIBUTE_USERNAME, username); session.setAttribute(Constants.SESSION_ATTRIBUTE_PASSWORD, password); } } catch (Exception e) { e.printStackTrace(); } // not allowed -> send http status 403 if (!allowed) { response.sendError(HttpServletResponse.SC_FORBIDDEN); } return null; }