IDEA下springboot项目spring+mybatis+sqlserver+log4j+简单登录验证拦截器的框架搭建
先贴一个项目的目录结构
Attention:templates.html5是templates目录下新建了一个html5目录
1.项目创建
这个简单,选择jdk目录,填写项目信息,选择springboot版本,勾选一些需要的包依赖,基本上就是一直next,然后finish就好了,略过。。。
2.pom.xml
项目创建时,会将你选择的springboot包依赖加进去,我们需要的是加入一些创建时没有的依赖
2.1 数据库连接池--druid
关于druid数据库连接池的优点和性能就先不说,百度一下,你就知道
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
2.2 数据返回格式--JSONObject
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.36</version>
</dependency>
2.3日志输出--log4j
<!-- Log start -->
<!-- spring boot start -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除自带的logback依赖 -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- springboot-log4j -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.8.RELEASE</version>
</dependency>
<!-- Log end -->
引入log4j之前首先要排除springboot自带的logback依赖,在创建Spring Boot工程时,我们引入了spring-boot-starter
,其中包含了spring-boot-starter-logging
,该依赖内容就是Spring Boot默认的日志框架Logback。
附上一份log4j.properties的文件
#logger level
log4j.rootCategory=DEBUG,stdout,DebugAppender,InfoAppender,ErrorAppender
log4j.debug=true
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
#logger input file
log4j.logger.DebugAppender.access=DEBUG
log4j.appender.DebugAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DebugAppender.File=../logs/a.log
log4j.appender.DebugAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugAppender.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
#logger input file
log4j.logger.InfoAppender.access=INFO
log4j.appender.InfoAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.InfoAppender.File=../logs/b.log
log4j.appender.InfoAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.InfoAppender.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n
#error log input file
log4j.logger.ErrorAppender.access=ERROR
log4j.appender.ErrorAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ErrorAppender.File=../logs/c.log
log4j.appender.ErrorAppender.Append = true
log4j.appender.ErrorAppender.threshold = ERROR
log4j.appender.ErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ErrorAppender.layout.ConversionPattern=%d{MM-dd HH:mm:ss.SSS} %c %-5p - %m%n
pom.xml到这里就基本结束了,贴上一张log4j的日志打印图。
3.简单的登录验证拦截器
自定义一个登录验证拦截器LoginInterceptor,实现HandlerInterceptor接口
扫描二维码关注公众号,回复:
4179115 查看本文章
![](/qrcode.jpg)
3.1 LoginInterceptor.java
private static final Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);
//在目标方法执行前执行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.err.println("------preHandle------");
System.out.println("ContextPath" + request.getContextPath());
System.out.println("getRequestURI" + request.getRequestURI());
//获取session
HttpSession session = request.getSession(true);
//判断用户是否存在,不存在就跳转到登录页面
if (session.getAttribute("username")==null){
String uri = request.getRequestURI();
System.out.println(uri);
String url = request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/index";
System.out.println(url);
response.sendRedirect(request.getContextPath()+"/index");
return false;
}
return true;
}
//在目标方法执行后执行,但在请求返回前,我们仍然可以对 ModelAndView进行修改
@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 {
}
3.2 登录配置类--LoginConfig.java
继承WebMvcConfigurerAdapter类,此类一定要记得加上@Configuration注解,否则无法生效
public LoginConfig() {
super();
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
//拦截规则:除了excludePathPatterns中地址,其他都拦截判断
registry.addInterceptor(new LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/index");
super.addInterceptors(registry);
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
super.addResourceHandlers(registry);
}
4.结语
大概就是这样,中间漏了一部分内容,等有空闲时补上。新手上路,多多指教。