色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Java的接口調用時的權限驗證功能的實現

瀏覽:2日期:2022-08-20 14:07:47

提示:這里可以添加本文要記錄的大概內容:

例如:一般系統前端調用后臺相關功能接口時,需要驗證此時用戶的權限是否滿足調用該接口的條件,因此我們需要配置相應的驗證權限的功能。

提示:以下是本篇文章正文內容,下面案例可供參考

一、編寫的環境

工具:IDEA框架:GUNS框架(自帶后臺權限驗證配置,我們這里需要編寫前端權限驗證配置)

二、使用步驟

1.配置前端調用的接口

代碼如下(示例):

Java的接口調用時的權限驗證功能的實現

在WebSecurityConfig中:

// 登錄接口放開過濾.antMatchers('/login').permitAll()// session登錄失效之后的跳轉.antMatchers('/global/sessionError').permitAll()// 圖片預覽 頭像.antMatchers('/system/preview/*').permitAll()// 錯誤頁面的接口.antMatchers('/error').permitAll().antMatchers('/global/error').permitAll()// 測試多數據源的接口,可以去掉.antMatchers('/tran/**').permitAll()//獲取租戶列表的接口.antMatchers('/tenantInfo/listTenants').permitAll()//微信公眾號接入.antMatchers('/weChat/**').permitAll()//微信公眾號接入.antMatchers('/file/**').permitAll()//前端調用接口.antMatchers('/api/**').permitAll().anyRequest().authenticated();

加入前端調用接口請求地址:

.antMatchers('/api/**').permitAll()

添加后前端所有/api的請求都會被攔截,不會直接調用相應接口

2.配置攔截路徑

代碼如下(示例):

Java的接口調用時的權限驗證功能的實現

在創建文件JwtlnterceptorConfig:

