
当没有输入正确的账号密码登录成功时, 除了登录页,其他页面都无法访问(静态资源要放行)
2.实现步骤pom.xml
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.0 com.limi springboot-test2 0.0.1-SNAPSHOT springboot-test2 Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-configuration-processor true org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok org.springframework.boot spring-boot-maven-plugin org.springframework.boot spring-boot-configuration-processor
application.properties
server.port=80802.java代码
SpringbootTest2Application
package com.limi.springboottest2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootTest2Application {
;
public static void main(String[] args) {
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(SpringbootTest2Application.class, args);
}
}
LoginInterceptor
package com.limi.springboottest2.interceptor;
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;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("=========LoginInterceptor preHandle==========");
HttpSession session = request.getSession();
if(session.getAttribute("username")==null) //未登录
{
//未登录, 重定向到登录页
response.sendRedirect("/index"); //重定向到登录controller
return false;//拦截
}
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("==========LoginInterceptor postHandle==========");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("==========LoginInterceptor afterCompletion==========");
}
}
WebConfig
package com.limi.springboottest2.config;
import com.limi.springboottest2.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginInterceptor())//拦截器注册到容器中
.addPathPatterns("/**") //所有请求都被拦截包括静态资源
.excludePathPatterns("/index", "/login") //放行的网络请求,
.excludePathPatterns("/view/index.html","/css/**","/images/**", "/js/**"); //放行的资源请求
}
}
HelloController
package com.limi.springboottest2.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
@Controller
public class HelloController {
@GetMapping("/index")
public String index(){
return "/view/index.html"; //没有使用模板引擎, 所以要带上后缀
}
@PostMapping("/login")
public String login(HttpSession session, String username, String password){
System.out.println("username:"+username+" password:"+password);
if("andy".equals(username)&&"123456".equals(password)) { //账号密码匹配成功
session.setAttribute("username", username);
return "redirect:/success";
}
return "redirect:/index";
}
@GetMapping("/success")
public ModelAndView test1(HttpSession session){
System.out.println("======执行控制器中方法success======");
String name = (String)session.getAttribute("username");
ModelAndView mv = new ModelAndView();
mv.addObject("name", name);
mv.setViewName("/view/success.html");
return mv;
}
}
3.前端代码
index.css
h1{
color: blueviolet;
}
index.html
Title
请输入账号密码进行登录!
success.html
Title
登录成功!
3.运行测试
1.直接通过网址访问登录成功页面
被重定向到登录页
2.输入错误账号密码
被重定向到登录页
3.输入正确账号密码