溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

發(fā)布時間:2022-06-21 09:45:42 來源:億速云 閱讀:284 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題”文章能幫助大家解決問題。

準備

spring-boot:2.1.4.RELEASE

spring-security-oauth3:2.3.3.RELEASE(如果要使用源碼,不要隨意改動這個版本號,因為2.4往上的寫法不一樣了)

mysql:5.7

效果展示

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

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

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

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

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

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

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

實現(xiàn)

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

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

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

講一下幾個點吧

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

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

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

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

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

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

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

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

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

/**
     * 獲得當(dāng)前請求所需要的角色
     * @param object
     * @return
     * @throws IllegalArgumentException
     */
    @Override
    public Collection<ConfigAttribute> getAttributes(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();
        List<Object> list = new ArrayList<>();  //無需權(quán)限的,直接返回
        list.add("/oauth/**");
        list.add("/open/**");
        if(matcher.pathsMatchesUrl(list,requestUrl))
            return null;

        Set<String> roleNames = 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){   //默認權(quán)限的則只要登錄了,無需權(quán)限匹配都可訪問
                    roleNames = new HashSet();
                    break;
                }
                Map map = new HashMap();
                map.put("resc_id", resc.getResc_id());
                // 獲取能訪問該資源的所有權(quán)限(角色)
                List<RoleRescDTO> roles = roleRescMapper.findAll(map);
                for (RoleRescDTO rr : roles)
                    roleNames.add(rr.getRole_name());
            }
        }

        Set<ConfigAttribute> configAttributes = new HashSet();
        for(String roleName:roleNames)
            configAttributes.add(new SecurityConfig(roleName));

        log.debug("【所需的權(quán)限(角色)】:" + configAttributes);

        return configAttributes;
    }

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

@Override
    public void decide(Authentication authentication, Object o, Collection<ConfigAttribute> configAttributes) throws AccessDeniedException, InsufficientAuthenticationException {
        if(configAttributes == null){   //屬于白名單的,不需要權(quán)限
            return;
        }
        Iterator<ConfigAttribute> iterator = 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)限訪問");

    }

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

為什么需要這個呢,如果不配置這個,對于前端,后端來說都很難去理解鑒權(quán)失敗返回的內(nèi)容,還不能統(tǒng)一解讀,廢話不多說,先看看不配置和配置了的返回情況

(1)未自定義前,沒有攜帶token去訪問受保護的API接口時,返回的結(jié)果是這樣的

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

(2)我們規(guī)定一下,鑒權(quán)失敗的接口返回接口之后,變成下面這種了,是不是更利于我們處理和提示用戶

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

好了,來看一下是在哪里去配置的吧

我們資源服務(wù)器OautyResourceConfig,重寫下下面這部分的代碼,來自定義鑒權(quán)異常返回的結(jié)果

大伙可以參考下這個http://www.kemok4.com/article/131668.htm

@Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.authenticationEntryPoint(authenticationEntryPoint)    //token失效或沒攜帶token時
                .accessDeniedHandler(requestAccessDeniedHandler);   //權(quán)限不足時
    }

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

3、獲取當(dāng)前登錄用戶

第一種:使用JWT攜帶用戶信息,拿到token后再解析

暫不做解釋

第二種:寫一個SecurityUser實現(xiàn)UserDetails接口(這個工程中使用的是這一種)

原來的只有UserDetails接口只有username和password,這里我們加上我們系統(tǒng)中的User

protected User user;
    public SecurityUser(User user) {
        this.user = user;
    }

    public User getUser() {
        return user;
    }

在BaseController,每個Controller都會繼承這個的,在里面寫給getUser()的方法,只要用戶帶了token來訪問,我們可以直接獲取當(dāng)前登錄用戶的信息了

protected User getUser() {
        try {
            SecurityUser userDetails = (SecurityUser) SecurityContextHolder.getContext().getAuthentication()
                    .getPrincipal();

            User user = userDetails.getUser();
            log.debug("【用戶:】:" + user);

            return user;
        } catch (Exception e) {
        }
        return null;
    }

那么用戶登錄成功后,如何去拿到用戶的角色集合等呢,這里面就要實現(xiàn)UserDetailsService接口了

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

@Service
public class TokenUserDetailsService implements UserDetailsService{

    @Autowired
    private LoginService loginService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = loginService.loadUserByUsername(username);  //這個我們拎出來處理
        if(Objects.isNull(user))
            throw new UsernameNotFoundException("用戶名不存在");
        return new SecurityUser(user);
    }
}

然后在我們的安全配置類中設(shè)置UserDetailsService為上面的我們自己寫的就行

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

@Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

最后我們只需要在loginService里面實現(xiàn)我們的方法就好,根據(jù)我們的實際業(yè)務(wù)處理判斷該用戶是否存在等

@Override
    public User loadUserByUsername(String username){
        log.debug(username);
        Map map = new HashMap();
        map.put("username",username);
        map.put("is_deleted",-1);
        User user = userMapper.findByUsername(map);
        if(user != null){
            map = new HashMap();
            map.put("user_id",user.getUser_id());
            //查詢用戶的角色
            List<UserRoleDTO> userRoles = userRoleMapper.findAll(map);
            user.setRoles(listRoles(userRoles));
            //權(quán)限集合
            Collection<? extends GrantedAuthority> authorities = merge(userRoles);
            user.setAuthorities(authorities);
            return user;
        }
        return null;

    }

數(shù)據(jù)庫文件在這

SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題

關(guān)于“SpringBoot怎么整合SpringSecurityOauth2實現(xiàn)鑒權(quán)動態(tài)權(quán)限問題”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI