企业如何通过vue小程序开发满足高效运营与合规性需求
915
2022-11-24
springboot使用-判断是否登录
目录springboot-判断是否登录实现-的两个步骤1、自定义-2、自定义配置类继承WebMvcConfigurerAdapterspringboot 增加-判断是否登录1、创建-2、继承WebMvcConfigureAdapter类3、LoginController4、未登录会自动跳转到登录页面
springboot-判断是否登录
实现-的两个步骤
自定义-实现HandlerInterceptor接口
创建一个配置类继承WebMvcConfigurerAdapter类并重写addInterceptors方法
代码:
1、自定义-
@Component
public class AdminLoginInterceptor implements HandlerInterceptor {
// 在请求处理之前调用,只有返回true才会执行请求
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
// 得到session
HttpSession session = httpServletRequest.getSession(true);
// 得到对象
Object admin = session.getAttribute("admin");
// 判断对象是否存在
if(admin!=null){
return true;
}else{
// 不存在则跳转到登录页
httpServletResponse.sendRedirect(httpServletRequest.getContextPath()+"/login/adminLogin");
return false;
}
}
// 试图渲染之后执行
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
// 在请求处理之后,视图渲染之前执行
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
2、自定义配置类继承WebMvcConfigurerAdapter
@SpringBootConfiguration
public class AdminLoginAdapter extends WebMvcConfigurerAdapter {
@Autowired
AdminLoginInterceptor adminLoginInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(adminLoginInterceptor).addPathPatterns("/admin/**").excludePathPatterns("/login/**");
super.addInterceptors(registry);
}
}
springboot 增加-判断是否登录
1、创建-
package com.example.demo.interceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 〈一句话功能简述〉
* 〈-〉
*
* @author 丶Zh1Guo
* @create 2018/11/22
* @since 1.0.0
*/
public class LoginInterceptor implements HandlerInterceptor {
private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
// 在请求处理之前,只有返回true才会执行请求
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
logger.info("[-]启动登录状态拦截");
// 得到session
HttpSession session = request.getSession();
logger.info("[-]sessionID:" + session.getId());
// 得到用户信息
Object userInfo = session.getAttribute("userInfo");
//判断用户是否登录
if (userInfo != null) {
logger.info("[-]用户已经登录,用户名,密码:" + session.getAttribute("userInfo"));
return true;
} else {
//不存在跳转至登录页
response.sendRedirect(request.getContextPath() + "/"); // 跳转到首页登录
logger.info("[-]用户没有登录,已跳转到:" + request.getContextPath() + "/");
return false;
}
}
// 视图渲染后执行
@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、继承WebMvcConfigureAdapter类
覆盖其addInterceptors接口,注册自定义的-
@Configuration 注解一定要有
package com.example.demo.interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 〈一句话功能简述〉
* 〈自定义配置类〉
*
* @author 丶Zh1Guo
* @create 2018/11/22
* @since 1.0.0
*/
@Configuration
public class LoginConfig implements WebMvcConfigurer {
/**
* 该方法用于注册-
* 可注册多个-,多个-组成一个-链
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns 添加路径
// excludePathPatterns 排除路径
registry.addInterceptor(new LoginInterceptor())
.addPathPatterns("/sys/*"); // 拦截sys路径下的url
// .excludePathPatterns("");
}
}
3、LoginController
/**
* Copyright (C), 2017-2018, XXX有限公司
* FileName: LoginController
* Author: 丶Zh1Guo
* Date: 2018/11/22 11:10
* Description: 登录
* History:
*
*/
package com.example.demo.controller;
import com.example.demo.dao.UserDAO;
import com.example.demo.pojo.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
/**
* 〈一句话功能简述〉
* 〈登录〉
*
* @author 丶Zh1Guo
* @create 2018/11/22
* @since 1.0.0
*/
@Controller
public class LoginController {
// 日志
private Logger logger = LoggerFactory.getLogger(LoginController.class);
@Autowired
private UserDAO userDAO;
// 启动服务自动跳转登录
// @RequestMapping(value = {"/", "/login"})
@RequestMapping(value = "/")
public String login() {
return "login";
}
// 登录
@RequestMapping(value = "/loginCheck", method = RequestMethod.POST)
@ResponseBody
public String loginCheck(HttpServletRequest request) {
// 获取登录信息
String userName = request.getParameter("userName");
String password = request.getParameter("password");
// 封装成对象
User user = new User();
user.setUserName(userName);
user.setPassword(password);
// 校验用户信息
User info = userDAO.checkUser(user);
if (info != null) {
request.getSession().setAttribute("userInfo", userName + "-" + password);
logger.info("登录成功,用户名:" + userName + "密码:" + password);
return "success";
} else {
logger.info("登录失败,用户名:" + userName + "密码:" + password);
return "fail";
}
}
}
4、未登录会自动跳转到登录页面
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~