SpringBoot配置⽅式实例代码步骤:
1.实现WebMvcConfigurer配置类
2.实现 3
. 把添加到配置中
4.添加需要拦截的请求
5.添加需要排除的请求
package com.zp.springbootdemo.interceptor;
import t.annotation.Bean;
import t.annotation.Configuration;
import org.springframework.fig.annotation.InterceptorRegistry;
import org.springframework.fig.annotation.WebMvcConfigurer;
/
**
* 和springmvc的webmvc拦截配置⼀样
* @author zp
*/
@Configuration
public class WebConfigurer implements WebMvcConfigurer {
/**
* 配置
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
// addPathPatterns拦截所有请求,excludePathPatterns排除特殊的请求
//registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**");
registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**");
//excludePathPatterns 排除的请求
// registry.addInterceptor(LoginInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/**");
// registry.addInterceptor(AuthorityInterceptor()).addPathPatterns("/**").excludePathPatterns("/user/**");
}
/**
* ⼀
* @return
*/
@Bean
public LoginInterceptor LoginInterceptor() {
return new LoginInterceptor();
}
/**
* ⼆
* @return
*/
@Bean
public AuthorityInterceptor AuthorityInterceptor() {
return new AuthorityInterceptor();
}
}
基于URL实现的:
package com.zp.springbootdemo.interceptor;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginInterceptor extends HandlerInterceptorAdapter {
/**
* 在请求处理之前进⾏调⽤(Controller⽅法调⽤之前)
* 基于URL实现的
* @param request
* @param response
* @param handler
springboot和过滤器* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String path = ServletPath();
System.out.println("当前请求路径:"+path);
System.out.println("=========进⼊1===================");
if (path.matches(Const.NO_INTERCEPTOR_PATH)) {
//不需要的拦截直接过
System.out.println("=========⽆需拦截===================");
return true;
} else {
System.out.println("=========开始拦截===================");
// 这写你拦截需要⼲的事⼉,⽐如取缓存,SESSION,权限判断等
String token = Parameter("token");
if(token == null){
System.out.println("=========拦截完成==================="+false);
return false;
}
System.out.println("=========拦截完成==================="+true);
return true;
}
}
}
关键代码:path.matches(Const.NO_INTERCEPTOR_PATH 就是基于正则匹配的url。
package com.zp.springbootdemo.interceptor;
/**
* @author zp
* @explain 常量类
*/
public class Const {
public static final String SUCCESS = "SUCCESS";
public static final String ERROR = "ERROR";
public static final String FIALL = "FIALL";
/**********************对象和个体****************************/
public static final String SESSION_USER = "loginedAgent"; // ⽤户对象
public static final String SESSION_LOGINID = "sessionLoginID"; // 登录ID
public static final String SESSION_USERID = "sessionUserID"; // 当前⽤户对象ID编号
public static final String SESSION_USERNAME = "sessionUserName"; // 当前⽤户对象ID编号
public static final Integer PAGE = 10; // 默认分页数
public static final String SESSION_URL = "sessionUrl"; // 被记录的url
public static final String SESSION_SECURITY_CODE = "sessionVerifyCode"; // 登录页验证码
// 时间缓存时间
public static final int TIMEOUT = 1800;// 秒
public static final String ON_LOGIN = "/logout.htm";
public static final String LOGIN_OUT = "/toLogout";
// 不验证URL anon:不验证/authc:受控制的
public static final String NO_INTERCEPTOR_PATH =".*/((.css)|(.js)|(images)|(login)|(anon)).*";
}
测试:
结果:
基于注解的
①创建注解:
package com.zp.springbootdemo.interceptor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 在需要登录验证的Controller的⽅法上使⽤此注解
*/
@Target({ElementType.METHOD})// 可⽤在⽅法名上
@Retention(RetentionPolicy.RUNTIME)// 运⾏时有效
public @interface LoginRequired {
}
②创建:
package com.zp.springbootdemo.interceptor;
import org.hod.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import flect.Method;
public class AuthorityInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("===============进⼊2=====================");
// 如果不是映射到⽅法直接通过
if (!(handler instanceof HandlerMethod)) {
System.out.println("===============⽆需拦截=====================");
return true;
}
// ①:START ⽅法注解级
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = Method();
// 判断接⼝是否需要登录
LoginRequired methodAnnotation = Annotation(LoginRequired.class);
// 有 @LoginRequired 注解,需要认证
if (methodAnnotation != null) {
// 这写你拦截需要⼲的事⼉,⽐如取缓存,SESSION,权限判断等
System.out.println("===============开始拦截=====================");
String token = Parameter("token");
if(token == null){
System.out.println("=========拦截完成==================="+false);
return false;
}
System.out.println("=========拦截完成==================="+true);
return true;
}
System.out.println("===============⽆需拦截===================");
return true;
}
}
测试接⼝代码1:
package com.zp.ller;
import com.zp.springbootdemo.interceptor.LoginRequired;
import com.zp.springbootdemo.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getUser")
//@LoginRequired
public Map<String,String> getUser(@RequestParam Map<String,String> user){
User(user);
}
}
请求地址:
结果:
测试接⼝代码2:
package com.zp.ller;
import com.zp.springbootdemo.interceptor.LoginRequired;
import com.zp.springbootdemo.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/getUser")
@LoginRequired
public Map<String,String> getUser(@RequestParam Map<String,String> user){
User(user);
}
}
请求地址:
结果:
到此这篇关于SpringBoot配置⽅式实例代码的⽂章就介绍到这了,更多相关SpringBoot配置内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论