当前位置: 首页 > news >正文

淘宝网站建设方案做婚礼网站的公司简介

淘宝网站建设方案,做婚礼网站的公司简介,网站建设小程序定制开发,做那类网站赚钱AOP,是一种面向切面编程,可以通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。 在软件开发中,鉴权(Authentication)是一项非常重要的安全措施,用于验证用户身份和权限。在应用程序中&…

AOP,是一种面向切面编程,可以通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。

在软件开发中,鉴权(Authentication)是一项非常重要的安全措施,用于验证用户身份和权限。在应用程序中,我们通常会使用AOP(Aspect-Oriented Programming)来实现鉴权功能,以便在需要进行鉴权的地方进行统一的处理。

一种常用的实现AOP鉴权的方式是使用自定义注解。通过定义一个自定义注解,并在需要进行鉴权的方法上加上该注解,我们可以在运行时通过AOP切面来拦截方法调用,并进行鉴权操作。

第一种实现逻辑

首先,我们需要定义一个自定义注解,用于标识需要进行鉴权的方法。可以使用Java的注解机制来实现,如下所示:

import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  @Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)  
public @interface Authenticated {  String[] roles() default {};  
}  

在上面的代码中,我们定义了一个名为Authenticated的注解,并指定了它的作用目标为方法。Authenticated注解还包含了一个可选的roles属性,用于指定允许访问该方法的用户角色。

接下来,我们需要编写一个AOP切面来实现鉴权逻辑。可以使用Spring框架提供的@Aspect注解来定义一个切面类,并使用@Around注解来实现方法拦截和鉴权逻辑,如下所示:

import org.aspectj.lang.JoinPoint;  
import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;  
import org.springframework.security.core.Authentication;  
import org.springframework.security.core.context.SecurityContextHolder;  
import org.springframework.stereotype.Component;  @Aspect  
@Component  
public class AuthorizationAspect {  @Around("@annotation(authenticated)")  public Object authenticate(ProceedingJoinPoint joinPoint, Authenticated authenticated) throws Throwable {  // 获取当前用户的角色  Authentication authentication = SecurityContextHolder.getContext().getAuthentication();  String[] userRoles = authentication.getAuthorities().stream().map(Object::toString).toArray(String[]::new);  // 获取方法需要的角色  String[] requiredRoles = authenticated.roles();  // 鉴权逻辑  boolean authorized = false;  for (String requiredRole : requiredRoles) {  for (String userRole : userRoles) {  if (userRole.equals(requiredRole)) {  authorized = true;  break;  }  }  }  // 如果鉴权通过,则继续执行原方法,否则抛出鉴权异常  if (authorized) {  return joinPoint.proceed();  } else {  throw new AuthorizationException("Access denied");  }  }  
}  

在上面的代码中,我们定义了一个名为AuthorizationAspect的切面类,并使用@Around注解来标识需要拦截的方法。在authenticate方法中,我们首先获取当前用户的角色,然后与方法需要的角色进行比较,判断是否有权限访问该方法。

最后,我们在需要进行鉴权的方法上加上@Authenticated注解,并指定允许访问该方法的用户角色,如下所示:

@Authenticated(roles = {"admin", "user"})  
public void someMethod() {  // 需要鉴权的方法逻辑  
} 

通过上述步骤,我们就实现了使用自定义注解方式来实现AOP鉴权。在运行时,AOP切面会拦截带有@Authenticated注解的方法调用,并进行鉴权操作。如果鉴权通过,则继续执行原方法,否则抛出鉴权异常。

第二种实现逻辑

新建一个注解类

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PermissionAnnotation {String id() default "";String name() default "";
}

新建切面类

@Aspect
@Component
public class PermissionAspect {@Pointcut("@annotation(xxx.xxx.xxx.PermissionAnnotation)")public void permissionPointCut() {// Do nothing because of it's a pointcut}@Around("permissionPointCut()")public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {MethodSignature sign = (MethodSignature) proceedingJoinPoint.getSignature();Method method = sign.getMethod();PermissionAnnotation annotation = method.getAnnotation(PermissionAnnotation.class);String id= annotation.id();String name= annotation.name();if (checkPermission(id, name)) {// 有权限,业务方法执行Object result = proceedingJoinPoint.proceed();// 业务方法执行完可进行额外操作} else {// 没有权限}}private boolean checkPermission() {// 权限判断if (xxx) {return true;}return false;}
}

业务方法使用

@Override
@PermissionAnnotation(id= "${id}")
public void deleteUser(String id) {//业务代码
}

其实这俩种实现逻辑基于的原理是一样的,就是看实际应用需要的是何种形式,当然也会有第三种第四种…

总结起来,使用自定义注解方式实现AOP鉴权可以提高代码的可读性和可维护性

http://www.yayakq.cn/news/676234/

相关文章:

  • 一个服务器怎么做两个网站网页设计模板html代码运行
  • 怎么做网站超链接淘客做网站运营
  • 世界杯网站开发wordpress各函数
  • 学生为学校做网站企业网站需要多少费用
  • 福州外贸网站建设python开发app
  • 做网站公司哪好网站后台数据改不了
  • 网络公司怎样推广网站软件开发包括网站开发吗
  • 重庆网站建设川娃子企业网站建设费用怎么记账
  • 成都品牌设计网站深圳建网站好的公司
  • 四川建设局网站万网网站编辑
  • 南通住房和城乡建设局网站旅游网站建设开发
  • cms网站建设方案安阳区号是什么
  • 网站建设中文百深圳装修公司排行榜
  • 潍坊市房屋和城乡建设局网站德州有做网站的
  • 临海高端营销型网站建设地址非法集资罪提供网站建设
  • 公众号第三方建微网站做一个招聘网站需要多少钱
  • 如何做自己的vip视频解析网站沧州网站建设专业的公司4000-262-
  • flask做的购物网站柳编网站建设意义
  • 济南做网站哪里便宜视频app推广
  • seo网站标题pc端宣传网站开发
  • 网站建设规划书的目的海南网站建设
  • 泉州手机网站建设永久免费的网络加速器
  • 如何上传文件到网站四川个人证书查询网官网
  • 网站建设开发决策谷歌浏览器 安卓下载
  • wordpress如何建立网站制作微信小程序的教程
  • 网站制作加盟旅游网站开发方案ppt
  • 网站的优化方法seo网站项目讲解
  • 上海市 建设执业资格注册中心网站全案网络推广公司
  • 永久网站建设有特色的网站设计
  • 杭州网站建设设计公司郑州公司建设网站