當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot_web开发-【实验】-登陆拦截器
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot_web开发-【实验】-登陆拦截器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我們以前用的HandlerInterceptor,我們專門來實現一個攔截器,登陸狀態檢查的攔截器,要實現HandlerInterceptor接口,這個攔截器的作用就是做登陸檢查,沒有登陸的用戶就不能訪問后臺的主頁,和對員工進行增刪改查,怎么樣檢查登陸狀態呢,當登陸成功以后我們可以把用戶放在session中,key是loginUser,已經登陸的用戶,只要登陸了用戶就會在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)) {// 登陸成功,防止表單重復提交,可以重定向到主頁session.setAttribute("loginUser", username);return "redirect:/main.html";}else {// 登陸失敗map.put("msg", "用戶名密碼錯誤");System.out.println("密碼錯誤");return "index";}}
}
preHandler就是目標方法執行之前,我們可以來執行一個preHandler,我們可以來進行一個預檢查,我們攔截器寫了以后一定要配置出來,只有這種情況下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 {// 目標方法執行之前@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {String user = (String) request.getSession().getAttribute("loginUser");if(user==null) {// 未登錄,返回登陸頁面request.setAttribute("msg", "沒有權限請先登陸");request.getRequestDispatcher("/index.html").forward(request, response);return false;}else {// 已登錄,放行請求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:指明當前類是配置類;就是來替代之前的Spring配置文件* * 在配置文件中用<bean></bean>標簽添加組件* */
//@EnableWebMvc 不要接管SpringMVC
@Configuration
public class MyMvcConfig extends WebMvcConfigurerAdapter {/*@Overridepublic void addViewControllers(ViewControllerRegistry registry) {// 瀏覽器發送/atguigu 請求來到successregistry.addViewController("/").setViewName("index");}*/// 所有的WebMvcConfigurerAdapter組件都會一起起作用@Bean // 將組件注冊到容器中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");}// 注冊攔截器@Overridepublic void addInterceptors(InterceptorRegistry registry) {// 靜態資源, *.css,*.js// SpringBoot已經做好了靜態資源映射registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login");}}; return adapter;}@Beanpublic LocaleResolver localeResolver() {return new MyLocaleResolver();}@Beanpublic EmployeeDao employeeDao() {return new EmployeeDao();}
}
?
總結
以上是生活随笔為你收集整理的SpringBoot_web开发-【实验】-登陆拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot_web开发-thy
- 下一篇: SpringBoot_数据访问-简介