国产毛多水多高潮高清,久热这里只有精品视频6,国内精品久久久久久久久电影网,国产男同志CHINA69,精品999日本久久久影院,人人妻人人澡人人爽人人精品,亚洲中文无码永久免

SpringBoot整合SpringSecurityOauth2实现鉴权-AG电玩娱乐

SpringBoot整合SpringSecurityOauth2实现鉴权

2026-01-17 22:04:44投稿人:九億電競(jìng)登錄(大慶)有限公司圍觀7563 評(píng)論

SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限

寫在前面

思考:為什么需要鑒權(quán)呢  ?

系統(tǒng)開發(fā)好上線后,API接口會(huì)暴露在互聯(lián)網(wǎng)上會(huì)存在一定的安全風(fēng)險(xiǎn),例如 :爬蟲、惡意訪問等。因此 ,我們需要對(duì)非開放API接口進(jìn)行用戶鑒權(quán) ,鑒權(quán)通過之后再允許調(diào)用。

準(zhǔn)備

spring-boot :2.1.4.RELEASE

spring-security-oauth2 :2.3.3.RELEASE(如果要使用源碼,不要隨意改動(dòng)這個(gè)版本號(hào) ,因?yàn)?.4往上的寫法不一樣了)

mysql :5.7

效果展示

這邊只用了postman做測(cè)試 ,暫時(shí)未使用前端頁面來對(duì)接 ,下個(gè)版本角色菜單權(quán)限分配的會(huì)有頁面的展示

1 、訪問開放接口 http://localhost:7000/open/hello

SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限

2 、不帶token訪問受保護(hù)接口 http://localhost:7000/admin/user/info

SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限

3、登錄后獲取token,帶上token訪問,成功返回了當(dāng)前的登錄用戶信息

SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限

實(shí)現(xiàn)

oauth2一共有四種模式 ,這邊就不做講解了 ,網(wǎng)上搜一搜,千篇一律

因?yàn)楝F(xiàn)在只考慮做單方應(yīng)用的  ,所以使用的是密碼模式。

后面會(huì)出一篇SpringCloud+Oauth2的文章,網(wǎng)關(guān)鑒權(quán)

講一下幾個(gè)點(diǎn)吧

1、攔截器配置動(dòng)態(tài)權(quán)限

SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限

新建一個(gè) MySecurityFilter類,繼承AbstractSecurityInterceptor ,并實(shí)現(xiàn)Filter接口

初始化,自定義訪問決策管理器

@PostConstruct public void init(){         super.setAuthenticationManager(authenticationManager);        super.setAccessDecisionManager(myAccessDecisionManager);  }   

自定義 過濾器調(diào)用安全元數(shù)據(jù)源

@Overridepublic SecurityMetadataSource obtainSecurityMetadataSource() {     return this.mySecurityMetadataSource;}

先來看一下自定義過濾器調(diào)用安全元數(shù)據(jù)源的核心代碼

以下代碼是用來獲取到當(dāng)前請(qǐng)求進(jìn)來所需要的權(quán)限(角色)

SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限
/**     * 獲得當(dāng)前請(qǐng)求所需要的角色     * @param object     * @return     * @throws IllegalArgumentException     */    @Override    public CollectiongetAttributes(Object object) throws IllegalArgumentException {         String requestUrl = ((FilterInvocation) object).getRequestUrl();        if (IS_CHANGE_SECURITY) {             loadResourceDefine();        }        if (requestUrl.indexOf("?") >-1) {             requestUrl = requestUrl.substring(0, requestUrl.indexOf("?"));        }        UrlPathMatcher matcher = new UrlPathMatcher();        Listlist = new ArrayList<>();  //無需權(quán)限的,直接返回        list.add("/oauth/**");        list.add("/open/**");        if(matcher.pathsMatchesUrl(list,requestUrl))            return null;        SetroleNames = new HashSet();        for (Resc resc: resources) {             String rescUrl = resc.getResc_url();            if (matcher.pathMatchesUrl(rescUrl, requestUrl)) {                 if(resc.getParent_resc_id() != null && resc.getParent_resc_id().intValue() == 1){    //默認(rèn)權(quán)限的則只要登錄了
,無需權(quán)限匹配都可訪問                    roleNames = new HashSet();                    break;                }                Map map = new HashMap();                map.put("resc_id", resc.getResc_id());                // 獲取能訪問該資源的所有權(quán)限(角色)                Listroles = roleRescMapper.findAll(map);                for (RoleRescDTO rr : roles)                    roleNames.add(rr.getRole_name());            }        }        SetconfigAttributes = new HashSet();        for(String roleName:roleNames)            configAttributes.add(new SecurityConfig(roleName));        log.debug("【所需的權(quán)限(角色)】:" + configAttributes);        return configAttributes;    }SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限

再來看一下自定義訪問決策管理器核心代碼,這段代碼主要是判斷當(dāng)前登錄用戶(當(dāng)前登錄用戶所擁有的角色會(huì)在最后一項(xiàng)寫到)是否擁有該權(quán)限角色

SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限
@Override    public void decide(Authentication authentication, Object o, CollectionconfigAttributes) throws AccessDeniedException, InsufficientAuthenticationException {         if(configAttributes == null){    //屬于白名單的	,不需要權(quán)限            return;        }        Iteratoriterator = configAttributes.iterator();        while (iterator.hasNext()){             ConfigAttribute configAttribute = iterator.next();            String needPermission = configAttribute.getAttribute();            for (GrantedAuthority ga: authentication.getAuthorities()) {                 if(needPermission.equals(ga.getAuthority())){    //有權(quán)限,可訪問                    return;                }            }        }        throw new AccessDeniedException("沒有權(quán)限訪問");    }
SpringBoot整合SpringSecurityOauth2實(shí)現(xiàn)鑒權(quán)-動(dòng)態(tài)權(quán)限

2 、自定義鑒權(quán)異常返回通用結(jié)果

為什么需要這個(gè)呢,如果不配置這個(gè)