SpringMVC的拦截器
1.在spirngmvc.xml的配置文件中添加配置:
<!--登录的拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/user/login"/>
<mvc:exclude-mapping path="/user/logout"/>
<mvc:exclude-mapping path="/gotoLogin"/>
<mvc:exclude-mapping path="/static/**"/>
<bean class="com.etoak.interceptor.LoginInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
2.编写拦截器Interceptor,Interceptor要实现org.springframework.web.servlet.HandlerInterceptor;
package com.etoak.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.etoak.bean.User;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
log.info("Login Interceptor");
HttpSession session=request.getSession();
User user=(User)session.getAttribute("user");
if(user==null){
String contextPath=request.getContextPath();
response.sendRedirect(contextPath+"/gotoLogin");
return false;
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
3.全局异常处理
一.首先我们先自己创建一个异常类;这个类继承了RuntimeException
package com.etoak.common;
import lombok.AllArgsConstructor;
@AllArgsConstructor
public class LoginException extends RuntimeException {
//序列化
private static final long serialVersionUID= 1L;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
二.然后这个异常在service的实现类中进行抛出;
@Service
public class UserServiceImpl implements UserService{
@Autowired
UserMapper userMapper;
@Override
public User queryUser(String username, String password) {
// TODO Auto-generated method stub
User user=userMapper.queryUser(username, password);
if(user==null){
throw new LoginException("用户名或密码异常");
}
return user;
}
}
三.创建一个处理全局异常的类;
顺便提一下@ControllerAdvice的作用
通过@ControllerAdvice注解可以将对于控制器的全局配置放在同一个位置。
注解了@Controller的类的方法可以使用@ExceptionHandler、@InitBinder、@ModelAttribute注解到方法上。
@ControllerAdvice注解将作用在所有注解了@RequestMapping的控制器的方法上
@ExceptionHandler:用于全局处理控制器里的异常。
具体的详细解析:@ControllerAdvice
@ControllerAdvice
@Slf4j
public class GLobalException {
@ExceptionHandler(LoginException.class)
public ModelAndView loginError(LoginException e){
log.error("login Exception message:{}",e.getMessage());
ModelAndView mv=new ModelAndView();
mv.setViewName("login");
mv.addObject("error", e.getMessage());
return mv;
}
}
4.登录的controller层
@PostMapping("/login")
public String login(HttpServletRequest request,@RequestParam String username,
@RequestParam String password){
User user=userService.queryUser(username, password);
log.info("user:{}",user);
HttpSession session=request.getSession();
session.setAttribute("user", user);
return "redirect:/";
}
个人初学者的总结认为拦截器是通过判断Session中是否含有我们所塞进去的attribute的有无情况去进行拦截:
controller层进行session.setAttribute()
Interceptor层进行session.getAttribute()
目前自己的水平是这样简单是认识的。