背景:需要开发的一个鉴权程序,在有权限的时候走正常逻辑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);
}
}