DefaultLoginResultToView implements LoginResultToView { @Override public ModelAndView loginResultToView(ModelAndView mv, LoginResult result, HttpServletRequest request, HttpServletResponse response) { if(mv==null){ mv = new ModelAndView(); } if(result==null || request==null || response==null){ return mv; } if (result.isSuccess()) { Authentication authentication = result.getAuthentication(); request.getSession().removeAttribute(WebConstants.KI4SO_SERVICE_KEY_IN_SESSION); if (authentication != null && authentication.getAttributes() != null) { Map<String, Object> attributes = authentication.getAttributes(); if (attributes .get(AuthenticationPostHandler.KI4SO_SERVER_EC_KEY) != null) { response.addCookie(new Cookie( WebConstants.KI4SO_SERVER_ENCRYPTED_CREDENTIAL_COOKIE_KEY, attributes .get(AuthenticationPostHandler.KI4SO_SERVER_EC_KEY) .toString())); } if (attributes .get(AuthenticationPostHandler.KI4SO_CLIENT_EC_KEY) != null && !StringUtils.isEmpty(attributes.get(WebConstants.SERVICE_PARAM_NAME))) { mv.getModel().put("authentication", authentication); mv.setView(this .buildRedirectView( attributes.get(WebConstants.SERVICE_PARAM_NAME).toString(), attributes .get(AuthenticationPostHandler.KI4SO_CLIENT_EC_KEY) .toString())); return mv; } } mv.getModel().put("authentication", authentication); mv.setViewName("loginSucess"); } else { Cookie[] cookies = request.getCookies(); if(cookies!=null && cookies.length>0){ for(Cookie cookie:cookies){ if(WebConstants.KI4SO_SERVER_ENCRYPTED_CREDENTIAL_COOKIE_KEY.equals(cookie.getName())){ cookie.setMaxAge(0); response.addCookie(cookie); } } } mv.getModel().put("code", result.getCode()); mv.getModel().put("msg", MessageUtils.getMessage(result.getMsgKey())); } return mv; }  @Override ModelAndView loginResultToView(ModelAndView mv, LoginResult result, HttpServletRequest request,
			HttpServletResponse response);  }
@Test public void testLoginResultToView(){ Assert.assertNotNull(loginResultToView.loginResultToView(null, null, null, null)); Assert.assertNotNull(loginResultToView.loginResultToView(new ModelAndView(), null, null, null)); Assert.assertNotNull(loginResultToView.loginResultToView(new ModelAndView(), new LoginResult(), null, null)); Assert.assertNotNull(loginResultToView.loginResultToView(new ModelAndView(), new LoginResult(), new MockHttpServletRequest(), null)); ModelAndView mv = new ModelAndView(); MockHttpServletRequest request = new MockHttpServletRequest(); MockHttpServletResponse response = new MockHttpServletResponse(); LoginResult result = new LoginResult(); result.setSuccess(false); result.setCode(InvalidCredentialException.CODE); result.setMsgKey(InvalidCredentialException.MSG_KEY); request.setCookies(new Cookie(WebConstants.KI4SO_SERVER_ENCRYPTED_CREDENTIAL_COOKIE_KEY, "test")); ModelAndView mvResult = loginResultToView.loginResultToView(mv, result, request, response); Assert.assertNotNull(mvResult); Assert.assertEquals(InvalidCredentialException.CODE, mvResult.getModel().get("code")); Assert.assertEquals(MessageUtils.getMessage(InvalidCredentialException.MSG_KEY), mvResult.getModel().get("msg")); Assert.assertEquals("test", response.getCookie(WebConstants.KI4SO_SERVER_ENCRYPTED_CREDENTIAL_COOKIE_KEY).getValue()); mv = new ModelAndView(); request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); result = new LoginResult(); request.getSession().setAttribute(WebConstants.KI4SO_SERVICE_KEY_IN_SESSION, "service"); result.setSuccess(true); AuthenticationImpl authentication = new AuthenticationImpl(); Map<String, Object> attributes = new HashMap<String, Object>(); authentication.setAttributes(attributes); attributes.put(AuthenticationPostHandler.KI4SO_SERVER_EC_KEY, "ki4so-sever-key"); result.setAuthentication(authentication); mvResult = loginResultToView.loginResultToView(mv, result, request, response); Assert.assertNotNull(mvResult); Assert.assertNull(request.getSession().getAttribute(WebConstants.KI4SO_SERVICE_KEY_IN_SESSION)); Assert.assertEquals("loginSucess", mvResult.getViewName()); Assert.assertEquals(authentication, mvResult.getModel().get("authentication")); Assert.assertEquals("ki4so-sever-key", response.getCookie(WebConstants.KI4SO_SERVER_ENCRYPTED_CREDENTIAL_COOKIE_KEY).getValue()); mv = new ModelAndView(); request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); result = new LoginResult(); result.setSuccess(true); authentication = new AuthenticationImpl(); attributes = new HashMap<String, Object>(); authentication.setAttributes(attributes); attributes.put(AuthenticationPostHandler.KI4SO_SERVER_EC_KEY, "ki4so-sever-key"); attributes.put(AuthenticationPostHandler.KI4SO_CLIENT_EC_KEY, "ki4so-client-key"); attributes.put(WebConstants.SERVICE_PARAM_NAME, "htpp: result.setAuthentication(authentication); mvResult = loginResultToView.loginResultToView(mv, result, request, response); Assert.assertNotNull(mvResult); Assert.assertNull(request.getSession().getAttribute(WebConstants.KI4SO_SERVICE_KEY_IN_SESSION)); Assert.assertTrue(mvResult.getView() instanceof RedirectView); RedirectView view = (RedirectView)mvResult.getView(); Assert.assertEquals("htpp: Assert.assertEquals(authentication, mvResult.getModel().get("authentication")); Assert.assertEquals("ki4so-sever-key", response.getCookie(WebConstants.KI4SO_SERVER_ENCRYPTED_CREDENTIAL_COOKIE_KEY).getValue()); mv = new ModelAndView(); request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); result = new LoginResult(); result.setSuccess(true); authentication = new AuthenticationImpl(); attributes = new HashMap<String, Object>(); authentication.setAttributes(attributes); attributes.put(AuthenticationPostHandler.KI4SO_SERVER_EC_KEY, "ki4so-sever-key"); attributes.put(AuthenticationPostHandler.KI4SO_CLIENT_EC_KEY, "ki4so-client-key"); attributes.put(WebConstants.SERVICE_PARAM_NAME, "htpp: result.setAuthentication(authentication); mvResult = loginResultToView.loginResultToView(mv, result, request, response); Assert.assertNotNull(mvResult); Assert.assertNull(request.getSession().getAttribute(WebConstants.KI4SO_SERVICE_KEY_IN_SESSION)); Assert.assertTrue(mvResult.getView() instanceof RedirectView); view = (RedirectView)mvResult.getView(); Assert.assertEquals("htpp: Assert.assertEquals(authentication, mvResult.getModel().get("authentication")); Assert.assertEquals("ki4so-sever-key", response.getCookie(WebConstants.KI4SO_SERVER_ENCRYPTED_CREDENTIAL_COOKIE_KEY).getValue()); }