免费爱碰视频在线观看,九九精品国产屋,欧美亚洲尤物久久精品,1024在线观看视频亚洲

      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)限

      寫在前面

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

      系統(tǒng)開發(fā)好上線后,API接口會(huì)暴露在互聯(lián)網(wǎng)上會(huì)存在一定的安全風(fēng)險(xiǎn),例如:爬蟲、惡意訪問(wèn)等。因此,我們需要對(duì)非開放API接口進(jìn)行用戶鑒權(quán),鑒權(quán)通過(guò)之后再允許調(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í)未使用前端頁(yè)面來(lái)對(duì)接,下個(gè)版本角色菜單權(quán)限分配的會(huì)有頁(yè)面的展示

      1、訪問(wèn)開放接口 http://localhost:7000/open/hello

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

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

      實(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)限

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

      初始化,自定義訪問(wèn)決策管理器

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

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

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

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

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

      /** * 獲得當(dāng)前請(qǐng)求所需要的角色 * @param object * @return * @throws IllegalArgumentException */ @Override public Collection 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 list = new ArrayList(); //無(wú)需權(quán)限的,直接返回 list.add(“/oauth/**”); list.add(“/open/**”); if(matcher.pathsMatchesUrl(list,requestUrl)) return null; Set 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){ //默認(rèn)權(quán)限的則只要登錄了,無(wú)需權(quán)限匹配都可訪問(wèn) roleNames = new HashSet(); break; } Map map = new HashMap(); map.put(“resc_id”, resc.getResc_id()); // 獲取能訪問(wèn)該資源的所有權(quán)限(角色) List roles = roleRescMapper.findAll(map); for (RoleRescDTO rr : roles) roleNames.add(rr.getRole_name()); } } Set configAttributes = new HashSet(); for(String roleName:roleNames) configAttributes.add(new SecurityConfig(roleName)); log.debug(“【所需的權(quán)限(角色)】:” + configAttributes); return configAttributes; }

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

      @Override public void decide(Authentication authentication, Object o, Collection configAttributes) throws AccessDeniedException, InsufficientAuthenticationException { if(configAttributes == null){ //屬于白名單的,不需要權(quán)限 return; } Iterator 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)限,可訪問(wèn) return; } } } throw new AccessDeniedException(“沒(méi)有權(quán)限訪問(wèn)”); }

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

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

      (1)未自定義前,沒(méi)有攜帶token去訪問(wèn)受保護(hù)的API接口時(shí),返回的結(jié)果是這樣的

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

      好了,來(lái)看一下是在哪里去配置的吧

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

      大伙可以參考下這個(gè) https://blog.csdn.net/Pastxu/article/details/124538364

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

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

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

      暫不做解釋

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

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

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

      在BaseController,每個(gè)Controller都會(huì)繼承這個(gè)的,在里面寫給getUser()的方法,只要用戶帶了token來(lái)訪問(wèn),我們可以直接獲取當(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; }

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

      @Servicepublic class TokenUserDetailsService implements UserDetailsService{ @Autowired private LoginService loginService; @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { User user = loginService.loadUserByUsername(username); //這個(gè)我們拎出來(lái)處理 if(Objects.isNull(user)) throw new UsernameNotFoundException(“用戶名不存在”); return new SecurityUser(user); }}

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

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

      最后我們只需要在loginService里面實(shí)現(xiàn)我們的方法就好,根據(jù)我們的實(shí)際業(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 userRoles = userRoleMapper.findAll(map); user.setRoles(listRoles(userRoles)); //權(quán)限集合 Collection authorities = merge(userRoles); user.setAuthorities(authorities); return user; } return null; }

      大功告成啦,趕緊動(dòng)起手來(lái)吧!

      附上源碼地址:https://gitee.com/jae_1995/spring-boot-oauth2

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

      點(diǎn)個(gè)小贊唄

      鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場(chǎng),版權(quán)歸原作者所有,如有侵權(quán)請(qǐng)聯(lián)系管理員(admin#wlmqw.com)刪除。
      (0)
      用戶投稿
      上一篇 2022年6月21日 15:14
      下一篇 2022年6月21日 15:14

      相關(guān)推薦

      聯(lián)系我們

      聯(lián)系郵箱:admin#wlmqw.com
      工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息