今天在看尚硅谷SpringBoot视频的拦截器配置时,自己也玩了一下,看着很简单的东西。写的过程中却遇到了一些坑,下面就将我自己的踩坑过程记录下来,以供自己回顾。
1、首先在网上将登陆页面的代码给拷贝了过来,如下
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Signin Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link th:href="@{/asserts/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/asserts/css/signin.css}" rel="stylesheet">
</head>
<body class="text-center">
<form class="form-signin" action="dashboard.html" th:action="@{/user/login}" method="post">
<img class="mb-4" th:src="@{/asserts/img/bootstrap-solid.svg}" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal" th:text="#{login.tip}">Please sign in</h1>
<p style="color: red" th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></p>
<label class="sr-only" th:text="#{login.username}">Username</label>
<input name="username" type="text" class="form-control" th:placeholder="#{login.username}" placeholder="Username" required="" autofocus="">
<label class="sr-only" th:text="#{login.password}">Password</label>
<input name="password" type="password" class="form-control" th:placeholder="#{login.password}" placeholder="Password" required="">
<div class="checkbox mb-3">
<label>
<input type="checkbox" value="remember-me"> [[#{login.remember}]]
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit" th:text="#{login.btn}">Sign in</button>
<p class="mt-5 mb-3 text-muted">© 2017-2018</p>
<a class="btn btn-sm" th:href="@{/login.html(l='zh_CN')}">中文</a>
<a class="btn btn-sm" th:href="@{/login.html(l='en_US')}">English</a>
</form>
</body>
</html>
2、开始配置拦截器
第一步 编写拦截器
package com.boot.component;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @ClassName LoginHandlerInterceptor
* @Description TODO 登录检查
* @Author shanzz
* @Date 2019/1/4 11:06
* @Version 1.0
**/
@Component
public class LoginHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
Object user = request.getSession().getAttribute("loginUser");
if(user == null){
//未登录返回登录页面
request.setAttribute("msg","没有权限登录");
request.getRequestDispatcher("/login.html").forward(request,response);
return false;
}else {
//已登录 放行
return true;
}
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
}
}
第二步 注册拦截器;可以根据你的需要去拦截需要拦截的请求和放开不需拦截的请求
/**
* @Author shanzz
* @Description //TODO 注册拦截器
* @Date 11:17 2019/1/4
* @Param [registry]
* @return void
**/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**")//拦截所有
.excludePathPatterns("/static/**","/login.html","/login","/","/user/login")//排除不用拦截的
;
}
第三步 注册静态资源解析器
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
}
运行项目,结果如下:
所有的css,js,img都被拦截器给拦截了。
郁闷了半天,发现,这里访问的css等都不是在static静态资源下的,于是将页面的css等路径修改成如下方式
<link th:href="@{/static/asserts/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<link th:href="@{/static/asserts/css/signin.css}" rel="stylesheet">
发现项目一切正常。
总结:
可能在看视频或者书的时候,发现别人讲起来好像都是很简单的东西。自己真正实践起来才会发现,有时候虽然是很简单的东西,但是有时候成败往往在一些细节上面。自己动手了才会发现不同,发现问题处理好问题 最后别人的才能真正成为自己的。在这里鼓励大家多动手去做,真正能成为一个高手,只有你比别人能发现问题,比别人能解决问题,这才会是别人信服。