springboot过滤token,并返回结果及异常处理操作
1.springboot 处理过滤token,并且返回结果
import org.apachemons.lang3.StringUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
@Component
public class ECInterceptor implements HandlerInterceptor {
/**
* @Description 在业务处理器处理请求之前被调⽤。预处理,可以进⾏编码、安全控制等处理;
* @Date 2019/5/14 16:04
* @Version 1.0
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.RequestURL()+"===========preHandle===========");
String token = Parameter("token");
if(StringUtils.isNotEmpty(token)){
Subject subject = Subject(token);
if(subject != null && subject.isAuthenticated()){
return true;
} else{
//返回校验token结果
returnJson(response);
// return false; //我做的时候返回数据后忘记return false了,所以导致异常
}
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
System.out.ContextPath()+"============postHandle==========");
}
/**
* @Description 在DispatcherServlet完全处理完请求后被调⽤,也就是说视图渲染已经完毕或者调⽤者已经拿到结果
* @Date 2019/5/14 16:05
* @Version 1.0
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
System.out.ContextPath()+"============afterCompletion==========");
}
private void returnJson(HttpServletResponse response){
PrintWriter writer = null;
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
try {
writer = Writer();
Map<String, Object> result = urnJson(400, "⽤户令牌token⽆效");
result.put("data", null);
writer.print(result);
} catch (IOException e){
LoggerUtil.logError(ECInterceptor.class, "输出流异常"+e);
} finally {
if(writer != null){
writer.close();
}
}
}
}
2.java.lang.IllegalStateException: getWriter() has already been called for this response异常
我再做处理response数据后忘记return false返回,导致被调⽤两次,报java.lang.IllegalStateException: getWriter() has already been called for this response异常,犯这样低级错误花了很长
时间才解决,谨记
[2019-05-27 09:27:17.690] [http-nio-8080-exec-1 ] [ERROR] [C.[.[.[.[dispatcherServlet] :175 ] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalSta java.lang.IllegalStateException: getWriter() has already been called for this response
at org.OutputStream(Response.java:548)
at org.OutputStream(ResponseFacade.java:210)
at javax.OutputStream(ServletResponseWrapper.java:105)
at org.springframework.http.Body(ServletServerHttpResponse.java:83)
at org.verter.json.AbstractJackson2HttpMessageConverter.writeInternal(AbstractJackson2HttpMessageConverter.java:255)
at org.verter.AbstractGenericHttpMessageConverter.write(AbstractGenericHttpMessageConverter.java:103)
at org.springframework.web.hod.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:290)
at org.springframework.web.hod.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:180)
at urnvalue.HandlerMethodReturnValueHandlerProxy.handleReturnValue(HandlerMethodReturnValueHandlerProxy.java:40)
at org.hod.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
at org.springframework.web.hod.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:119)
at org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:892)
at org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797)
at org.springframework.web.hod.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1038)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:882)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at at.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.shiro.web.uteChain(AbstractShiroFilter.java:449)
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
at org.apache.shiro.subject.ute(DelegatingSubject.java:387)
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at fig.filter.ECWebFilter.doFilter(ECWebFilter.java:24)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
at org.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.tor.CoyoteAdapter.service(CoyoteAdapter.java:343)
at http11.Http11Processor.service(Http11Processor.java:408)
at AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:834)
at at.util.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at at.util.SocketProcessorBase.run(SocketProcessorBase.java:49)
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at at.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
补充知识:springboot 过滤权限和错误处理
先说异常处理,增加⼀个异常处理的类MyControllerAdvice就可以了,不需要其他地⽅使⽤,注意使⽤注解@ControllerAdvice
@ControllerAdvice
public class MyControllerAdvice {
@Resource
GetRootPath getRootPath;
private static final Logger logger = Logger(MyControllerAdvice.class);
public void output(Object json, HttpServletRequest request, HttpServletResponse response) throws IOException {
String header = Header("Origin");
response.setContentType("application/json;charset=UTF-8;");
PrintWriter out = Writer();
out.println(json);
out.flush();
out.close();
}
/***
* 404处理
* @param e
* @return
*/
@ExceptionHandler(NoHandlerFoundException.class)
public void notFountHandler(HttpServletRequest request, HttpServletResponse response, Model model, NoHandlerFoundException e) throws IOException, JSONException {
JSONObject json = new JSONObject();
json.put("code", 500);
json.put("content", null);
json.put("msg", "未到路径:"+ServletPath());
output(json,request,response);
}
/**
* 运⾏时异常
*
* @param exception
* @return
*/
@ExceptionHandler({RuntimeException.class})
@ResponseStatus(HttpStatus.OK)
public void processException(HttpServletRequest request, HttpServletResponse response, Model model, RuntimeException exception) throws JSONException, IOException {
JSONObject json = new JSONObject();
json.put("code", 500);
json.put("content", null);
json.put("msg", Message());
output(json,request,response);
}
/**
* Excepiton异常
*
* @param exception
* @return
*/
@ExceptionHandler({Exception.class})
@ResponseStatus(HttpStatus.OK)
public void processException(HttpServletRequest request, HttpServletResponse response, Model model, Exception exception) throws JSONException, IOException {
JSONObject json = new JSONObject();
json.put("code", 500);
json.put("content", null);
json.put("msg", Message());
output(json,request,response);
}
}
再来权限验证的,接⼿的项⽬框架中只有验证是否登录的,没有验收权限的,增加⼀个类WebMvcConfig,注意使⽤注解@Configuration,不需要在其他地⽅引⽤即可起作⽤,并注意使⽤excludePathPatterns去掉不需要拦截的部分,否则会拦截掉静态资源。
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
/**
* 添加
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
//静态资源不拦截
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**");
}
}
增加类LoginInterceptor
public class LoginInterceptor extends HandlerInterceptorAdapter {
/**
* 检查是否已经登录
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Object username = Session().getAttribute(Constants.LOGIN_USER);
String servletPath = ServletPath();
String type = Header("X-Requested-With") == null ? "" : Header("X-Requested-With");
if (username != null) {
//检查页⾯访问的权限
if (!"XMLHttpRequest".equals(type)) {
int userId = Integer.Session().getAttribute(Constants.LOGIN_USERID).toString());
List<ModuleEntity> moduleList = (List<ModuleEntity>) Session().getAttribute(Constants.USER_MODULE);
boolean chkResult = methodPermissionLimit(moduleList, servletPath);
if(!chkResult){
JSONObject outputMSg = new JSONObject();
outputMSg.put("code", 500);
outputMSg.put("content", "");
outputMSg.put("msg", "没有权限");
output(outputMSg, request, response);
return false;
write的返回值}else{
return true;
}
} else {
//如果是json访问,则不做检查
return true;
}
} else {
//检查是否登录允许
if (methodLoginLimit(handler)) {
return true;
} else {
if ("XMLHttpRequest".equals(type)) {
JSONObject outputMSg = new JSONObject();
outputMSg.put("code", 500);
outputMSg.put("content", "");
outputMSg.put("msg", "登录过期,请重新登陆");
output(outputMSg, request, response);
return false;
} else {
String redirectUrl = ContextPath() + "/login";
response.sendRedirect(redirectUrl);
return false;
}
}
}
}
public boolean methodLoginLimit(Object handler) {
HandlerMethod method = (HandlerMethod) handler;
//获取当前⽅法PermessionLimit
LoginLimit loginLimit = MethodAnnotation(LoginLimit.class);
if (loginLimit == null) {
//获取控制器的PermessionLimit
loginLimit = Method().getDeclaringClass().getAnnotation(LoginLimit.class);
}
if (loginLimit != null && !loginLimit.limit()) {
return true;
} else {
return false;
}
}
/**
* 检查权限
*
* @param moduleList
* @param path
* @return
*/
public boolean methodPermissionLimit(List<ModuleEntity> moduleList, String path) {
boolean havePermission = moduleList.stream().anyMatch(f-&Path().toLowerCase().LowerCase()));
return havePermission;
}
public void output(Object json, HttpServletRequest request, HttpServletResponse response) throws IOException {
String header = Header("Origin");
response.setContentType("application/json;charset=UTF-8;");
PrintWriter out = Writer();
out.println(json);
out.flush();
out.close();
}
}
这样的只对页⾯地址进⾏拦截,对ajax提交的数据不做处理,做普通项⽬的权限过滤是可以了。
以上这篇springboot过滤token,并返回结果及异常处理操作就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论