Spring mvc 實(shí)現(xiàn)用戶(hù)登錄的方法(攔截器)
用戶(hù)登錄時(shí),將用戶(hù)信息放到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 {
/**
* 向用戶(hù)登錄頁(yè)面跳轉(zhuǎn)
*/
@RequestMapping(value = "/login",method = RequestMethod.GET)
public String toLogin(){
return "login";
}
/**
* 用戶(hù)登錄
* @param user
* @param model
* @param session
* @return
*/
@RequestMapping(value = "/login",method = RequestMethod.POST)
public String login(User user, Model model, HttpSession session){
//獲取用戶(hù)名和密碼
String username=user.getUsername();
String password=user.getPassword();
//些處橫板從數(shù)據(jù)庫(kù)中獲取對(duì)用戶(hù)名和密碼后進(jìn)行判斷
if(username!=null&&username.equals("admin")&&password!=null&&password.equals("admin")){
//將用戶(hù)對(duì)象添加到Session中
session.setAttribute("USER_SESSION",user);
//重定向到主頁(yè)面的跳轉(zhuǎn)方法
return "redirect:main";
}
model.addAttribute("msg","用戶(hù)名或密碼錯(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";
}
}
攔截未登錄的用戶(hù)
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是可以公開(kāi)訪(fǎng)問(wèn)的,其他的URL都進(jìn)行攔截控制
if (uri.indexOf("/login") >= 0) {
return true;
}
//獲取session
HttpSession session = request.getSession();
User user = (User) session.getAttribute("USER_SESSION");
//判斷session中是否有用戶(hù)數(shù)據(jù),如果有,則返回true,繼續(xù)向下執(zhí)行
if (user != null) {
return true;
}
//不符合條件的給出提示信息,并轉(zhuǎn)發(fā)到登錄頁(yè)面
request.setAttribute("msg", "您還沒(méi)有登錄,請(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)用戶(hù)登錄認(rèn)證快速使用示例代碼(前后端分離項(xiàng)目)
- Springboot+Shiro記錄用戶(hù)登錄信息并獲取當(dāng)前登錄用戶(hù)信息的實(shí)現(xiàn)代碼
- spring aop action中驗(yàn)證用戶(hù)登錄狀態(tài)的實(shí)例代碼
- springmvc+spring+mybatis實(shí)現(xiàn)用戶(hù)登錄功能(下)
- springmvc+spring+mybatis實(shí)現(xiàn)用戶(hù)登錄功能(上)
- SpringMvc實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能
- Spring實(shí)現(xiàn)加法計(jì)算器和用戶(hù)登錄功能
相關(guān)文章
springboot?html調(diào)用js無(wú)效400問(wèn)題及解決
這篇文章主要介紹了springboot?html調(diào)用js無(wú)效400的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
SpringBoot修改子模塊Module的jdk版本的方法 附修改原因
這篇文章主要介紹了SpringBoot修改子模塊Module的jdk版本的方法 附修改原因,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Spring?BeanDefinition父子關(guān)系示例解析
這篇文章主要為大家介紹了Spring?BeanDefinition父子關(guān)系示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
java學(xué)習(xí)之理解自動(dòng)拆裝箱特性
這篇文章主要介紹java自動(dòng)拆裝箱特性以及java自動(dòng)拆裝箱的應(yīng)用,有需要的朋友可以借鑒參考下,希望可以有所幫助,祝大家早日升職加薪2021-09-09
Java微信小程序oss圖片上傳的實(shí)現(xiàn)方法
這篇文章主要介紹了Java微信小程序oss圖片上傳的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Java util concurrent及基本線(xiàn)程原理簡(jiǎn)介
這篇文章主要介紹了Java util concurrent及基本線(xiàn)程原理簡(jiǎn)介,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

