spring filter 修改request中的访问路径uri、参数以及异常处理

背景:需要开发的一个鉴权程序,在有权限的时候走正常逻辑filterChain.doFilter正常执行,在出现权限不足的时候需要返回前端异常信息,返回前端的时候需要给定固定的httpcode提示和json格式处理。如果使用throw new HttpResponseException(HttpStatus.UNAUTHORIZED.value(), resultDesc);返回前端是报错信息是500,所以自己写了一种解决方案。

如果不加filter 请求/sad/ 正常,加了filter之后请求/sad/会走到异常处理。
在使用的时候可以根据自己需要加判断条件是否切换uri。

如果有其他更好的解决方案烦请留言,讨论。谢谢。

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
        System.out.println(httpRequest.getRequestURI());
        String path=httpRequest.getRequestURI();
        if(true){
            path="/jackray/error/";
            System.out.println(path);
            httpRequest.getRequestDispatcher(path).forward(servletRequest,servletResponse);
        }
        else {
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }
@Controller
public class Test {
    @RequestMapping(value = "/sad/",method = RequestMethod.GET)
    @ResponseBody
    public String zhe(){
        return "sad";
    }
}

注意:method = RequestMethod.GET 编写error接口的时候不要添加请求方法,否则请求方法不一致导致报错405

@Controller
public class ErrorException {
    @RequestMapping(value = "/iam/error/" )
    @ResponseBody
    public ResponseEntity<?> iamError(){
        return new ResponseEntity<>("sad", HttpStatus.UNAUTHORIZED);
    }
}

如果想修改请求里面的参数可以

package com.jackray.swagger.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;


public class RequestParameterWrapper extends HttpServletRequestWrapper
{

    private Map<String, String[]> params = new HashMap<String, String[]>();

    public RequestParameterWrapper(HttpServletRequest request)
    {
        super(request);
        this.params.putAll(request.getParameterMap());
    }

    @Override
    public String getParameter(String name) {
        String[]values = params.get(name);
        if(values == null || values.length == 0) {
            return null;
        }
        return values[0];
    }

    public void addParameters(Map<String, Object> extraParams)
    {
        Iterator<Map.Entry<String, Object>> iterator = extraParams.entrySet().iterator();

        while (iterator.hasNext())
        {
            Map.Entry<String, Object> next = iterator.next();
            Object value = next.getValue();
            String name = next.getKey();

            if (value instanceof String[])
            {
                params.put(name, (String[]) value);
            }
            else if (value instanceof String)
            {
                params.put(name, new String[]{(String) value});
            }
            else
            {
                params.put(name, new String[]{String.valueOf(value)});
            }

        }


    }

}

通过RequestParameterWrapper 类实现参数的添加,或者修改。

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;
        System.out.println(httpRequest.getRequestURI());
        String path=httpRequest.getRequestURI();
        if(path.contains("/")){
            path="/iam/error/";
            System.out.println(path);
            Map<String, Object> extraParams = new HashMap<>();
            extraParams.put("resultDesc","sssss");
            RequestParameterWrapper requestParameterWrapper = new RequestParameterWrapper(httpRequest);
            requestParameterWrapper.addParameters(extraParams);
            httpRequest.getRequestDispatcher(path).forward(requestParameterWrapper,servletResponse);
        }
        else {
            filterChain.doFilter(servletRequest,servletResponse);
        }

    }

在controller层可以 通过getParameter 方法获取参数值 ,然后继续后续的相应的动作。

@Controller
public class ErrorException {

    @RequestMapping(value = "/iam/error/")
    @ResponseBody
    public ResponseEntity<?> iamError(HttpServletRequest request){
        String resultDesc = request.getParameter("resultDesc");
        return new ResponseEntity<>(resultDesc , HttpStatus.UNAUTHORIZED);
    }

}

参照地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值