国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring boot 利用注解實現權限驗證的實現代碼

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

2021-06-17 11:03蘭茗翔 Java教程

這篇文章主要介紹了spring boot 利用注解實現權限驗證的實現代碼,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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

引入依賴

?
1
2
3
4
<dependency>
  <groupid>org.springframework.boot</groupid>
  <artifactid>spring-boot-starter-aop</artifactid>
</dependency>

定義注解

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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();
}

攔截實現登錄和權限驗證

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
@slf4j
public 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 值就是在數據庫里面定義的 權限規則名稱

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
 * 獲取管理員列表
 */
@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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://segmentfault.com/a/1190000017131530

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲一级在线 | 欧美一级在线视频 | 最近韩国日本免费观看mv免费版 | 日韩1区 | 日韩午夜电影 | 精品在线一区二区 | 色狠狠网| 免费av在线网站 | 亚洲成人日韩在线 | 寡妇少妇高潮免费看蜜臀a 午夜免费电影 | 日韩免费在线观看视频 | 91麻豆精品国产91久久久更新时间 | 亚洲精品在线观看网站 | 国产精品3区 | 国产黄色小视频 | 欧美精品亚洲精品日韩精品 | 久久66 | 中国一级毛片 | 久久国产成人 | 欧美亚洲国产日韩 | 国产黄色网址在线观看 | 一级黄免费看 | 国产一区 欧美 | 亚洲九九九 | 欧美激情精品久久久久久 | 久久久精品网站 | 免费一级毛片免费播放 | 日韩在线 | 亚洲成人高清 | 一区二区三区 | 亚洲国产精品自拍 | 夜夜操av | 成人精品视频 | 一区二区国产在线观看 | 亚洲欧洲一区二区三区 | 国产精品一区二区无线 | 日韩高清在线一区 | 91网在线观看 | 欧美日韩国产一区二区三区 | 国产高清在线 | 国产在线中文字幕 |