javascript
spring boot security 权限用postman测试_Spring Security(五):前后端权限控制详解
文章回顧:
- Spring Security(一):整合JWT實現登錄功能
- Spring Security(二):獲取用戶權限菜單樹
- Spring Security(三):與Vue.js整合
- Spring Security(四):更新前端路由獲取方式
1、每次請求時,都會帶有token,Spring Security會根據token判斷用戶信息,進行授權。
2、對于接口權限的控制,我們可以用過使用Spring的EL表達式配合@PreAuthorize("hasAnyRole('ADMIN')")注解來對接口進行權限控制,這段注解表示,只有當前用戶的角色為ADMIN的時候,Spring Security才會放行。注意:建議使用ROLE_*的方式存放在數據庫中用來規定角色名。
Example
使用本系統的系統管理員和測試用戶分別使用Postman測試,這是測試用戶訪問進行訪問時,會拋出AccessDeniedException權限不足。
使用系統管理員測試結果,可以訪問接口獲取數據。
前端權限控制
1、由于本系統采用的是動態加載路由,所以如果當前用戶的路由列表中沒有你所輸入訪問的會轉到404頁面。
2、自定義權限判斷方法。配合v-if指令來進行驗證。
創建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>確定刪除本條數據嗎?</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、首先引入依賴
2、在application.yml中配置PageHelper如下
pagehelper:helperDialect: mysqlreasonable: truesupportMethodsArguments: trueparams: count=countSql3、建議封裝一個PageUtil,原因是通常Vue前端分頁需要我們傳遞當前頁:pageNum,頁大小:pageSize,總數量pageTotal等參數。
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;}}返回數據的格式
然后在前端渲染數據就ok了。目前做了角色管理頁面,其中也對角色操作一欄使用hasPermission進行了權限控制。代碼已經同步到Github上了,如果你有任何的建議歡迎聯系我~
歡迎關注我的個人微信公眾號~
總結
以上是生活随笔為你收集整理的spring boot security 权限用postman测试_Spring Security(五):前后端权限控制详解的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: lfw分类 python_无法在skle
- 下一篇: 互斥锁和读写锁的区别
