Spring mvc 實(shí)現(xiàn)用戶登錄的方法(攔截器)
用戶登錄時(shí),將用戶信息放到session中
package cn.woniubushiniu.controller;
import cn.woniubushiniu.po.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
@Controller
public class UserController {
/**
* 向用戶登錄頁(yè)面跳轉(zhuǎn)
*/
@RequestMapping(value = "/login",method = RequestMethod.GET)
public String toLogin(){
return "login";
}
/**
* 用戶登錄
* @param user
* @param model
* @param session
* @return
*/
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String login(User user, Model model, HttpSession session){
//獲取用戶名和密碼
String username=user.getUsername();
String password=user.getPassword();
//些處橫板從數(shù)據(jù)庫(kù)中獲取對(duì)用戶名和密碼后進(jìn)行判斷
if(username!=null&&username.equals("admin")&&password!=null&&password.equals("admin")){
//將用戶對(duì)象添加到Session中
session.setAttribute("USER_SESSION",user);
//重定向到主頁(yè)面的跳轉(zhuǎn)方法
return "redirect:main";
}
model.addAttribute("msg","用戶名或密碼錯(cuò)誤,請(qǐng)重新登錄!");
return "login";
}
@RequestMapping(value = "/main")
public String toMain(){
return "main";
}
@RequestMapping(value = "/logout")
public String logout(HttpSession session){
//清除session
session.invalidate();
//重定向到登錄頁(yè)面的跳轉(zhuǎn)方法
return "redirect:login";
}
}
攔截未登錄的用戶
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
//獲取請(qǐng)求的RUi:去除http:localhost:8080這部分剩下的
String uri = request.getRequestURI();
//UTL:除了login.jsp是可以公開訪問的,其他的URL都進(jìn)行攔截控制
if (uri.indexOf("/login") >= 0) {
return true;
}
//獲取session
HttpSession session = request.getSession();
User user = (User) session.getAttribute("USER_SESSION");
//判斷session中是否有用戶數(shù)據(jù),如果有,則返回true,繼續(xù)向下執(zhí)行
if (user != null) {
return true;
}
//不符合條件的給出提示信息,并轉(zhuǎn)發(fā)到登錄頁(yè)面
request.setAttribute("msg", "您還沒有登錄,請(qǐng)先登錄!");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
return false;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
配置文件
配置到spring 的配置文件中
<!--登錄攔截器-->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="cn.woniubushiniu.interceptor.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
配置web.xml 攔截所有url,并設(shè)置需要掃描的spring文件
<!--配置前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springsecurity實(shí)現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項(xiàng)目)
- Springboot+Shiro記錄用戶登錄信息并獲取當(dāng)前登錄用戶信息的實(shí)現(xiàn)代碼
- spring aop action中驗(yàn)證用戶登錄狀態(tài)的實(shí)例代碼
- springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(下)
- springmvc+spring+mybatis實(shí)現(xiàn)用戶登錄功能(上)
- SpringMvc實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Spring實(shí)現(xiàn)加法計(jì)算器和用戶登錄功能
相關(guān)文章
基于Spring Boot應(yīng)用ApplicationEvent案例場(chǎng)景
這篇文章主要介紹了基于Spring Boot應(yīng)用ApplicationEvent,利用Spring的機(jī)制發(fā)布ApplicationEvent和監(jiān)聽ApplicationEvent,需要的朋友可以參考下2023-03-03
idea運(yùn)行tomcat報(bào)錯(cuò)找不到catalina.bat,系統(tǒng)找不到指定的文件問題
這篇文章主要介紹了idea運(yùn)行tomcat報(bào)錯(cuò)找不到catalina.bat,系統(tǒng)找不到指定的文件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,2023-11-11
maven多個(gè)plugin相同phase的執(zhí)行順序
這篇文章主要介紹了maven多個(gè)plugin相同phase的執(zhí)行順序,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
基于springboot實(shí)現(xiàn)一個(gè)簡(jiǎn)單的aop實(shí)例
這篇文章主要介紹了基于springboot實(shí)現(xiàn)一個(gè)簡(jiǎn)單的aop,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
詳解Spring與Mybatis的整合方法(基于Eclipse的搭建)
這篇文章主要介紹了Spring與Mybatis的整合方法(基于Eclipse的搭建),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
基于spring實(shí)現(xiàn)websocket實(shí)時(shí)推送實(shí)例
這篇文章主要為大家詳細(xì)介紹了基于spring實(shí)現(xiàn)websocket實(shí)時(shí)推送實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
SpringBoot整合Redis使用注解進(jìn)行緩存方式
文章介紹了使用Redis進(jìn)行數(shù)據(jù)緩存的幾種方式,包括手動(dòng)配置RedisTemplate、使用Spring的Caching模塊以及配置自定義的RedisCacheManager2025-03-03

