Springboot自定义注解实现用户登录状态校验(一)
拦截器方式
定义注解类
import java.lang.annotation.*;
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface LoginCheck {
String description() default "";
boolean required() default true;
String value() default "";
}
实现拦截器拦截权限注解
import com.tobu.funnykits.annotions.LoginCheck;
import com.tobu.funnykits.core.domain.entity.UserInfo;
import org.springframework.http.HttpStatus;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (this.hasPermission(request, handler)) {
return true;
}
response.sendError(HttpStatus.FORBIDDEN.value(), "对不起,无目标方法的访问权限");
return false;
}
private boolean hasPermission(HttpServletRequest request, Object handler) {
System.out.println("进入权限校验===========,当前时间:"+LocalDateTime.now());
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
LoginCheck loginCheck = handlerMethod.getMethod().getAnnotation(LoginCheck.class);
if (loginCheck == null) {
return false;
}
if (loginCheck.required() == Boolean.FALSE) {
return true;
}
Object loginToken = request.getSession().getAttribute("LOGIN_TOKEN");
if (loginToken == null) {
return false;
}else{
if(redisTemplate.hasKey(loginToken.toString())){
}else{
return false;
}
}
Object loginUserObj = request.getSession().getAttribute("LOGIN_USER");
UserInfo userInfo = (UserInfo)loginUserObj;
String permission = userInfo.getPermission();
if (permissionSet.isEmpty()) {
return false;
}
return permission.equals(handlerMethod.getMethod().getDeclaringClass().getName()+"."+controllerMethodPermissionStr);
}
return true;
}
@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 {
}
}
注册拦截器
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author:小飞猪
* @date:2020/12/1 21:52
* @version:1.0
* @description:注册权限拦截器
*/
@Configuration
public class PermissionConfig implements WebMvcConfigurer {
@Bean
public LoginInterceptor loginInterceptor() {
return new LoginInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor()).excludePathPatterns("/static/*")
.excludePathPatterns("/error").addPathPatterns("/**");
}
}
demo
@GetMapping("/getRandomPassword")
@LoginCheck(description = "获取随机密码",required = true,value = "GET")
public String getRandomPassword(){
return randomPassWordService.getRandomPassWord();
}