2021年6月10日:
两个拦截器代码:
package com.atguigu.crud.lanjieqi;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.atguigu.crud.bean.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CompleteInfoInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
User user = (User) request.getSession().getAttribute("user");
// 如果有用户信息,并且昵称为null,说明没有完善个人资料
if (user != null && user.getNick_name() == null) {
response.sendRedirect(request.getContextPath() + "/user/edit?action=change");
return false;// 拦截
}
return true;
}
}
package com.atguigu.crud.lanjieqi;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
Object user = request.getSession().getAttribute("user");
if (user == null) {
response.sendRedirect(request.getContextPath() + "/login.jsp");
return false;
}
return true;
}
}