springboot配置登陆拦截器

1:在config层中新建LoginHandlerInterceptor类,使得这个类继承HandlerInterceptor并重写方法

2:在controller层中写可以传入用户session的方法,首先添加HttpSession在具体业务前面传入session的值请添加图片描述

请添加图片描述

3:在LoginHandlerInterceptor类中编写代码:

package com.example.springbootweb.config;

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

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

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 16:16
 */
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //登陆成功之后,应该有用户的session
        Object loginUser = request.getSession().getAttribute("loginUser");
        if(loginUser==null ){ //没有登陆
            request.setAttribute("msg","没有权限,请先登录");
            request.getRequestDispatcher("./index.html").forward(request,response);
            return false;
        }else {
            return  true;
        }
    }
}

4:编写LoginController类

package com.example.springbootweb.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 14:00
 */
@Controller
public class LoginController {

    @RequestMapping("/user/login")
    public  String login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            Model model, HttpSession session){

        //具体业务
        if (!StringUtils.isEmpty(username) && "123456".equals(password)){
            session.setAttribute("loginUser",username);

            return "redirect:/main.html";
        }
        //告诉用户登陆失败了
        else {
            model.addAttribute("msg","用户名或密码错误");
            return "index";

        }

    }
}

5:在MyMvcConfig中配置需要拦截的东西

package com.example.springbootweb.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.WebMvcConfigurer;

/**
 * @author ${范涛之}
 * @Description
 * @create 2021-09-19 10:16
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    // 添加视图控制
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
       registry.addViewController("/").setViewName("index");
       registry.addViewController("/index.html").setViewName("index");
       registry.addViewController("/main.html").setViewName("dashboard");
    }

    //自定义的国际化就生效了
    @Bean
    public LocaleResolver localeResolver(){ return  new MyLocalResolver(); }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(new LoginHandlerInterceptor())
          .addPathPatterns("/**")
          .excludePathPatterns("/index.html","/","/user/login","/css/*","/js/**","/img/**");
    }
}

  • 42
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

water-之

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值