【后端】创建拦截器——用户必须先通过某个界面(如:登录界面),进入到另个页面

用户必须先通过某个界面A(如:登录界面)后才能进入另一个界面B,即不能直接跳过A界面直接进如B界面

1.创建拦截器类:
【后端】创建拦截器——用户必须先通过某个界面(如:登录界面),进入到另个页面_第1张图片
代码如下(Ctr+o添加类中方法):


import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


//设置拦截器,避免用户越过登录界面直接访问其他页面
public class LoginHandleInterceptor implements HandlerInterceptor {
//    在目标方法执行之前
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("loignUser");
        if (user == null){
//            未登录,返回登录页面
            request.setAttribute("msg","登录失败,请输入用户名和密码!");
            request.getRequestDispatcher("/index.html").forward(request,response);
            return false;
        }else{
//            已登录,放行
            return true;
        }
    }

    @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 {

    }
}

在这里插入代码片

2.添加拦截器(在创建好的MyMvcConfig文件夹下ctr+o引入刚才创建好的拦截器):
【后端】创建拦截器——用户必须先通过某个界面(如:登录界面),进入到另个页面_第2张图片
代码如下:

package com.atguigu.springboot.config;

import com.atguigu.springboot.component.LoginHandleInterceptor;
import com.atguigu.springboot.component.MyLocaleResolver;
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.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;


@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
     
    public void addViewControllers(ViewControllerRegistry registry){
     
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/index.html").setViewName("login");
        registry.addViewController("/main.html").setViewName("dashboard");

    }

//    添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
     
        registry.addInterceptor(new LoginHandleInterceptor()).addPathPatterns("/**").excludePathPatterns("/index.html","/","/user/login");
    }

    @Bean
    public LocaleResolver localeResolver(){
     
        return new MyLocaleResolver();
    }
}

注:“loignUser”定义:
在用户从登录界面向服务器发送请求时:
【后端】创建拦截器——用户必须先通过某个界面(如:登录界面),进入到另个页面_第3张图片
代码:

package com.atguigu.springboot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
     

    @PostMapping(value = "/user/login")
    public String dashboard(@RequestParam("UserName") String UserName,
                            @RequestParam("PassWord") String PassWord,
                            Map<String,Object> map, HttpSession session) {
     
        if(!StringUtils.isEmpty(UserName) && "123456".equals(PassWord)) {
     
//            在用户通过HTTP向服务器发送请求时,服务器会创建session,session中可有多个属性,每个属性对应一个值。如下面:创建session属性(可有多个),其中loignUser中的值为UserName
            session.setAttribute("loignUser",UserName);
            return "redirect:/main.html";
        }else {
     
//            登陆失败
            map.put("msg","登录失败,请检查登录名和密码是否正确!");
            return "login";
        }
    }
}

你可能感兴趣的:(计算机,后端,java,java)