Springboot登录拦截器 和 swagger框架接口自动生成html文档
使用开发工具:IDEA
实现步骤如下
1.需导入的依赖如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--swagger api 依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.登录拦截器类
package com.ckf.springbootinterceptor.config; import org.springframework.web.servlet.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.util.ArrayList; import java.util.List; /** * 登录拦截器 */
public class LoginInterceptor implements HandlerInterceptor { /** * 用于存储排除拦截的url (登录/login.html, /css,/js,/img) */
private List<String> urls = new ArrayList<String>(); /** * 进入控制器之前拦截 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HttpSession session = request.getSession(); if (session.getAttribute("admin") != null) { //已登录,放行。。
return true; } else { System.out.println("你还没登录,没有权限"); /* response.sendRedirect("/no_login");*/
/* session.setAttribute("no_login",session); request.setAttribute("msg", "没有权限,先登录");*/
//未登录,拦截 返回login
response.sendRedirect("/login"); //未登录,拦截跳转到登录页
return false; } } /*设置能通过的url*/
public List<String> getUrls() { urls.add("/login"); //login url请求
urls.add("/doLogin"); //登录请求
urls.add("/no_login"); urls.add("/swagger-ui.html"); //静态资源
urls.add("/img/*"); urls.add("/js/*"); urls.add("/css/*"); return urls; } }
3.注册拦截器配置类
package com.ckf.springbootinterceptor.config; 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 WebappAdrapter implements WebMvcConfigurer { /** * 将登录拦截器添加进来 * addPathPatterns()添加拦截 * excludePathPatterns()排除拦截 */ @Override public void addInterceptors(InterceptorRegistry registry) { //实例化登录拦截器,用于注册
LoginInterceptor loginInterceptor = new LoginInterceptor(); /*** * 所有路径都被拦截 addPathPatterns("/**") * 允许通过 excludePathPatterns("/login", "/register") */ registry.addInterceptor(loginInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger-resources/**").excludePathPatterns(loginInterceptor.getUrls()); WebMvcConfigurer.super.addInterceptors(registry); } }
项目已托管码云
地址:https://gitee.com/ckfeng/springboot-interceptor.git