springboot使用注解实现鉴权功能
作者:Gms89
这篇文章主要介绍了springboot使用注解实现鉴权功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
Spring Boot 使用注解和AOP实现鉴权功能
一、自定义注解
自定义一个注解,实现以下几个要求:
1、注解使用使用在方法上;
2、注解保留到运行时;
3、注解可以传入单个参数、多个参数或者不传参数
@Documented
@Target({ElementType.METHOD}) // 用在方法上
@Retention(RetentionPolicy.RUNTIME) //注解保留到运行时
public @interface RoleType {
String[] value() default {};
}二、用户信息上下文
定义了一个名为 UserContext 的类,用于管理用户上下文信息。它使用 ThreadLocal 变量来存储每个线程独立的用户数据,包括用户名、角色列表和登录状态。
public class UserContext {
private static final ThreadLocal<List<String>> role = new ThreadLocal<>();
private static final ThreadLocal<String> username = new ThreadLocal<>();
private static final ThreadLocal<Boolean> loginStatus = new ThreadLocal<>();
public static boolean loginStatus() {
return loginStatus.get();
}
public static void login() {
UserContext.loginStatus.set(true);
}
public static void logout() {
UserContext.loginStatus.set(false);
}
public static String getUsername() {
return username.get();
}
public static void setUsername(String username) {
UserContext.username.set(username);
}
public static void clearUsername() {
username.remove();
}
public static List<String> getRole() {
return role.get();
}
public static void setRole(List<String> role) {
UserContext.role.set(role);
}
public static void setRole(String role) {
UserContext.role.set(List.of(role));
}
public static void clearRole() {
role.remove();
}
}三、设置用于拦截识别用户信息的过滤器
@Component
public class AuthFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response,
FilterChain filterChain ) throws ServletException, IOException {
//将用户信息写入上下文,可以从session或redis或者从关系型数据库获取用户信息及角色信息
UserContext.setUsername(username);
UserContext.setRole(roles);
UserContext.login();
filterChain.doFilter(request,response);
}
}四、使用AOP实现权限校验
使用Spring AOP(面向切面编程)实现的权限控制切面。使用注解的方法检查用户是否具有访问该方法所需的权限。
@Aspect
@Component
public class AuthAspect extends HttpServlet {
@Pointcut("@annotation(RoleType)")
public void annotatedMethod() {
}
//注解存在时,需要在登陆情况下v爱可以访问接口
@Around("annotatedMethod()")
public Object aroundAnnotatedMethod(ProceedingJoinPoint joinPoint) throws Throwable {
//访问接口时需要的权限标识集合
List<String> apiRole = Arrays.asList(AnnotationUtils.findAnnotation(((MethodSignature) joinPoint.getSignature()).getMethod(), RoleType.class).value());
//用户拥有的权限标识集合
List<String> userRole = UserContext.getRole();
//注解存在,并且登陆情况下可以访问接口
if (UserContext.loginStatus()){
//如果任意接口标识中元素在用户权限标识中存在,则有权访问该接口
if (apiRole.isEmpty() || apiRole.stream().anyMatch(userRole::contains)) {
return joinPoint.proceed();
} else {
throw new MallException(403, "无权限访问!");
}
}else {
throw new MallException(500,"请先登陆再访问!");
}
}
//程序运行结束后清楚上下文
@After("annotatedMethod()")
public void afterAnnotatedMethod(JoinPoint joinPoint) {
UserContext.clearRole();
UserContext.clearUsername();
UserContext.clearRole();
}
}六、注解的使用
1、无参数
使用注解情况下,必须登录情况下才可以访问
@RoleType
public String test(){
return UserContext.getRole();
}2、一个参数
用户有role权限标识才可以访问该方法
@RoleType("role")
public String test(){
return UserContext.getRole();
}3、多个参数
用户有role或test等任意一个权限标识才可以访问该方法
@RoleType({"role","test",...})
public String test(){
return UserContext.getRole();
}到此这篇关于springboot使用注解实现鉴权功能的文章就介绍到这了,更多相关springbbot注解实现鉴权内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
