12.spring boot 拦截器 日志 权限
2017-05-03 本文已影响0人
会灰的大飞狼
2步:1个配置类,一个实现类.
用拦截器也能实现日志和权限过滤.下面是日志的拦截器,只是获取不了参数值.
package com.yunchuang.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author 尹冬飞
* @create 2017-05-03 9:02
*/
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
/**
* {@inheritDoc}
* <p>This implementation is empty.
*
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyInterceptor1()).addPathPatterns("/**");
registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");
super.addInterceptors(registry);
}
}
package com.yunchuang.config;
import com.yunchuang.Log;
import com.yunchuang.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author 尹冬飞
* @create 2017-05-03 8:59
*/
public class MyInterceptor1 implements HandlerInterceptor {
Logger logger = LoggerFactory.getLogger(MyInterceptor1.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler.getClass().isAssignableFrom(HandlerMethod.class)) {
HandlerMethod handler2 = (HandlerMethod) handler;
Log annotation = handler2.getMethodAnnotation(Log.class);
if (annotation != null) {
HttpSession session = request.getSession();
User user = (User) session.getAttribute("loginUser");
String name = user != null ? "[" + user.getName() + "]" : "";
logger.info(name + "[" + annotation.value() + "][" + handler2.getMethod().getDeclaringClass().getName() + "." + handler2.getMethod().getName() + "][" + request.getRemoteAddr() + "]");
}
}
logger.info("preHandle1");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
logger.info("postHandle1");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.info("afterCompletion1");
}
}
package com.yunchuang.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 尹冬飞
* @create 2017-05-03 9:00
*/
public class MyInterceptor2 implements HandlerInterceptor {
Logger logger = LoggerFactory.getLogger(MyInterceptor2.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
logger.info("preHandle2");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
logger.info("postHandle2");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
logger.info("afterCompletion2");
}
}
package com.yunchuang.web;
import com.yunchuang.Log;
import com.yunchuang.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
/**
* @author 尹冬飞
* @create 2017-05-02 9:58
*/
@RestController
public class TestController {
private Logger logger = LoggerFactory.getLogger(TestController.class);
@GetMapping("/{id}/{a}")
@Log("点点滴滴")
public String hello(@PathVariable String id, @PathVariable String a, String dd, HttpSession session) {
User user=new User();
user.setName("张三");
session.setAttribute("loginUser",user);
//logger.info("--hello--");
return "123";
}
}
image.png
如果想实现权限拦截器,思路是自定义一个权限注解,在preHandle方法里有注解的就拦截,判断里面的值是否有权限,有就放行,return true.没有就跳转,return false.跳转语句.
request.getSession().setAttribute("error", "您没有权限");
response.sendRedirect(request.getContextPath()+"/error");