日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Security构建Rest服务-1400-授权

發布時間:2024/4/15 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Security构建Rest服务-1400-授权 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

安全分為 認證和授權,前邊講的都是認證,現在說授權。

前端業務系統的權限簡單些,一般只區分是否登錄,復雜點的還會區分 VIP用戶等簡單的角色,權限規則基本不變。

后臺系統比較復雜,角色眾多,權限隨著業務不斷變化。

?1,用代碼控制簡單的權限

直接在配置類 BrowserSecurityConfig? ?extends? ?WebSecurityConfigurerAdapter 的configure方法里

http //--------------授權相關的配置 --------------------- .authorizeRequests() // /authentication/require:處理登錄,securityProperties.getBrowser().getLoginPage():用戶配置的登錄頁 .antMatchers(SecurityConstants.DEFAULT_UNAUTHENTICATION_URL, securityProperties.getBrowser().getLoginPage(),//放過登錄頁不過濾,否則報錯 SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_MOBILE,SecurityConstants.SESSION_INVALID_PAGE,SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX+"/*")//驗證碼.permitAll() //-------上邊的不用授權也允許訪問------//~=========簡單的權限控制,只區分是否登錄的情況可以配置在這里=======// /user 的post請求需要ADMIN權限 .antMatchers("/user/*").hasRole("ADMIN").anyRequest() //任何請求.authenticated() //都需要身份認證

一行紅色部分配置就可以了,意思是 /user/* 的所有請求需要有ADMIN 權限,如果是Rest風格的服務,只需要配置成 antMatchers(HttpMethod.POST,"/user/*").hasRole("ADMIN") 格式即可。

這個權限在UserDetailsService 的loadUserByUsername方法返回的user的權限集合里定義。格式是ROLE_ADMIN( ROLE_權限名稱,ADMIN和匹配器里一致)(這個格式具體在ExpressionUrlAuthorizationConfigurer里)

?

?

private SocialUserDetails buildUser(String userId) {String password = passwordEncoder.encode("123456");System.err.println("加密后密碼: "+password);//參數:用戶名|密碼|是否啟用|賬戶是否過期|密碼是否過期|賬戶是否鎖定|權限集合return new SocialUser(userId,password,true,true,true,true,//工具類 將字符串轉換為權限集合,ROLE_角色 是spring要求的權限格式 AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"));}

?

再說一個過濾器,AnonymousAuthenticationFilter,這個過濾器就是判斷前邊的過濾器是否認證成功,如果沒有認證成功,就創建一個默認的用戶創建一個Authentication 做登錄。具體代碼看其源碼。

SpringSecurity 授權相關類

?==============================================================================================================================

控制復雜權限:基于rbac

自定義查詢權限的類根據用戶名查詢用戶的權限

package com.imooc.security.rbac;import java.util.HashSet; import java.util.Set;import javax.servlet.http.HttpServletRequest;import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.stereotype.Component; import org.springframework.util.AntPathMatcher;@Component("rbacService") public class RbacServiceImpl implements RbacService {private AntPathMatcher antPathMatcher = new AntPathMatcher();@Overridepublic boolean hasPermission(HttpServletRequest request, Authentication authentication) {Object principal = authentication.getPrincipal();boolean hasPermission = false;if(principal instanceof UserDetails){String username = ((UserDetails)principal).getUsername();//讀取用戶所有權限的url,需要查詢數據庫Set<String> urls = new HashSet<>();urls.add("/user/*");for(String url : urls){if(antPathMatcher.match(url, request.getRequestURI())){hasPermission = true ;break ;}}}return hasPermission;}}

配置,注意,授權的配置要配置在免登錄就能訪問的服務器的最后

?github:https://github.com/lhy1234/spring-security

轉載于:https://www.cnblogs.com/lihaoyang/p/8607595.html

總結

以上是生活随笔為你收集整理的Spring Security构建Rest服务-1400-授权的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。