feat: add interceptor

This commit is contained in:
xtaodada 2024-06-10 18:28:29 +08:00
parent 3ac281a2d7
commit 73d145c45b
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package cn.edu.cqwu.repair.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author xtaod
*/
public class AdminInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
if (session != null && session.getAttribute("adminUser") != null) {
return true;
} else {
request.setAttribute("adminLoginMess", "*请先登录!");
request.getRequestDispatcher("/manage.jsp").forward(request, response);
}
return false;
}
}

View File

@ -0,0 +1,24 @@
package cn.edu.cqwu.repair.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author xtaod
*/
public class StuInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
if (session != null && session.getAttribute("stu") != null) {
return true;
} else {
request.setAttribute("stuLoginMess", "*请先登录!");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
return false;
}
}

View File

@ -0,0 +1,24 @@
package cn.edu.cqwu.repair.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* @author xtaod
*/
public class WorkerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
if (session != null && session.getAttribute("workerUser") != null) {
return true;
} else {
request.setAttribute("workerLoginMess", "*请先登录!");
request.getRequestDispatcher("/worker_login.jsp").forward(request, response);
}
return false;
}
}

View File

@ -33,4 +33,19 @@
</bean>
<mvc:default-servlet-handler />
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/stu/**"/>
<bean class="cn.edu.cqwu.repair.interceptor.StuInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/worker/**"/>
<bean class="cn.edu.cqwu.repair.interceptor.WorkerInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/admin/**"/>
<bean class="cn.edu.cqwu.repair.interceptor.AdminInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>