WEB开发----springboot的登录拦截机制
如果是一个后台的管理项目的,有些东西是不能直接就可以访问的,必须要登录才可以进去,所以就需要进行登录拦截,只有登录过的用户才可以正常访问.
登录拦截是不会拦截jsp页面的方法,所以我们需要在Controller写方法进行页面的调用,而且需要把jsp页面从webapp文件夹下放到WEB-INF下面,因为webapp下的文件是可以直接访问到的:文件目录
,
首先创建一个WebConfig.class文件,进行拦截器的创建,拦截器需要实现WebMvcConfigurerAdapter类,继承ApplicationContextAware类,
代码如下:
package com;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import com.interceptor.LoginInterceptor;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
public WebConfig(){
super();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("1");
registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");
registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");
super.addResourceHandlers(registry);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("11");
this.applicationContext = applicationContext;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
System.out.println("111");
//拦截规则:除了login,其他都拦截判断
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/login","/user/gologin");
super.addInterceptors(registry);
}
}
上面的文件除了/user/login(登录信息验证方法),/user/gologin(返回登录页面方法)这两个方法不拦截,别的都拦截判断
然后编写自定义的验证规则,判断拦截到的请求是否通过
package com.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class LoginInterceptor implements HandlerInterceptor {
private static final Logger log = LoggerFactory.getLogger(LoginInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
// TODO Auto-generated method stub
log.info("------preHandle------");
// 获取session
HttpSession session = request.getSession(true);
// 判断用户ID是否存在,不存在就跳转到登录界面
if (session.getAttribute("userId") == null) {
log.info("------:跳转到login页面!");
System.out.println(request.getContextPath() + "/login");
response.sendRedirect("/user/gologin");
return false;
} else {
return true;
}
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
}
}
当用户登录成功,将用户的信息存到session中,之后的访问,就会去session中判断有没有用户信息,如果没有用户信息,则跳转到登录页面,进行用户登录
WEB开发----springboot的登录拦截机制的更多相关文章
- springboot的登录拦截机制
转自:https://blog.csdn.net/qq_26555463/article/details/78296103 如果是一个后台的管理项目的,有些东西是不能直接就可以访问的,必须要登录才可以 ...
- SpringBoot Web开发(5) 开发页面国际化+登录拦截
SpringBoot Web开发(5) 开发页面国际化+登录拦截 一.页面国际化 页面国际化目的:根据浏览器语言设置的信息对页面信息进行切换,或者用户点击链接自行对页面语言信息进行切换. **效果演示 ...
- Springboot 系列(七)Spring Boot web 开发之异常错误处理机制剖析
前言 相信大家在刚开始体验 Springboot 的时候一定会经常碰到这个页面,也就是访问一个不存在的页面的默认返回页面. 如果是其他客户端请求,如接口测试工具,会默认返回JSON数据. { &quo ...
- Android开发系列之事件拦截机制
对于Android开发者来说理解事件传递机制的重要性,我想应该是不言而喻的.在一个Activity里面,我们经常会重写onTouchEvent事件,可是重写结束之后,对于是返回true还是返回fals ...
- SpringBoot(四): SpringBoot web开发 SpringBoot使用jsp
1.在SpringBoot中使用jsp,需要在pom.xml文件中添加依赖 <!--引入Spring Boot内嵌的Tomcat对JSP的解析包--> <dependency> ...
- springboot深入学习(二)-----profile配置、运行原理、web开发
一.profile配置 通常企业级应用都会区分开发环境.测试环境以及生产环境等等.spring提供了全局profile配置的方式,使得在不同环境下使用不同的applicaiton.properties ...
- springboot全局异常拦截源码解读
在springboot中我们可以通过注解@ControllerAdvice来声明一个异常拦截类,通过@ExceptionHandler获取拦截类抛出来的具体异常类,我们可以通过阅读源码并debug去解 ...
- 4_4.springboot之Web开发登录和拦截器
1.登录处理 1).禁用模板引擎的缓存 # 禁用缓存 spring.thymeleaf.cache=false 2).页面修改完用ctrl+f9:重新编译: LoginController @Cont ...
- Springboot 系列(六)Spring Boot web 开发之拦截器和三大组件
1. 拦截器 Springboot 中的 Interceptor 拦截器也就是 mvc 中的拦截器,只是省去了 xml 配置部分.并没有本质的不同,都是通过实现 HandlerInterceptor ...
随机推荐
- codevs1669(dfs)子集和目标值
1692 子集和的目标值 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给定n个整数in和目标值T,求某一非空子集 ...
- 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns
题目传送门 /* 题意:删除若干行,使得n行字符串成递增排序 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 */ /*********************** ...
- Spring Boot (31) 数据验证
曾经参数的验证是这样的: public String test(User user){ if(user == null){ throw new NullPointerException("u ...
- linux环境mxnet 安装
环境ubuntu 16.04 1.安装依赖项: sudo update && sudo apt-get install -y build-essential git libatlas- ...
- go 语言开发环境的安装与配置
go 语言开发环境的安装与配置 编辑器选择 一直以来都是用sublime,但是听说sublime对于golang的插件支持并不是特别完善,并且VS Code只要在自身所带的扩展商店里安装go插件就可以 ...
- HTML城市联动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- leetcode516 Longest Palindromic Subsequence
思路: 区间dp. 实现: class Solution { public: int longestPalindromeSubseq(string s) { int n = s.length(); ] ...
- Greenplum开发
Greenplum(GP)采用了MPP架构,基于开源的数据库 PostgreSQL(PG). 1.首先什么是MPP架构? GreenPlum的架构采用了MPP(大规模并行处理).在 MPP 系统中,每 ...
- Windows10环境中 laravel任务调度 如何启动调度
Windows10环境中 laravel任务调度 如何启动调度 一:问题由来 1:今天在做用laravel开发订单系统的时候,需要使用定时任务来大批量提交订单,测试一下订单金额是否有误.发现larav ...
- IDEA中springboot项目打包成jar
springboot的打包方式有很多种.有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的.这里主要介绍如何打成jar进行部署.不推荐用war,因为spri ...