在現(xiàn)代的企業(yè)級應用開發(fā)中,Spring Boot 已經(jīng)成為了開發(fā)人員的首選框架之一,而 Shiro 作為一個強大的權(quán)限管理框架,可以有效地處理認證、授權(quán)等安全問題。Vue.js 作為前端開發(fā)框架,憑借其輕量、高效和靈活的特點,也得到了廣泛應用。將 Spring Boot、Shiro 和 Vue 整合在一起,可以實現(xiàn)一個完整的安全認證和權(quán)限管理的系統(tǒng),下面將詳細介紹如何將這三者結(jié)合在一起實現(xiàn)用戶權(quán)限管理。
一、技術(shù)架構(gòu)概述
整合 Spring Boot、Shiro 和 Vue 時,我們需要了解每一部分的角色和作用。Spring Boot 是后端開發(fā)框架,負責處理業(yè)務邏輯、數(shù)據(jù)持久化、請求和響應等。Shiro 是一個基于 Java 的安全框架,主要負責用戶的身份認證、授權(quán)、會話管理等。Vue.js 則是前端開發(fā)框架,負責用戶界面的展示、與后端交互的數(shù)據(jù)獲取與展示。
這種架構(gòu)的實現(xiàn)思路是前后端分離,前端通過 Vue.js 構(gòu)建用戶界面,與后端 Spring Boot 服務進行交互;后端通過 Spring Boot 處理數(shù)據(jù)邏輯,Shiro 負責安全認證和權(quán)限控制。
二、Spring Boot 整合 Shiro
首先,Spring Boot 整合 Shiro 的核心是配置 Shiro 的認證和授權(quán)邏輯。我們需要添加相關(guān)依賴,并配置 Shiro 的過濾器和 Realm。下面介紹具體步驟:
1. 添加 Maven 依賴
在 "pom.xml" 文件中添加 Spring Boot 和 Shiro 的依賴:
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring-boot-starter</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>添加完依賴后,Maven 會自動下載相關(guān)的包。
2. 配置 Shiro 的 Realm
Shiro 使用 Realm 來進行認證和授權(quán)。我們可以創(chuàng)建一個自定義的 Realm 類,重寫認證和授權(quán)的方法。以下是一個簡單的 Realm 配置:
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
public class MyShiroRealm extends AuthorizingRealm {
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
// 添加權(quán)限配置
info.addStringPermission("user:read");
info.addStringPermission("user:write");
return info;
}
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = (String) token.getPrincipal();
// 此處應該進行用戶名、密碼的驗證,簡化起見,假設認證通過
return new SimpleAuthenticationInfo(username, "password", getName());
}
}3. 配置 ShiroFilter
Shiro 通過過濾器進行請求攔截和權(quán)限驗證,我們需要配置一個 ShiroFilter 來攔截請求并進行認證和授權(quán)判斷。創(chuàng)建一個 "ShiroConfig" 配置類:
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.web.filter.mgt.DefaultFilter;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.config.Ini;
@Configuration
public class ShiroConfig {
@Bean
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
shiroFilterFactoryBean.setSecurityManager(securityManager);
// 配置 URL 過濾規(guī)則
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
filterChainDefinitionMap.put("/user/", "authc");
filterChainDefinitionMap.put("/admin/", "roles[admin]");
filterChainDefinitionMap.put("/", "anon");
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
return shiroFilterFactoryBean;
}
@Bean
public SecurityManager securityManager(MyShiroRealm myShiroRealm) {
DefaultSecurityManager securityManager = new DefaultSecurityManager();
securityManager.setRealm(myShiroRealm);
return securityManager;
}
}三、Vue 前端與 Spring Boot 后端交互
在前端部分,我們將使用 Vue.js 來構(gòu)建用戶界面,并與后端進行 API 調(diào)用。以下是如何配置前端 Vue 與后端交互的步驟:
1. 安裝 Axios
首先,我們需要安裝 Axios 來發(fā)送 HTTP 請求??梢酝ㄟ^ npm 或 yarn 安裝:
npm install axios --save
2. 配置 Vue 與后端交互
在 Vue 中創(chuàng)建一個服務類 "ApiService.js" 來封裝 API 請求:
import axios from 'axios';
export default {
login(data) {
return axios.post('/api/login', data);
},
getUserInfo() {
return axios.get('/api/user');
}
}在 Vue 組件中調(diào)用這些接口:
<template>
<div>
<h2>User Info</h2>
<div v-if="userInfo">Username: {{ userInfo.username }}Role: {{ userInfo.role }}</div>
</div>
</template>
<script>
import ApiService from '@/services/ApiService';
export default {
data() {
return {
userInfo: null
};
},
mounted() {
ApiService.getUserInfo().then(response => {
this.userInfo = response.data;
});
}
}
</script>四、前后端聯(lián)調(diào)與權(quán)限控制
前后端聯(lián)調(diào)時,前端需要向后端請求認證信息,并將 token 存儲在瀏覽器的 localStorage 或 sessionStorage 中,之后所有需要認證的接口都需要攜帶 token 進行身份驗證。
例如,當用戶登錄時,后端生成 token 并返回給前端,前端將 token 存儲在 localStorage 中,并在每次請求時將 token 添加到請求頭中:
axios.defaults.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token');后端通過 Shiro 的 "JwtFilter" 來驗證請求中的 token。
五、總結(jié)
通過整合 Spring Boot、Shiro 和 Vue.js,我們可以構(gòu)建一個完整的權(quán)限管理系統(tǒng)。在后端,Shiro 負責安全認證和授權(quán);在前端,Vue.js 提供用戶界面并與后端交互。通過 JWT 或 Session 等方式進行前后端數(shù)據(jù)傳輸,能夠?qū)崿F(xiàn)安全、穩(wěn)定的權(quán)限控制。
這種技術(shù)架構(gòu)既能夠保證系統(tǒng)的安全性,又能提高開發(fā)效率,適用于中大型項目的開發(fā)。