日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring boot security 权限用postman测试_Spring Security(五):前后端权限控制详解

發(fā)布時間:2023/12/20 javascript 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot security 权限用postman测试_Spring Security(五):前后端权限控制详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章回顧:

  • Spring Security(一):整合JWT實現(xiàn)登錄功能
  • Spring Security(二):獲取用戶權限菜單樹
  • Spring Security(三):與Vue.js整合
  • Spring Security(四):更新前端路由獲取方式

1、每次請求時,都會帶有token,Spring Security會根據(jù)token判斷用戶信息,進行授權。
2、對于接口權限的控制,我們可以用過使用Spring的EL表達式配合@PreAuthorize("hasAnyRole('ADMIN')")注解來對接口進行權限控制,這段注解表示,只有當前用戶的角色為ADMIN的時候,Spring Security才會放行。注意:建議使用ROLE_*的方式存放在數(shù)據(jù)庫中用來規(guī)定角色名。

@PreAuthorize("hasAnyRole('ADMIN')")@RequestMapping(value = "/getRoleList",method = RequestMethod.POST)public RetResult getRoleList(@RequestBody Map<String,Object> map){//...}

Example

使用本系統(tǒng)的系統(tǒng)管理員和測試用戶分別使用Postman測試,這是測試用戶訪問進行訪問時,會拋出AccessDeniedException權限不足。

使用系統(tǒng)管理員測試結果,可以訪問接口獲取數(shù)據(jù)。

前端權限控制

1、由于本系統(tǒng)采用的是動態(tài)加載路由,所以如果當前用戶的路由列表中沒有你所輸入訪問的會轉(zhuǎn)到404頁面。
2、自定義權限判斷方法。配合v-if指令來進行驗證。

創(chuàng)建srcutilspermission.js

import store from '@/store'export default function hasPermission(value) {if (value && value instanceof Array && value.length > 0) {const roles = store.getters && store.getters.rolesconst permissionList = valueconst isPermission = roles.some(role => {return permissionList.includes(role.rolename)})if (!isPermission) {return false}return true} else {this.$message({message: '需要角色權限列表',type: 'error'})return false}}

解釋一下:就是從Vuex中拿到角色,然后與頁面中定義的權限角色進行判斷,如果包含的話就可以訪問。

<template slot-scope="scope"><el-popover//在這里使用v-if進行判斷就行v-if="hasPermission(['ROLE_ADMIN'])":ref="scope.row.id"placement="top"width="180"><p>確定刪除本條數(shù)據(jù)嗎?</p><div style="text-align: right; margin: 0"><el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button><el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">確定</el-button></div><el-button slot="reference" :disabled="scope.row.id === 1" type="danger" size="mini">刪除</el-button></el-popover></template>...<script>import hasPermission from '@/utils/permission'...methods: {hasPermission,}

這樣可以對按鈕,或者頁面中的一部分頁面進行權限控制了~

GitHub分頁插件

再說一下Spring Boot中使用Github的分頁插件
1、首先引入依賴

<!--github分頁插件--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.3</version></dependency>

2、在application.yml中配置PageHelper如下

pagehelper:helperDialect: mysqlreasonable: truesupportMethodsArguments: trueparams: count=countSql

3、建議封裝一個PageUtil,原因是通常Vue前端分頁需要我們傳遞當前頁:pageNum,頁大小:pageSize,總數(shù)量pageTotal等參數(shù)。

package com.example.security.util;import com.github.pagehelper.Page;import lombok.Data;import java.util.List;/*** @Autoor:楊文彬* @Date:2019/1/21* @Description:*/@Datapublic class PageUtil {private Integer pageCur;private Integer pageSize;private Integer rowTotal;private Integer pageTotal;private List data;public PageUtil(Page page,List data) {this.pageCur = page.getPageNum();this.pageSize = page.getPageSize();this.rowTotal = page.getPages();this.pageTotal = Integer.valueOf((int)page.getTotal());this.data = data;}}

返回數(shù)據(jù)的格式

然后在前端渲染數(shù)據(jù)就ok了。目前做了角色管理頁面,其中也對角色操作一欄使用hasPermission進行了權限控制。代碼已經(jīng)同步到Github上了,如果你有任何的建議歡迎聯(lián)系我~

歡迎關注我的個人微信公眾號~

總結

以上是生活随笔為你收集整理的spring boot security 权限用postman测试_Spring Security(五):前后端权限控制详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。