Springboot登錄攔截器 和 swagger框架介面自動生成html文檔 使用開發工具:IDEA 實現步驟如下 1.需導入的依賴如下: <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifa ...
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