麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > Java > 正文

spring boot 利用注解實現權限驗證的實現代碼

2024-07-14 08:42:56
字體:
來源:轉載
供稿:網友

這里使用 aop 來實現權限驗證

引入依賴

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-aop</artifactId></dependency>

定義注解

package com.lmxdawn.api.admin.annotation;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 AuthRuleAnnotation {  String value();}

攔截實現登錄和權限驗證

package com.lmxdawn.api.admin.aspect;import com.lmxdawn.api.admin.annotation.AuthRuleAnnotation;import com.lmxdawn.api.admin.enums.ResultEnum;import com.lmxdawn.api.admin.exception.JsonException;import com.lmxdawn.api.admin.service.auth.AuthLoginService;import com.lmxdawn.api.common.utils.JwtUtils;import io.jsonwebtoken.Claims;import lombok.extern.slf4j.Slf4j;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.aspectj.lang.reflect.MethodSignature;import org.springframework.stereotype.Component;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes;import javax.annotation.Resource;import javax.servlet.http.HttpServletRequest;import java.lang.reflect.Method;import java.util.List;/** * 登錄驗證 AOP */@Aspect@Component@Slf4jpublic class AuthorizeAspect {  @Resource  private AuthLoginService authLoginService;  @Pointcut("@annotation(com.lmxdawn.api.admin.annotation.AuthRuleAnnotation)")  public void adminLoginVerify() {  }  /**   * 登錄驗證   *   * @param joinPoint   */  @Before("adminLoginVerify()")  public void doAdminAuthVerify(JoinPoint joinPoint) {    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();    if (attributes == null) {      throw new JsonException(ResultEnum.NOT_NETWORK);    }    HttpServletRequest request = attributes.getRequest();    String id = request.getHeader("X-Adminid");    Long adminId = Long.valueOf(id);    String token = request.getHeader("X-Token");    if (token == null) {      throw new JsonException(ResultEnum.LOGIN_VERIFY_FALL);    }    // 驗證 token    Claims claims = JwtUtils.parse(token);    if (claims == null) {      throw new JsonException(ResultEnum.LOGIN_VERIFY_FALL);    }    Long jwtAdminId = Long.valueOf(claims.get("admin_id").toString());    if (adminId.compareTo(jwtAdminId) != 0) {      throw new JsonException(ResultEnum.LOGIN_VERIFY_FALL);    }    // 判斷是否進行權限驗證    MethodSignature signature = (MethodSignature) joinPoint.getSignature();    //從切面中獲取當前方法    Method method = signature.getMethod();    //得到了方,提取出他的注解    AuthRuleAnnotation action = method.getAnnotation(AuthRuleAnnotation.class);    // 進行權限驗證    authRuleVerify(action.value(), adminId);  }  /**   * 權限驗證   *   * @param authRule   */  private void authRuleVerify(String authRule, Long adminId) {    if (authRule != null && authRule.length() > 0) {      List<String> authRules = authLoginService.listRuleByAdminId(adminId);      // admin 為最高權限      for (String item : authRules) {        if (item.equals("admin") || item.equals(authRule)) {          return;        }      }      throw new JsonException(ResultEnum.AUTH_FAILED);    }  }}

Controller 中使用

使用 AuthRuleAnnotation 注解, value 值就是在數據庫里面定義的 權限規則名稱

/** * 獲取管理員列表 */@AuthRuleAnnotation("admin/auth/admin/index")@GetMapping("/admin/auth/admin/index")public ResultVO index(@Valid AuthAdminQueryForm authAdminQueryForm,           BindingResult bindingResult) {  if (bindingResult.hasErrors()) {    return ResultVOUtils.error(ResultEnum.PARAM_VERIFY_FALL, bindingResult.getFieldError().getDefaultMessage());  }  if (authAdminQueryForm.getRoleId() != null) {    List<AuthRoleAdmin> authRoleAdmins = authRoleAdminService.listByRoleId(authAdminQueryForm.getRoleId());    List<Long> ids = new ArrayList<>();    if (authRoleAdmins != null && !authRoleAdmins.isEmpty()) {      ids = authRoleAdmins.stream().map(AuthRoleAdmin::getAdminId).collect(Collectors.toList());    }    authAdminQueryForm.setIds(ids);  }  List<AuthAdmin> authAdminList = authAdminService.listAdminPage(authAdminQueryForm);  // 查詢所有的權限  List<Long> adminIds = authAdminList.stream().map(AuthAdmin::getId).collect(Collectors.toList());  List<AuthRoleAdmin> authRoleAdminList = authRoleAdminService.listByAdminIdIn(adminIds);  // 視圖列表  List<AuthAdminVo> authAdminVoList = authAdminList.stream().map(item -> {    AuthAdminVo authAdminVo = new AuthAdminVo();    BeanUtils.copyProperties(item, authAdminVo);    List<Long> roles = authRoleAdminList.stream()        .filter(authRoleAdmin -> authAdminVo.getId().equals(authRoleAdmin.getAdminId()))        .map(AuthRoleAdmin::getRoleId)        .collect(Collectors.toList());    authAdminVo.setRoles(roles);    return authAdminVo;  }).collect(Collectors.toList());  PageInfo<AuthAdmin> authAdminPageInfo = new PageInfo<>(authAdminList);  PageSimpleVO<AuthAdminVo> authAdminPageSimpleVO = new PageSimpleVO<>();  authAdminPageSimpleVO.setTotal(authAdminPageInfo.getTotal());  authAdminPageSimpleVO.setList(authAdminVoList);  return ResultVOUtils.success(authAdminPageSimpleVO);}

相關地址

GitHub 地址: https://github.com/lmxdawn/vue-admin-java

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄色网址进入 | 精品久久中文字幕 | 一级毛片免费高清 | 在线区 | 在线看免电影网站 | 在线亚洲欧美 | 国产精品wwww| 亚洲aⅴ免费在线观看 | 久久久成人精品视频 | 国产精品91久久久 | 久久草在线视频免费 | 国产中文99视频在线观看 | 久久综合久久美利坚合众国 | 午夜视频免费播放 | 一色屋任你操 | 日本一区二区三区视频在线 | 91专区在线观看 | 一区二区三区欧美精品 | 禁漫天堂久久久久久久久久 | 国产毛片毛片毛片 | 少妇一级淫片免费放4p | 亚洲福利在线视频 | 香蕉久久久精品 | 免费a网 | 在线看免费观看日本 | 亚洲成人第一区 | 天天干导航 | 斗破苍穹在线免费 | 日韩视频1| 女人解衣喂奶电影 | 黄a大片 | 成人毛片100部 | 成人在线精品视频 | 亚洲午夜国产 | 伦一区二区三区中文字幕v亚洲 | 久久精品国产99久久久古代 | 精精国产xxxx视频在线播放7 | 久久最新网址 | 久久91久久久久麻豆精品 | 中文字幕在线免费观看电影 | 欧美成人精品不卡视频在线观看 |