我创建了一个webapp,我正在修改url中的参数来解密其中一个参数。解密后,我想将参数附加到新的URL,以便我可以获得预期的响应。我创建了一个收集输入的HTML,登陆页面是我正在拦截请求的Java页面。我不希望请求转到目标网页,而是希望将其发送到新网址。
下面是我的过滤器代码。
package com.test.secondServlet;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* Servlet Filter implementation class SecondFilter
*/
@WebFilter("/SecondFilter")
public class SecondFilter implements Filter {
public String decryptedString;
private static final String characterEncoding = "UTF-8";
Properties prop = new Properties();
InputStream input = null;
public String globalKey;
public String RedirectURL;
public String DN;
public String TYPE;
public String NAME;
public String NUMBER;
/**
* @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
*/
@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
//chain.doFilter(req, res);
HttpServletRequest request = (HttpServletRequest) req;
//String requestURI = request.getRequestURI();
//System.out.println(requestURI);
String url = ((HttpServletRequest)request).getRequestURL().toString() +"?"+request.getQueryString();
System.out.println(url);
String queryString = ((HttpServletRequest)request).getQueryString();
DN = ((HttpServletRequest) request).getParameter("DN");
TYPE = ((HttpServletRequest) request).getParameter("TYPE");
NAME = ((HttpServletRequest) request).getParameter("NAME");
NUMBER = ((HttpServletRequest) request).getParameter("encryptedssn");
// String key = ((HttpServletRequest) request).getParameter("aeskey");
//Code to load key from property file
try {
String filename = "Secretkey.properties";
input = SecondFilter.class.getClassLoader().getResourceAsStream(filename);
if(input==null){
System.out.println("Sorry, unable to find " + filename);
return;
}
//load a properties file from class path, inside static method
prop.load(input);
//get the property value and print it out
globalKey=prop.getProperty("Secretkey");
System.out.println("Global Key : "+globalKey);
} catch (IOException ex) {
ex.printStackTrace();
}
//code ends
if (queryString!=null) {
//decryptingssnhere
byte[] clearText;
try {
clearText = AesEncryption.decryptBase64EncodedWithManagedIV(NUMBER, globalKey);
decryptedString = new String(clearText,characterEncoding);
System.out.println("ClearText:" +decryptedString);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// System.out.println("ClearText:" + new String(clearText,characterEncoding));
//if(url.startsWith("/SSNdecrypt/SSNdecrypt")){
System.out.println("queryString :"+queryString);
System.out.println("DN : "+DN);
System.out.println("TYPE : "+TYPE);
System.out.println("NAME : "+NAME);
System.out.println("S1/ NUMBER : "+NUMBER);
//System.out.println("key : "+key);
System.out.println("decrypted S1/ Number : "+decryptedString);
//String toReplace = url.substring(38);
System.out.println("input : "+input);
//String Redirect = "SSNdecrypt?"+queryString.substring(0,13)+decryptedString;
//System.out.println("redirect : "+Redirect);
//String newURI = url.replace(queryString, Redirect);
//System.out.println("new URL"+newURI);
//String newURI = url.concat("?"+newquerystring);
RedirectURL = "http://differentservername.com/contentexplorer/servlet/VipDms?";
String Redirect =RedirectURL+"DN="+DN+"&TYPE="+TYPE+"&NAME="+NAME+"&-NUMBER="+decryptedString;
System.out.println("redircting new URL for DOCVIEW : "+Redirect);
final HttpServletRequestWrapper wrapped = new HttpServletRequestWrapper(request) {
@Override
public StringBuffer getRequestURL() {
final StringBuffer originalUrl = ((HttpServletRequest) getRequest()).getRequestURL();
return new StringBuffer(Redirect);
}
};
//chain.doFilter(wrapped, res);
wrapped.getRequestDispatcher(Redirect).forward(wrapped, res);
} else {
chain.doFilter(req, res);
}
//SSNdecrypt/SSNdecrypt?encryptedssn=1234
//res.getWriter().println(requestURI.toString());
/**if (request instanceof HttpServletRequest) {
String url = ((HttpServletRequest)request).getRequestURL().toString();
String queryString = ((HttpServletRequest)request).getQueryString();
}**/
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
}
我不想使用urlrewriter并希望在同一个过滤器中执行此操作。如何更改请求的URL?
答案 0 :(得分:0)
我正在使用HttpServletResponse resp = (HttpServletResponse) res; resp.sendRedirect(Redirect);
而不是转发wrapped.getRequestDispatcher(Redirect).forward(wrapped, res);
的请求。我使用HTTP响应包装器而不是使用请求qrapper,而是能够从请求URL修改完整的URL。