SpringBoot拦截器实现登录拦截
pom.xml:
WebMvcConfigurer:继承WebMvcConfigurationSupport类,重写addInterceptors方法
/** * 在springboot2.0.0之后,WebMvcConfigurerAdapter已经过时了 * 会使用WebMvcConfigurer或者WebMvcConfigurationSupport替代 * * @author wyj * @create 2019-06-01 21:48 */@Configurationpublic class WebMvcConfigurer extends WebMvcConfigurationSupport { /** * 在springboot2.0.0之前继承WebMvcConfigurerAdapter类,重写addInterceptors方法 * * @param registry */// @Override// public void addInterceptors(InterceptorRegistry registry) {// /**// * 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL,前面的拦截器会执行,后面的拦截器将不执行// */// registry.addInterceptor(new AuthorityInterceptor())// .addPathPatterns("/user/**");// super.addInterceptors(registry);// } /** * 在springboot2.0.0之后实现WebMvcConfigurer接口,重写addInterceptors方法 * * @param registry */// @Override// public void addInterceptors(InterceptorRegistry registry) {// /**// * 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL,前面的拦截器会执行,后面的拦截器将不执行// */// registry.addInterceptor(new AuthorityInterceptor())// .addPathPatterns("/user/**");// } /** * 在springboot2.0.0之后继承WebMvcConfigurationSupport类,重写addInterceptors方法 * * @param registry */ @Override protected void addInterceptors(InterceptorRegistry registry) { /** * 拦截器按照顺序执行,如果不同拦截器拦截存在相同的URL,前面的拦截器会执行,后面的拦截器将不执行 */ registry.addInterceptor(new AuthorityInterceptor()) .addPathPatterns("/user/**"); super.addInterceptors(registry); }}
AuthorityInterceptor:实现HandlerInterceptor接口,重写preHandle、postHandle、afterCompletion方法
preHandle:在请求处理之前进行调用(Controller方法调用之前) postHandle:请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后) afterCompletion:在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)
@Slf4jpublic class AuthorityInterceptor implements HandlerInterceptor { private static final Set
UserController:
@Controller@RequestMapping(value = "/user")public class UserController { @RequestMapping(value = "/login.html") public String index() { return "login"; } @RequestMapping(value = "/login") public String login(User user) { //查询数据库,我这里直接写死 User dbUser = new User(1, "zhangsan", "123456", "admin"); if (dbUser.getPassword().equals(user.getPassword())) { UserInfo userInfo = new UserInfo(dbUser.getId(), dbUser.getUsername(), dbUser.getRole()); HttpSession session = getRequest().getSession(); session.setAttribute("user_info_in_the_session", userInfo); return "admin"; } return "login"; } @RequestMapping(value = "/userInfo") @ResponseBody public String userInfo() { HttpSession session = getRequest().getSession(); UserInfo userInfo = (UserInfo) session.getAttribute("user_info_in_the_session"); return userInfo.toString(); } private HttpServletRequest getRequest() { return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); }}
User:
@Data@NoArgsConstructor@AllArgsConstructorpublic class User implements Serializable { private int id; private String username; private String password; private String role;}
UserInfo: 用于存在用户信息储存在session中
@Data@NoArgsConstructor@AllArgsConstructorpublic class UserInfo implements Serializable { private int id; private String username; private String role;}
login.html:只是一个很简单的登录表单
admin.html: