Filter过滤器解密Url并且重定向和request.getRequestDispatcher).forward(request, response); 404 问题解决

        这两天在做项目的时候URL链接上的参数需要隐藏,后来就想到了加密的方式,前后端一起处理加解密,于是乎就写了个过滤器去处理,在demo中一切都很顺利,但是到项目中一直是404,后来发现是forward转发的Url链接不正确,到时候你们按需看用哪个操作吧。我这里写的demo就使用的是最简单的加密方式base64,你们可以按照自己的需求修改即可。且行且珍惜吧,希望后面自己再处理的时候少跳个坑。

第一,配置类,设置过滤器级别等

package com.diwork.intelliv.workbench.auth.config;

import com.diwork.intelliv.workbench.auth.filter.NccUrlFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.ErrorPageFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 过滤器配置设置
 * @author Administrator
 */
@Configuration
public class FilterConfig {

    @Bean
    public FilterRegistrationBean registFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new NccUrlFilter());
        registration.addUrlPatterns("/*");
        registration.setName("NccUrlFilter");
        registration.setOrder(0);
        return registration;
    }

    @Bean
    public ErrorPageFilter errorPageFilter() {
        return new ErrorPageFilter();
    }

    @Bean
    public FilterRegistrationBean disableSpringBootErrorFilter(ErrorPageFilter filter) {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(filter);
        filterRegistrationBean.setEnabled(false);
        return filterRegistrationBean;
    }

}

第二,过滤器类

package com.diwork.intelliv.workbench.auth.filter;


import com.diwork.intelliv.workbench.auth.util.Base64Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * NCC请求链接解密处理
 * @author Administrator
 */
public class NccUrlFilter implements Filter {
    private static final Logger log = LoggerFactory.getLogger(NccUrlFilter.class);

    private static final String NCCPARAM = "nccparam";



    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String nccparam=request.getParameter(NCCPARAM);
        //Ncc带参数nccparam的连接需要解密后转发
        if(StringUtils.isNotEmpty(nccparam)){
            String itemUrl=getNccUrl(request,nccparam);
            request.getRequestDispatcher(itemUrl).forward(request,response);
        }else{
            chain.doFilter(request, response);
        }
    }

    public String getNccUrl(HttpServletRequest request,String nccparam){
        String url1=request.getRequestURI();
        log.info("项目中出错的请求链接getRequestURI=={}",url1);
        String url=request.getServletPath();
        log.info("项目中正确的请求链接getServletPath=={}",url);
        String queryString =request.getQueryString();
        log.info("解密前请求参数集合=={}",queryString);
        String itemQueryParam=queryString.split(NCCPARAM)[0]+ Base64Util.decodeBase64(nccparam);
        log.info("解密后请求参数集合=={}",itemQueryParam);
        String itemUrl=url+"?"+itemQueryParam;
        log.info("目标请求链接=={}",itemUrl);
        return itemUrl;
    }
}

第三,解密工具类

package com.diwork.intelliv.workbench.auth.util;

import org.apache.tomcat.util.codec.binary.Base64;

/**
 * @className Base64Util
 * @author Administrator
 * @version 1.0
 */
public class Base64Util {
    // 加密
    public static String encodeBase64(String str) {
        String encodeBase64String = Base64.encodeBase64String(str.getBytes());
        return encodeBase64String;
    }

    // 解密
    public static String decodeBase64(String s) {
        byte[] decodeBase64 = Base64.decodeBase64(s);
        s=new String(decodeBase64);
        return s;
    }

    public static void main(String[] args) {
        String a ="token=123&cc=qq";
        String base64 = encodeBase64(a);
        System.out.println(base64);
        String fromBase64 = decodeBase64(base64);
        System.out.println(fromBase64);
    }
}

第四,自己写个测试类验证一下

package com.example.springboottest1.controller;

import com.example.springboottest1.annotations.Permission;
import com.example.springboottest1.annotations.UrlSecret;
import com.example.springboottest1.entity.BaseVO;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */
@RestController
public class HelloWord {
    //@Permission
    // 注解在方法上,并传递了encryptStrName自己定义的加密字符串名称encryptJson
    //@UrlSecret(value = BaseVO.class,encryptStrName = "encryptJson")
    @RequestMapping("/hello")
    public String getHello(@RequestBody BaseVO dept){
        return "hello world==="+dept.getEncryptStr();
    }

    @RequestMapping("/hello1")
    public String getHello1(@RequestParam String token){
        return "hello world==="+token;
    }
}

第五,使用postman进行接口测试验证

Filter过滤器解密Url并且重定向和request.getRequestDispatcher).forward(request, response); 404 问题解决_第1张图片

总结:之前一直404的原因在request.getRequestURI()和request.getServletPath(),需要看一下你们项目中使用的是哪个,我这边最后选择了后者,就可以正常访问了,选择前者一直是404,就是路径不对。

你可能感兴趣的:(spring,springboot,java技术,forward404问题,url链接过滤器问题,url解密转发处理)