本篇为系列文,不以目的为目的,以学习为目的。旨在学习,重在研究。
创建AccountWrapper继承HttpServletRequestWrapper
HttpServletRequestWrapper介绍
-
package ;
-
-
import ;
-
import ;
-
import ;
-
import ;
-
import ;
-
-
import ;
-
import ;
-
import ;
-
import ;
-
-
import ;
-
-
/**
-
* @Description: Account--Request--请求包装类,解决json只能接受一次的问题
-
*/
-
public class AccountWrapper extends HttpServletRequestWrapper{
-
-
private byte[] requestBody;
-
-
public AccountWrapper(HttpServletRequest request) throws IOException {
-
super(request);
-
requestBody = (());
-
}
-
-
@Override
-
public BufferedReader getReader() throws IOException {
-
return new BufferedReader(new InputStreamReader(getInputStream(),StandardCharsets.UTF_8));
-
}
-
-
@Override
-
public ServletInputStream getInputStream() throws IOException {
-
if(requestBody == null){
-
requestBody = new byte[0];
-
}
-
final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);
-
return new ServletInputStream() {
-
-
@Override
-
public int read() throws IOException {
-
return ();
-
}
-
-
@Override
-
public void setReadListener(ReadListener readListener) {
-
-
}
-
-
@Override
-
public boolean isReady() {
-
return false;
-
}
-
-
@Override
-
public boolean isFinished() {
-
return false;
-
}
-
};
-
}
-
}
-
修改preHandle的代码
-
@Override
-
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
-
throws Exception {
-
try{
-
AccountInfo accountInfo = (request);
-
//省略业务判断逻辑代码
-
}catch (Exception e) {
-
return false;
-
}
-
}
-
-
/**
-
*@Description: 解析request里的AccountInfo
-
*/
-
public AccountInfo getParam(HttpServletRequest request) throws IOException, SSOException {
-
String jsonString = null;
-
String submitMethod = ();
-
if("GET".equals(submitMethod) || ("get")){
-
jsonString = getParamStringByGet(request);
-
}else if(("POST") || ("post")){
-
jsonString = getParamJsonStringByPost(request);
-
}else{
-
("GetRequestJsonUtil解析Request参数错误--"+submitMethod);
-
}
-
("解析出来的参数--getParam:{}",jsonString);
-
if((jsonString)){
-
throw new SSOException(new ReturnMsg(EC.RTF_FALSE,EC.ERR_PARAM_NULL_CODE,EC.ERR_PARAM_NULL_MSG));
-
}
-
AccountInfo parse = (jsonString, );
-
return parse;
-
}
-
-
/**
-
*@Description: 解析post方式提交的json数据
-
*/
-
private String getParamJsonStringByPost(HttpServletRequest request) throws IOException {
-
//这里修改了
-
AccountWrapper aWrapper = new AccountWrapper(request);
-
int contentLength = ();
-
if(contentLength<0){
-
return null;
-
}
-
byte buffer[] = new byte[contentLength];
-
for (int i = 0; i < contentLength; i++) {
-
int readlen = ().read(buffer,i,contentLength -i);
-
if(readlen == -1){
-
break;
-
}
-
i+=readlen;
-
}
-
return new String(buffer,"UTF-8");
-
}
-
-
/**
-
*@Description: 解析get方式提交的request参数
-
*/
-
private String getParamStringByGet(HttpServletRequest request) throws IOException {
-
//这里修改了
-
AccountWrapper aWrapper = new AccountWrapper(request);
-
return new String(().getBytes("ios-8859-1"),"utf-8").replaceAll("%22", "\"");
-
}
这样并没有解决问题,因为request中的数据在复制给AccountWrapper 时读取过了(调用了方法),所以要用AccountWrapper 代替request传到controller中。
这样就需要用过滤器配合
配置过滤器AccountFilter
-
package ;
-
-
import ;
-
-
import ;
-
import ;
-
import ;
-
import ;
-
import ;
-
import ;
-
import ;
-
-
import org.;
-
import org.;
-
-
import ;
-
-
/**
-
* @Description: 过滤器,用于controller可以获取json
-
*/
-
-
public class AccountFilter implements Filter{
-
-
Logger logger = ();
-
@Override
-
public void init(FilterConfig filterConfig) throws ServletException {
-
}
-
-
@Override
-
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
-
throws IOException, ServletException {
-
AccountWrapper accountWrapper = null;
-
("AccountFilter--doFilter");
-
if(request instanceof HttpServletRequest){
-
accountWrapper = new AccountWrapper((HttpServletRequest)request);
-
}
-
if(accountWrapper == null){
-
(request, response);
-
}else {
-
(accountWrapper, response);//在这里将AccountWrapper代替request去了controller
-
}
-
}
-
-
@Override
-
public void destroy() {
-
-
}
-
-
}
配置过滤器
-
<filter>
-
<filter-name>ssoAccountFilter</filter-name>
-
<filter-class></filter-class>
-
</filter>
-
<filter-mapping>
-
<filter-name>ssoAccountFilter</filter-name>
-
<url-pattern>/sso/account/*</url-pattern>
-
</filter-mapping>
spring-mvc注册拦截器 (如果在第一篇文章时配置了就不需要了)
-
<!-- 注册拦截器 -->
-
<!--拦截器-->
-
<mvc:interceptors>
-
<mvc:interceptor>
-
<mvc:mapping path="/sso/account/**" /> <! --拦截路径 -- >
-
<bean class=""></bean>
-
</mvc:interceptor>
-
</mvc:interceptors>
大功告成