Springboot登录拦截器
程序员文章站
2022-05-04 08:49:04
Springboot登录拦截器 和 swagger框架接口自动生成html文档 使用开发工具:IDEA 实现步骤如下 1.需导入的依赖如下: org.springframework.boot
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
下一篇: MySQL数据库入门详细笔记