springboot 实现登录的用户拦截功能

UsersController
package com.example.controller;

import com.example.entity.Users;
import com.example.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;


@Controller
public class UsersController {

    @Autowired
    private UsersService usersService;



    @RequestMapping("/login")
    public String login(String username, String password, Model model, HttpSession session){
        Users login = usersService.login(username, password);

        if(login!=null){
            session.setAttribute("verification",login);
            model.addAttribute("msg",username);
            return "main";
        }else{
            return "index";
        }

    }
}

 LoginHandInterceptor

package com.example.loginHandInterceptor;

import org.springframework.web.servlet.HandlerInterceptor;

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

public class LoginHandInterceptor implements HandlerInterceptor {
    //用户拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("verification");
        if(user!=null){


            return true;
        }else {

            request.setAttribute("msg","你还没有登录,请先登录!");
            request.getRequestDispatcher("/login").forward(request,response);
//            response.sendRedirect("/index.html");

            return false;
        }


    }
}
configAdapter
package com.example.config;

import com.example.loginHandInterceptor.LoginHandInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class configAdapter implements WebMvcConfigurer {


    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
        registry.addViewController("/index.html").setViewName("index");


    }


    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       //这里注意 我这里没有static里面没有什么 要是项目中里面有记得把这个也加进去,不然也会拦截 类如  "/css/**"加到excludePathPatterns里面即可
        registry.addInterceptor(new LoginHandInterceptor()).addPathPatterns("/**").excludePathPatterns("/","/index.html", "/login");


    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值