springboot配置登陆拦截器

导读:本篇文章讲解 springboot配置登陆拦截器,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

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/**");
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由半码博客整理,本文链接:https://www.bmabk.com/index.php/post/81097.html

(0)

相关推荐

半码博客——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!