SSM拦截器校验JSON数据(3) -- 解决拦截器获取json后,controller参数为空

时间:2024-10-12 19:35:33

本篇为系列文,不以目的为目的,以学习为目的。旨在学习,重在研究。

创建AccountWrapper继承HttpServletRequestWrapper

HttpServletRequestWrapper介绍

  1. package ;
  2. import ;
  3. import ;
  4. import ;
  5. import ;
  6. import ;
  7. import ;
  8. import ;
  9. import ;
  10. import ;
  11. import ;
  12. /**
  13. * @Description: Account--Request--请求包装类,解决json只能接受一次的问题
  14. */
  15. public class AccountWrapper extends HttpServletRequestWrapper{
  16. private byte[] requestBody;
  17. public AccountWrapper(HttpServletRequest request) throws IOException {
  18. super(request);
  19. requestBody = (());
  20. }
  21. @Override
  22. public BufferedReader getReader() throws IOException {
  23. return new BufferedReader(new InputStreamReader(getInputStream(),StandardCharsets.UTF_8));
  24. }
  25. @Override
  26. public ServletInputStream getInputStream() throws IOException {
  27. if(requestBody == null){
  28. requestBody = new byte[0];
  29. }
  30. final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody);
  31. return new ServletInputStream() {
  32. @Override
  33. public int read() throws IOException {
  34. return ();
  35. }
  36. @Override
  37. public void setReadListener(ReadListener readListener) {
  38. }
  39. @Override
  40. public boolean isReady() {
  41. return false;
  42. }
  43. @Override
  44. public boolean isFinished() {
  45. return false;
  46. }
  47. };
  48. }
  49. }

修改preHandle的代码

  1. @Override
  2. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
  3. throws Exception {
  4. try{
  5. AccountInfo accountInfo = (request);
  6. //省略业务判断逻辑代码
  7. }catch (Exception e) {
  8. return false;
  9. }
  10. }
  11. /**
  12. *@Description: 解析request里的AccountInfo
  13. */
  14. public AccountInfo getParam(HttpServletRequest request) throws IOException, SSOException {
  15. String jsonString = null;
  16. String submitMethod = ();
  17. if("GET".equals(submitMethod) || ("get")){
  18. jsonString = getParamStringByGet(request);
  19. }else if(("POST") || ("post")){
  20. jsonString = getParamJsonStringByPost(request);
  21. }else{
  22. ("GetRequestJsonUtil解析Request参数错误--"+submitMethod);
  23. }
  24. ("解析出来的参数--getParam:{}",jsonString);
  25. if((jsonString)){
  26. throw new SSOException(new ReturnMsg(EC.RTF_FALSE,EC.ERR_PARAM_NULL_CODE,EC.ERR_PARAM_NULL_MSG));
  27. }
  28. AccountInfo parse = (jsonString, );
  29. return parse;
  30. }
  31. /**
  32. *@Description: 解析post方式提交的json数据
  33. */
  34. private String getParamJsonStringByPost(HttpServletRequest request) throws IOException {
  35. //这里修改了
  36. AccountWrapper aWrapper = new AccountWrapper(request);
  37. int contentLength = ();
  38. if(contentLength<0){
  39. return null;
  40. }
  41. byte buffer[] = new byte[contentLength];
  42. for (int i = 0; i < contentLength; i++) {
  43. int readlen = ().read(buffer,i,contentLength -i);
  44. if(readlen == -1){
  45. break;
  46. }
  47. i+=readlen;
  48. }
  49. return new String(buffer,"UTF-8");
  50. }
  51. /**
  52. *@Description: 解析get方式提交的request参数
  53. */
  54. private String getParamStringByGet(HttpServletRequest request) throws IOException {
  55. //这里修改了
  56. AccountWrapper aWrapper = new AccountWrapper(request);
  57. return new String(().getBytes("ios-8859-1"),"utf-8").replaceAll("%22", "\"");
  58. }

这样并没有解决问题,因为request中的数据在复制给AccountWrapper 时读取过了(调用了方法),所以要用AccountWrapper 代替request传到controller中。

这样就需要用过滤器配合

配置过滤器AccountFilter

  1. package ;
  2. import ;
  3. import ;
  4. import ;
  5. import ;
  6. import ;
  7. import ;
  8. import ;
  9. import ;
  10. import org.;
  11. import org.;
  12. import ;
  13. /**
  14. * @Description: 过滤器,用于controller可以获取json
  15. */
  16. public class AccountFilter implements Filter{
  17. Logger logger = ();
  18. @Override
  19. public void init(FilterConfig filterConfig) throws ServletException {
  20. }
  21. @Override
  22. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  23. throws IOException, ServletException {
  24. AccountWrapper accountWrapper = null;
  25. ("AccountFilter--doFilter");
  26. if(request instanceof HttpServletRequest){
  27. accountWrapper = new AccountWrapper((HttpServletRequest)request);
  28. }
  29. if(accountWrapper == null){
  30. (request, response);
  31. }else {
  32. (accountWrapper, response);//在这里将AccountWrapper代替request去了controller
  33. }
  34. }
  35. @Override
  36. public void destroy() {
  37. }
  38. }

配置过滤器

  1. <filter>
  2. <filter-name>ssoAccountFilter</filter-name>
  3. <filter-class></filter-class>
  4. </filter>
  5. <filter-mapping>
  6. <filter-name>ssoAccountFilter</filter-name>
  7. <url-pattern>/sso/account/*</url-pattern>
  8. </filter-mapping>

spring-mvc注册拦截器 (如果在第一篇文章时配置了就不需要了)

  1. <!-- 注册拦截器 -->
  2. <!--拦截器-->
  3. <mvc:interceptors>
  4. <mvc:interceptor>
  5. <mvc:mapping path="/sso/account/**" /> <! --拦截路径 -- >
  6. <bean class=""></bean>
  7. </mvc:interceptor>
  8. </mvc:interceptors>

 

大功告成