當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
SpringBoot_web开发-【实验】-登陆拦截器
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot_web开发-【实验】-登陆拦截器
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
我們以前用的HandlerInterceptor,我們專門來(lái)實(shí)現(xiàn)一個(gè)攔截器,登陸狀態(tài)檢查的攔截器,要實(shí)現(xiàn)HandlerInterceptor接口,這個(gè)攔截器的作用就是做登陸檢查,沒(méi)有登陸的用戶就不能訪問(wèn)后臺(tái)的主頁(yè),和對(duì)員工進(jìn)行增刪改查,怎么樣檢查登陸狀態(tài)呢,當(dāng)?shù)顷懗晒σ院笪覀兛梢园延脩舴旁趕ession中,key是loginUser,已經(jīng)登陸的用戶,只要登陸了用戶就會(huì)在session中存在
package com.learn.controller;import java.util.Map;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;@Controller
public class LoginController {@PostMapping@RequestMapping(value="/user/login")public String login(@RequestParam("username") String username, @RequestParam("password") String password,Map<String,Object> map,HttpSession session) {System.out.println(username);System.out.println(password);System.out.println("!StringUtils.isEmpty(username)"+!StringUtils.isEmpty(username));System.out.println("\"123456\".equals(password)" + "123456".equals(password));if(!StringUtils.isEmpty(username) && "123456".equals(password)) {// 登陸成功,防止表單重復(fù)提交,可以重定向到主頁(yè)session.setAttribute("loginUser", username);return "redirect:/main.html";}else {// 登陸失敗map.put("msg", "用戶名密碼錯(cuò)誤");System.out.println("密碼錯(cuò)誤");return "index";}}
}
preHandler就是目標(biāo)方法執(zhí)行之前,我們可以來(lái)執(zhí)行一個(gè)preHandler,我們可以來(lái)進(jìn)行一個(gè)預(yù)檢查,我們攔截器寫(xiě)了以后一定要配置出來(lái),只有這種情況下SpringMVC才能用的到
package com.learn.component;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;/*** 登陸檢查**/
public class LoginHandlerInterceptor implements HandlerInterceptor {// 目標(biāo)方法執(zhí)行之前@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {String user = (String) request.getSession().getAttribute("loginUser");if(user==null) {// 未登錄,返回登陸頁(yè)面request.setAttribute("msg", "沒(méi)有權(quán)限請(qǐng)先登陸");request.getRequestDispatcher("/index.html").forward(request, response);return false;}else {// 已登錄,放行請(qǐng)求return true; }}@Overridepublic void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)throws Exception {// TODO Auto-generated method stub}@Overridepublic void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)throws Exception {// TODO Auto-generated method stub}}
package com.learn.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import com.learn.component.LoginHandlerInterceptor;
import com.learn.component.MyLocaleResolver;
import com.learn.dao.EmployeeDao;/*** @Configuration:指明當(dāng)前類是配置類;就是來(lái)替代之前的Spring配置文件* * 在配置文件中用<bean></bean>標(biāo)簽添加組件* */
//@EnableWebMvc 不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {/*@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// 瀏覽器發(fā)送/atguigu 請(qǐng)求來(lái)到successregistry.addViewController("/").setViewName("index");}*/// 所有的WebMvcConfigurerAdapter組件都會(huì)一起起作用@Bean // 將組件注冊(cè)到容器中public WebMvcConfigurerAdapter webMvcConfigurerAdapter() {WebMvcConfigurerAdapter adapter = new WebMvcConfigurerAdapter() {@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/").setViewName("index");registry.addViewController("/index.html").setViewName("index");registry.addViewController("/main.html").setViewName("dashboard");}// 注冊(cè)攔截器@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 靜態(tài)資源, *.css,*.js// SpringBoot已經(jīng)做好了靜態(tài)資源映射registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login");}}; return adapter;}@Beanpublic LocaleResolver localeResolver() {return new MyLocaleResolver();}@Beanpublic EmployeeDao employeeDao() {return new EmployeeDao();}
}
?
總結(jié)
以上是生活随笔為你收集整理的SpringBoot_web开发-【实验】-登陆拦截器的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: SpringBoot_web开发-thy
- 下一篇: SpringBoot_数据访问-简介