java登录以及拦截器_SpringBoot 之 实现登录功能及登录拦截器

本文介绍了如何在SpringBoot中实现登录功能及登录拦截器。通过LoginController处理登录和登出请求,当用户名和密码正确时,将用户信息存入session并重定向到主页面。同时,创建LoginHandlerInterceptor作为登录拦截器,检查session中的loginUser,若不存在则转发到登录页面。最后,将拦截器添加到容器中,并配置排除路径。
摘要由CSDN通过智能技术生成

增加登录退出控制器:

# src/main/java/com/wu/controller/LoginController.java

@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";

}

}

@RequestMapping("/user/logout")

public String logout(HttpSession session) {

session.invalidate();

return "redirect:/index.html";

}

}

增加登录拦截器:

# src/main/java/com/wu/interceptor/LoginHandlerInterceptor .java

public class LoginHandlerInterceptor implements HandlerInterceptor {

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

Object user = request.getSession().getAttribute("loginUser");

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 {

}

}

将登录拦截器配置到容器中:

# src/main/java/com/wu/config/MyMvcConfig.java

@Configuration

public class MyMvcConfig implements WebMvcConfigurer {

// 将登录拦截器配置到容器中

@Override

public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(new LoginHandlerInterceptor())

.addPathPatterns("/**")

.excludePathPatterns("/", "/index.html", "/user/login", "/css/**", "/js/**", "/img/**");

}

// 配置视图跳转

@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 MyLocaleResolver();

}

// 将视图解析器配置到容器中

@Bean

public ViewResolver myViewResolver() {

return new MyViewResolver();

}

// 自定义一个视图解析器

public static class MyViewResolver implements ViewResolver {

@Override

public View resolveViewName(String viewName, Locale locale) throws Exception {

return null;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值