授權和微服務的相互認證


一授權

1課程服務是一個資源服務

在其某個controller中加preauthorize注解,

2同時配置文件中開啟

3.jwt令牌中包含對應權限信息才可以操作

XcUserExt userext = userClient.findUserInfo(username);


/*
//獲取當前用戶的權限信息
List<XcMenu> menuList = xcMenuMapper.findMenuList(xcUser.getId());
xcUserExt.setPermissions(menuList);*/

 

select * from xc_menu where id in (select menu_id from xc_permission where role_id in(select role_id from xc_user_role where user_id ='49') )

 存到jwt令牌中

 

 

權限五張表阿帕奇的shrio和springsecurity都是基於

權限,權限角色roleid,resourceID,角色,用戶角色userid roleid,用戶,

查詢主表為權限,根據用戶id查roleid 查中間表

4.權限不足友好提示

 1 import com.google.common.collect.ImmutableMap;
 2 import com.xuecheng.filesystem.framework.model.response.CommonCode;
 3 import com.xuecheng.filesystem.framework.model.response.ResponseResult;
 4 import com.xuecheng.filesystem.framework.model.response.ResultCode;
 5 import lombok.extern.slf4j.Slf4j;
 6 import org.springframework.http.converter.HttpMessageNotReadableException;
 7 import org.springframework.web.bind.annotation.ControllerAdvice;
 8 import org.springframework.web.bind.annotation.ExceptionHandler;
 9 import org.springframework.web.bind.annotation.ResponseBody;
10 
11 //全局異常抓取類
12 @ControllerAdvice //增強controller
13 @Slf4j
14 public class ExceptionCatch {
15 
16     //ImmutableMap 線程安全,聲明之后內容不可變
17     private static ImmutableMap<Class<? extends Throwable>,ResultCode> EXCEPTIONS;
18 
19     protected static ImmutableMap.Builder<Class<? extends Throwable>,ResultCode> builder = ImmutableMap.builder();
20 
21     //抓取自定義異常(可預知異常)
22     @ExceptionHandler(CustomerException.class)
23     @ResponseBody
24     public ResponseResult customerException(CustomerException customerException){
25         //給用戶返回友好信息
26         ResultCode resultCode = customerException.getResultCode();
27 
28         ResponseResult responseResult = new ResponseResult(resultCode);
29         return responseResult;
30     }
31 
32     //抓取不可預知異常
33     @ExceptionHandler(Exception.class)
34     @ResponseBody
35     public ResponseResult exception(Exception exception){
36 
37         log.error(exception.getMessage());
38 
39         if (EXCEPTIONS == null){
40             EXCEPTIONS = builder.build();
41         }
42         ResultCode resultCode = EXCEPTIONS.get(exception.getClass());
43         if (resultCode == null){
44             return new ResponseResult(CommonCode.SERVER_ERROR);
45         }else{
46             return new ResponseResult(resultCode);
47         }
48 
49     }
50 
51     static {
52         builder.put(HttpMessageNotReadableException.class, CommonCode.INVALIDATE_PARAMS);
53     }
54 }

權限不足,無權操作。

 

點擊“”管理課程“”沒信息回顯,發出查詢課程基礎信息請求,被攔截,

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM