[RESOLVIDO] Vraptor + Interceptor: Setar valor no request
3 respostas
fred.dobroes
Olá Pessoal,
estou interceptando os valores que os usuário enviam no form para retirar tags HTML e Script maliciosos, para isso criei o seguinte interceptor:
@InterceptspublicclassLogimplementsInterceptor{privatefinalHttpServletRequestrequest;publicLog(HttpServletRequestrequest){this.request=request;}publicbooleanaccepts(ResourceMethodarg0){returntrue;}publicvoidintercept(InterceptorStackstack,ResourceMethodmethod,ObjectresourceInstance)throwsInterceptionException{Enumerationparams=request.getParameterNames();ArrayList<String>lista=Collections.list(params);//cria uma lista com o nome de todos os parametrosfor(Stringparametro:lista){Stringinsegura=request.getParameter(parametro);//pega o paramentro do request.Stringsegura=Jsoup.clean(insegura,Whitelist.none());// o Jsoup faz todo o trabalho de filtrar e gerar a string segura request.setAttribute(parametro,segura);//e setado o novo atributo}stack.next(method,resourceInstance);}}
O problema é que por mais que eu altere o request, os parâmetros que chegam no meu controller estão iguais aos do request antes de passar pelo interceptor.
Creio que você precisará sobrescrever o ParametersInstantiatorInterceptor.
Aqui está ele na forma “natural”.
/*** * Copyright (c) 2009 Caelum - www.caelum.com.br/opensource * All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */packagebr.com.caelum.vraptor.interceptor;importjava.util.ArrayList;importjava.util.Enumeration;importjava.util.List;importjavax.servlet.http.HttpSession;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importbr.com.caelum.vraptor.InterceptionException;importbr.com.caelum.vraptor.Intercepts;importbr.com.caelum.vraptor.Lazy;importbr.com.caelum.vraptor.Validator;importbr.com.caelum.vraptor.core.InterceptorStack;importbr.com.caelum.vraptor.core.Localization;importbr.com.caelum.vraptor.core.MethodInfo;importbr.com.caelum.vraptor.http.MutableRequest;importbr.com.caelum.vraptor.http.ParametersProvider;importbr.com.caelum.vraptor.resource.ResourceMethod;importbr.com.caelum.vraptor.validator.Message;/** * An interceptor which instantiates parameters and provide them to the stack. * * @author Guilherme Silveira */@Intercepts(after=ResourceLookupInterceptor.class)@LazypublicclassParametersInstantiatorInterceptorimplementsInterceptor{privatefinalParametersProviderprovider;privatefinalMethodInfoparameters;privatestaticfinalLoggerlogger=LoggerFactory.getLogger(ParametersInstantiatorInterceptor.class);privatefinalValidatorvalidator;privatefinalLocalizationlocalization;privatefinalList<Message>errors=newArrayList<Message>();privatefinalHttpSessionsession;publicstaticfinalStringFLASH_PARAMETERS="_vraptor_flash_parameters";privatefinalMutableRequestrequest;publicParametersInstantiatorInterceptor(ParametersProviderprovider,MethodInfoparameters,Validatorvalidator,Localizationlocalization,HttpSessionsession,MutableRequestrequest){this.provider=provider;this.parameters=parameters;this.validator=validator;this.localization=localization;this.session=session;this.request=request;}publicbooleanaccepts(ResourceMethodmethod){returnmethod.getMethod().getParameterTypes().length>0;}publicvoidintercept(InterceptorStackstack,ResourceMethodmethod,ObjectresourceInstance)throwsInterceptionException{Enumeration<String>names=request.getParameterNames();while(names.hasMoreElements()){fixParameter(names.nextElement());}Object[]values=getParametersFor(method);validator.addAll(errors);if(!errors.isEmpty()){logger.debug("There are conversion errors: {}",errors);}logger.debug("Parameter values for {} are {}",method,values);parameters.setParameters(values);stack.next(method,resourceInstance);}privatevoidfixParameter(Stringname){if(name.contains(".class.")){thrownewIllegalArgumentException("Bug Exploit Attempt with parameter: "+name+"!!!");}if(name.contains("[]")){String[]values=request.getParameterValues(name);for(inti=0;i<values.length;i++){request.setParameter(name.replace("[]","["+i+"]"),values[i]);}}}privateObject[]getParametersFor(ResourceMethodmethod){Object[]args=(Object[])session.getAttribute(ParametersInstantiatorInterceptor.FLASH_PARAMETERS);if(args==null){returnprovider.getParametersFor(method,errors,localization.getBundle());}session.removeAttribute(ParametersInstantiatorInterceptor.FLASH_PARAMETERS);returnargs;}}
Lucas_Cavalcanti
Receba no construtor um MutableRequest ao invés de HttpServletRequest, e use um setParameter ao invés de setAttribute…
coloque também @Intercepts(before=ParameterInstantiatorInterceptor.class)
fred.dobroes
Lucas Cavalcanti:
Receba no construtor um MutableRequest ao invés de HttpServletRequest, e use um setParameter ao invés de setAttribute…
coloque também @Intercepts(before=ParameterInstantiatorInterceptor.class)