package cn.stylefeng.guns.sys.modular.bzjxjy.config.jwt;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.config.annotation.InterceptorRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configurationpublic class JwtInterceptorConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { //默認攔截所有路徑 registry.addInterceptor(authenticationInterceptor()).addPathPatterns('/api/**') ; } @Bean public HandlerInterceptor authenticationInterceptor() { return new JwtAuthenticationInterceptor(); }}

3.創建驗證文件

創建文件JwtAuthenticationInterceptor,代碼如下(示例):

package cn.stylefeng.guns.sys.modular.bzjxjy.config.jwt;import cn.stylefeng.guns.sys.modular.bzjxjy.entity.Student;import cn.stylefeng.guns.sys.modular.bzjxjy.entity.TopTeacher;import cn.stylefeng.guns.sys.modular.bzjxjy.enums.RoleEnum;import cn.stylefeng.guns.sys.modular.bzjxjy.enums.StatusEnum;import cn.stylefeng.guns.sys.modular.bzjxjy.exception.NeedToLogin;import cn.stylefeng.guns.sys.modular.bzjxjy.exception.UserNotExist;import cn.stylefeng.guns.sys.modular.bzjxjy.service.StudentService;import cn.stylefeng.guns.sys.modular.bzjxjy.service.TopTeacherService;import cn.stylefeng.guns.sys.modular.bzjxjy.util.JwtUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.method.HandlerMethod;import org.springframework.web.servlet.HandlerInterceptor;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.lang.reflect.Method;/** * jwt驗證 * @author Administrator */public class JwtAuthenticationInterceptor implements HandlerInterceptor { @Autowired private TopTeacherService topTeacherService; @Autowired private StudentService studentService; @Override public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception { // 如果不是映射到方法直接通過 if (!(object instanceof HandlerMethod)) { return true; } HandlerMethod handlerMethod = (HandlerMethod) object; Method method = handlerMethod.getMethod(); //檢查是否有passtoken注釋,有則跳過認證 if (method.isAnnotationPresent(PassToken.class)) { PassToken passToken = method.getAnnotation(PassToken.class); if (passToken.required()) {return true; } } //默認全部檢查 else { // 執行認證 Object token1 = httpServletRequest.getSession().getAttribute('token'); if (token1 == null) {//這里其實是登錄失效,沒token了 這個錯誤也是我自定義的,讀者需要自己修改httpServletResponse.sendError(401,'未登錄');throw new NeedToLogin(); } String token = token1.toString(); //獲取載荷內容 String type = JwtUtils.getClaimByName(token, 'type').asString(); String id = JwtUtils.getClaimByName(token, 'id').asString(); String name = JwtUtils.getClaimByName(token, 'name').asString(); String idNumber = JwtUtils.getClaimByName(token, 'idNumber').asString(); //判斷當前為名師 if (RoleEnum.TOP_TEACHER.equals(type)){//檢查用戶是否存在TopTeacher topTeacher = topTeacherService.getById(id);if (topTeacher == null || topTeacher.getStatus().equals(StatusEnum.FORBIDDEN)) { httpServletResponse.sendError(203,'非法操作'); //這個錯誤也是我自定義的 throw new UserNotExist();} //學生 }else {//需要檢查用戶是否存在Student user = studentService.getById(id);if (user == null || user.getStatus().equals(StatusEnum.FORBIDDEN)) { httpServletResponse.sendError(203,'非法操作'); //這個錯誤也是我自定義的 throw new UserNotExist();} } // 驗證 token JwtUtils.verifyToken(token, id); //放入attribute以便后面調用 httpServletRequest.setAttribute('type', type); httpServletRequest.setAttribute('id', id); httpServletRequest.setAttribute('name', name); httpServletRequest.setAttribute('idNumber', idNumber); return true; } return true; } @Override public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,Object o, Exception e) throws Exception { }}

文件中有個string類型的token,這個token是用戶登錄時在controller里創建的,具體代碼加在用戶登陸的接口里:

String token = JwtUtils.createToken(topTeacher.getId(), RoleEnum.TOP_TEACHER,topTeacher.getName(),idNumber); request.getSession().setAttribute('token',token);

4.創建注解@PassToken

package cn.stylefeng.guns.sys.modular.bzjxjy.config.jwt;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/** * 在方法上加入本注解 即可跳過登錄驗證 比如登錄 * @author Administrator */@Target({ElementType.METHOD, ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)public @interface PassToken { boolean required() default true;}

總結

提示:這里對文章進行總結:以上就是完整的編寫一個前端頁面調用控制器接口時,進行驗證判斷相應權限的代碼實現。主要是針對guns框架寫的,因為guns框架本來自帶接口權限驗證功能,只不過只是針對后臺而已,我在這里添加了針對前端的權限驗證,僅供參考。

到此這篇關于Java的接口調用時的權限驗證功能的實現的文章就介紹到這了,更多相關Java 接口調用時權限驗證內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: a在线v| 久久久免费观成人影院 | 久草小视频 | 久久三级国产 | 亚洲1314 | 久久免费视频观看 | 日本不卡一二三区 | 国产91在线精品 | 中文字幕二区 | 国产精品久久久久久久毛片 | 无限资源中文免费 | 国产精品hd在线播放 | 日本韩经典三级在线播放 | 国产成人深夜福利在线观看 | 最近中文字幕在线 | 中文 | 欧美在线 | 欧美 | 九九在线偷拍视频在线播放 | 欧美综合成人 | 日韩免费a级在线观看 | 成人伊人青草久久综合网 | 国产精品视频男人的天堂 | 日本二级毛片免费 | 成人亚洲国产 | 97精品国产高清在线看入口 | 欧美一二三 | 大学生一级一片第一次欧美 | 国内精品久久久久久久久久影视 | 欧美一级特黄视频 | 国产偷怕自拍 | 久久3| 精品中文字幕在线 | 国产麻豆入在线观看 | 最近中文在线中文 | 日韩免费一区二区三区在线 | 欧美视频在线看 | 日本精品高清一区二区不卡 | 韩毛片 | 亚洲成a人片在线网站 | 国产成人福利夜色影视 | 日韩欧美一二区 | 一级做a免费视频观看网站 一级做a爰 |