springMVC盜鏈接詳解
springMVC配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--自動(dòng)掃描包--> <!-- 開(kāi)啟ioc 注解事務(wù)支持--> <context:component-scan base-package="cn"></context:component-scan> <!--開(kāi)啟spiring mvc注解支持--> <mvc:annotation-driven></mvc:annotation-driven> <!--配置spring 中的視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="resolver"> <property name="prefix" value="/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <bean id="loginInterceptor" class="cn.hp.interceptor.LoginInterceptor"></bean> </mvc:interceptor> </mvc:interceptors> </beans>
web.xml文件在我上一篇文章中攔截器https://blog.csdn.net/best_p1/article/details/118637785
登陸驗(yàn)證
package cn.hp.action; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import javax.servlet.http.HttpSession; @Controller public class UserAction { @RequestMapping("/test1.do") public String test01(){ System.out.println("正在執(zhí)行test1這個(gè)業(yè)務(wù)邏輯"); return "index"; } @RequestMapping("/test2.do") public String test02(){ System.out.println("正在執(zhí)行test2這個(gè)業(yè)務(wù)邏輯"); return "index"; } @RequestMapping("/login.do") public String login(String userName, String pwd, Model model,HttpSession session){ if (userName.equals("zs")&&pwd.equals("123")){ session.setAttribute("user",userName); return "redirect:/main.do"; }else { model.addAttribute("msg","用戶(hù)名和密碼錯(cuò)誤"); return "login"; } } @RequestMapping("/main.do") public String main(){ return "main"; } @RequestMapping("/loginOut.do") public String loginOut(HttpSession session){ session.invalidate(); return "login"; } }
登錄的攔截器LoginInterceptor:
package cn.hp.interceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String path= request.getRequestURI(); if(path.indexOf("login.do")>0){ return true; } Object obj= request.getSession().getAttribute("user"); if (obj!=null){ return true; }else { request.setAttribute("msg","別想歪心思!請(qǐng)登錄!"); request.getRequestDispatcher("login.jsp").forward(request,response); return false; } } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
jsp頁(yè)面: login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="login.do" method="post"> 賬號(hào):<input type="text" name="userName"><br/> 密碼:<input type="password" name="pwd"><br/> <input type="submit" value="登錄"> </form> ${msg} </body> </html>
main.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> ${user} <a href="loginOut.do">退出</a> </body> </html>
驗(yàn)證賬號(hào)密碼
進(jìn)行攔截 登錄才能訪(fǎng)問(wèn)
登錄成功 可以訪(fǎng)問(wèn)test1.do test2.do
點(diǎn)擊退出清除session
總結(jié)
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望能夠您能夠關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java中BigInteger類(lèi)的使用方法詳解(全網(wǎng)最新)
這篇文章主要介紹了Java中BigInteger類(lèi)的使用方法詳解,常用最全系列,本章作為筆記使用,內(nèi)容比較全面,但常用的只有:構(gòu)造函數(shù),基本運(yùn)算以及compareTo(),intValue(),setBit(),testBit()方法,需要的朋友可以參考下2023-05-05BufferedReader中read()方法和readLine()方法的使用
這篇文章主要介紹了BufferedReader中read()方法和readLine()方法的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04Java 實(shí)戰(zhàn)項(xiàng)目錘煉之校園宿舍管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+javaweb+mysql+ajax實(shí)現(xiàn)一個(gè)校園宿舍管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11Java線(xiàn)性結(jié)構(gòu)中的雙向鏈表實(shí)現(xiàn)原理
這篇文章將給大家詳細(xì)講解雙向鏈表的內(nèi)容,尤其是會(huì)通過(guò)代碼來(lái)進(jìn)行鏈表的操作,文中的代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07Java向Runnable線(xiàn)程傳遞參數(shù)方法實(shí)例解析
這篇文章主要介紹了Java向Runnable線(xiàn)程傳遞參數(shù)方法實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06SpringBoot?Http遠(yuǎn)程調(diào)用的方法
這篇文章主要為大家詳細(xì)介紹了SpringBoot?Http遠(yuǎn)程調(diào)用的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08基于Spring監(jiān)聽(tīng)Binlog日志的方法詳解
MySQL 的二進(jìn)制日志(binlog)有三種不同的格式,通常被稱(chēng)為 binlog 模式,這三種模式分別是 Statement 模式、Row 模式和Mixed 模式,本文將給大家介紹如何基于Spring監(jiān)聽(tīng)Binlog日志,需要的朋友可以參考下2024-09-09