@Test public void testPreHandleValidTokenOnCheck() throws Exception { HttpSessionTransactionTokenStore tokenStore = new HttpSessionTransactionTokenStore(); TransactionToken inputToken = new TransactionToken("testTokenAttr", "111", "222"); tokenStore.store(inputToken); assertThat(tokenStore.getSession().getAttribute(tokenStore .createSessionAttributeName(inputToken)), is(notNullValue())); request.setParameter( TransactionTokenInterceptor.TOKEN_REQUEST_PARAMETER, "testTokenAttr~111~222"); interceptor = new TransactionTokenInterceptor(new TokenStringGenerator(), new TransactionTokenInfoStore(), tokenStore); boolean result = interceptor.preHandle(request, response, new HandlerMethod(new TransactionTokenSampleController(), TransactionTokenSampleController.class .getDeclaredMethod("fifth", SampleForm.class, Model.class))); assertThat(result, is(true)); TransactionTokenContext transactionTokenCtx = (TransactionTokenContext) request .getAttribute( TransactionTokenInterceptor.TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME); TransactionToken token = transactionTokenCtx.getReceivedToken(); assertThat(token, is(notNullValue())); assertThat(token.getTokenName(), is("testTokenAttr")); assertThat(token.getTokenKey(), is("111")); assertThat(token.getTokenValue(), is("222")); }
@Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } logger.trace("preHandle"); HandlerMethod handlerMethod = (HandlerMethod) handler; TransactionTokenInfo tokenInfo = tokenInfoStore.getTransactionTokenInfo( handlerMethod); TransactionToken receivedToken = INVALID_TOKEN; if (tokenInfo.needValidate()) { receivedToken = createReceivedToken(request); if (!validateToken(receivedToken, tokenStore, tokenInfo)) { processTransactionTokenError(receivedToken); } } if (tokenInfo.getTransactionTokenType() == TransactionTokenType.BEGIN) { String tokenStr = request.getParameter(TOKEN_REQUEST_PARAMETER); if (null != tokenStr) { removeToken(new TransactionToken(tokenStr)); } } TransactionTokenContextImpl tokenContext = new TransactionTokenContextImpl(tokenInfo, receivedToken); request.setAttribute(TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME, tokenContext); return true; }
TransactionTokenInterceptor implements HandlerInterceptor { @Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } logger.trace("preHandle"); HandlerMethod handlerMethod = (HandlerMethod) handler; TransactionTokenInfo tokenInfo = tokenInfoStore.getTransactionTokenInfo( handlerMethod); TransactionToken receivedToken = INVALID_TOKEN; if (tokenInfo.needValidate()) { receivedToken = createReceivedToken(request); if (!validateToken(receivedToken, tokenStore, tokenInfo)) { processTransactionTokenError(receivedToken); } } if (tokenInfo.getTransactionTokenType() == TransactionTokenType.BEGIN) { String tokenStr = request.getParameter(TOKEN_REQUEST_PARAMETER); if (null != tokenStr) { removeToken(new TransactionToken(tokenStr)); } } TransactionTokenContextImpl tokenContext = new TransactionTokenContextImpl(tokenInfo, receivedToken); request.setAttribute(TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME, tokenContext); return true; } }
TransactionTokenInterceptor implements HandlerInterceptor { @Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } logger.trace("preHandle"); HandlerMethod handlerMethod = (HandlerMethod) handler; TransactionTokenInfo tokenInfo = tokenInfoStore.getTransactionTokenInfo( handlerMethod); TransactionToken receivedToken = INVALID_TOKEN; if (tokenInfo.needValidate()) { receivedToken = createReceivedToken(request); if (!validateToken(receivedToken, tokenStore, tokenInfo)) { processTransactionTokenError(receivedToken); } } if (tokenInfo.getTransactionTokenType() == TransactionTokenType.BEGIN) { String tokenStr = request.getParameter(TOKEN_REQUEST_PARAMETER); if (null != tokenStr) { removeToken(new TransactionToken(tokenStr)); } } TransactionTokenContextImpl tokenContext = new TransactionTokenContextImpl(tokenInfo, receivedToken); request.setAttribute(TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME, tokenContext); return true; } TransactionTokenInterceptor();  TransactionTokenInterceptor(int sizePerTokenName);  TransactionTokenInterceptor(TokenStringGenerator generator,
            TransactionTokenInfoStore tokenInfoStore,
            TransactionTokenStore tokenStore); }
TransactionTokenInterceptor implements HandlerInterceptor { @Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } logger.trace("preHandle"); HandlerMethod handlerMethod = (HandlerMethod) handler; TransactionTokenInfo tokenInfo = tokenInfoStore.getTransactionTokenInfo( handlerMethod); TransactionToken receivedToken = INVALID_TOKEN; if (tokenInfo.needValidate()) { receivedToken = createReceivedToken(request); if (!validateToken(receivedToken, tokenStore, tokenInfo)) { processTransactionTokenError(receivedToken); } } if (tokenInfo.getTransactionTokenType() == TransactionTokenType.BEGIN) { String tokenStr = request.getParameter(TOKEN_REQUEST_PARAMETER); if (null != tokenStr) { removeToken(new TransactionToken(tokenStr)); } } TransactionTokenContextImpl tokenContext = new TransactionTokenContextImpl(tokenInfo, receivedToken); request.setAttribute(TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME, tokenContext); return true; } TransactionTokenInterceptor();  TransactionTokenInterceptor(int sizePerTokenName);  TransactionTokenInterceptor(TokenStringGenerator generator,
            TransactionTokenInfoStore tokenInfoStore,
            TransactionTokenStore tokenStore); @Override boolean preHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler); @Override void postHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final ModelAndView modelAndView); @Override void afterCompletion(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final Exception ex); }
TransactionTokenInterceptor implements HandlerInterceptor { @Override public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) { if (!(handler instanceof HandlerMethod)) { return true; } logger.trace("preHandle"); HandlerMethod handlerMethod = (HandlerMethod) handler; TransactionTokenInfo tokenInfo = tokenInfoStore.getTransactionTokenInfo( handlerMethod); TransactionToken receivedToken = INVALID_TOKEN; if (tokenInfo.needValidate()) { receivedToken = createReceivedToken(request); if (!validateToken(receivedToken, tokenStore, tokenInfo)) { processTransactionTokenError(receivedToken); } } if (tokenInfo.getTransactionTokenType() == TransactionTokenType.BEGIN) { String tokenStr = request.getParameter(TOKEN_REQUEST_PARAMETER); if (null != tokenStr) { removeToken(new TransactionToken(tokenStr)); } } TransactionTokenContextImpl tokenContext = new TransactionTokenContextImpl(tokenInfo, receivedToken); request.setAttribute(TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME, tokenContext); return true; } TransactionTokenInterceptor();  TransactionTokenInterceptor(int sizePerTokenName);  TransactionTokenInterceptor(TokenStringGenerator generator,
            TransactionTokenInfoStore tokenInfoStore,
            TransactionTokenStore tokenStore); @Override boolean preHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler); @Override void postHandle(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final ModelAndView modelAndView); @Override void afterCompletion(final HttpServletRequest request,
            final HttpServletResponse response, final Object handler,
            final Exception ex); static final String TOKEN_CONTEXT_REQUEST_ATTRIBUTE_NAME; static final String NEXT_TOKEN_REQUEST_ATTRIBUTE_NAME; static final String TOKEN_REQUEST_PARAMETER; }