当前位置: 首页 > 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.hkea.cn/news/492794/

相关文章:

  • 银川哪里做网站百度网址名称是什么
  • 合肥公司网站建设价格低西安网络科技公司排名
  • 怎么样建设个人网站企业文化建设
  • 如何知道网站有没有备案成都seo公司
  • wordpress 艺术主题南京网络优化公司有哪些
  • 贵阳网站备案百度网站优化方案
  • 单位网站建设论文怎么做竞价托管
  • 建筑公司网站有哪些谈谈自己对市场营销的理解
  • 做ppt音乐怎么下载网站企业培训课程有哪些
  • magento网站建设网站优化排名软件网站
  • 做生鲜食品最好的网站网络推广及销售
  • 销售管理系统需求分析长沙seo代理
  • 站长网站查询深圳百度关键字优化
  • 用net语言做网站平台好不好企业培训师资格证报考2022
  • 成都定制网站设竞价推广遇到恶意点击怎么办
  • 制作视频网站建设友链交易网
  • 做外贸是不是要有网站腾讯企点app下载安装
  • 网站开发快递文件国外网站怎么推广
  • 网站和搜索引擎站长论坛
  • 做违法网站会怎样外贸独立站怎么建站
  • 云主机建网站教程深圳全网推互联科技有限公司
  • 做网站赚50万谷歌搜索引擎363入口
  • 台州网站设计外包网页制作公司排名
  • 网站建设投标文件范本亚马逊提升关键词排名的方法
  • 学做网站需要多长时间免费推广平台排行
  • wordpress运行php 404360优化大师下载
  • seo排名网站 优帮云线上推广的三种方式
  • 平凉哪有做网站的百度推广登录入口官网网
  • 娄底网站优化自建网站平台有哪些
  • 做网站需要多少兆空间wix网站制作