javascript
spring boot security 权限用postman测试_Spring Security(五):前后端权限控制详解
文章回顧:
- Spring Security(一):整合JWT實(shí)現(xiàn)登錄功能
- Spring Security(二):獲取用戶權(quán)限菜單樹(shù)
- Spring Security(三):與Vue.js整合
- Spring Security(四):更新前端路由獲取方式
1、每次請(qǐng)求時(shí),都會(huì)帶有token,Spring Security會(huì)根據(jù)token判斷用戶信息,進(jìn)行授權(quán)。
2、對(duì)于接口權(quán)限的控制,我們可以用過(guò)使用Spring的EL表達(dá)式配合@PreAuthorize("hasAnyRole('ADMIN')")注解來(lái)對(duì)接口進(jìn)行權(quán)限控制,這段注解表示,只有當(dāng)前用戶的角色為ADMIN的時(shí)候,Spring Security才會(huì)放行。注意:建議使用ROLE_*的方式存放在數(shù)據(jù)庫(kù)中用來(lái)規(guī)定角色名。
Example
使用本系統(tǒng)的系統(tǒng)管理員和測(cè)試用戶分別使用Postman測(cè)試,這是測(cè)試用戶訪問(wèn)進(jìn)行訪問(wèn)時(shí),會(huì)拋出AccessDeniedException權(quán)限不足。
使用系統(tǒng)管理員測(cè)試結(jié)果,可以訪問(wèn)接口獲取數(shù)據(jù)。
前端權(quán)限控制
1、由于本系統(tǒng)采用的是動(dòng)態(tài)加載路由,所以如果當(dāng)前用戶的路由列表中沒(méi)有你所輸入訪問(wèn)的會(huì)轉(zhuǎn)到404頁(yè)面。
2、自定義權(quán)限判斷方法。配合v-if指令來(lái)進(jìn)行驗(yàn)證。
創(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: '需要角色權(quán)限列表',type: 'error'})return false}}解釋一下:就是從Vuex中拿到角色,然后與頁(yè)面中定義的權(quán)限角色進(jìn)行判斷,如果包含的話就可以訪問(wèn)。
<template slot-scope="scope"><el-popover//在這里使用v-if進(jìn)行判斷就行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,}這樣可以對(duì)按鈕,或者頁(yè)面中的一部分頁(yè)面進(jìn)行權(quán)限控制了~
GitHub分頁(yè)插件
再說(shuō)一下Spring Boot中使用Github的分頁(yè)插件
1、首先引入依賴
2、在application.yml中配置PageHelper如下
pagehelper:helperDialect: mysqlreasonable: truesupportMethodsArguments: trueparams: count=countSql3、建議封裝一個(gè)PageUtil,原因是通常Vue前端分頁(yè)需要我們傳遞當(dāng)前頁(yè):pageNum,頁(yè)大小: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;}}返回?cái)?shù)據(jù)的格式
然后在前端渲染數(shù)據(jù)就ok了。目前做了角色管理頁(yè)面,其中也對(duì)角色操作一欄使用hasPermission進(jìn)行了權(quán)限控制。代碼已經(jīng)同步到Github上了,如果你有任何的建議歡迎聯(lián)系我~
歡迎關(guān)注我的個(gè)人微信公眾號(hào)~
總結(jié)
以上是生活随笔為你收集整理的spring boot security 权限用postman测试_Spring Security(五):前后端权限控制详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: lfw分类 python_无法在skle
- 下一篇: Fastjson实用工具类,List转J