javascript
Spring Security构建Rest服务-1400-授权
安全分為 認(rèn)證和授權(quán),前邊講的都是認(rèn)證,現(xiàn)在說授權(quán)。
前端業(yè)務(wù)系統(tǒng)的權(quán)限簡單些,一般只區(qū)分是否登錄,復(fù)雜點(diǎn)的還會區(qū)分 VIP用戶等簡單的角色,權(quán)限規(guī)則基本不變。
后臺系統(tǒng)比較復(fù)雜,角色眾多,權(quán)限隨著業(yè)務(wù)不斷變化。
?1,用代碼控制簡單的權(quán)限
直接在配置類 BrowserSecurityConfig? ?extends? ?WebSecurityConfigurerAdapter 的configure方法里
http //--------------授權(quán)相關(guān)的配置 --------------------- .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+"/*")//驗(yàn)證碼.permitAll() //-------上邊的不用授權(quán)也允許訪問------//~=========簡單的權(quán)限控制,只區(qū)分是否登錄的情況可以配置在這里=======// /user 的post請求需要ADMIN權(quán)限 .antMatchers("/user/*").hasRole("ADMIN").anyRequest() //任何請求.authenticated() //都需要身份認(rèn)證一行紅色部分配置就可以了,意思是 /user/* 的所有請求需要有ADMIN 權(quán)限,如果是Rest風(fēng)格的服務(wù),只需要配置成 antMatchers(HttpMethod.POST,"/user/*").hasRole("ADMIN") 格式即可。
這個權(quán)限在UserDetailsService 的loadUserByUsername方法返回的user的權(quán)限集合里定義。格式是ROLE_ADMIN( ROLE_權(quán)限名稱,ADMIN和匹配器里一致)(這個格式具體在ExpressionUrlAuthorizationConfigurer里)
?
?
private SocialUserDetails buildUser(String userId) {String password = passwordEncoder.encode("123456");System.err.println("加密后密碼: "+password);//參數(shù):用戶名|密碼|是否啟用|賬戶是否過期|密碼是否過期|賬戶是否鎖定|權(quán)限集合return new SocialUser(userId,password,true,true,true,true,//工具類 將字符串轉(zhuǎn)換為權(quán)限集合,ROLE_角色 是spring要求的權(quán)限格式 AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN"));}?
再說一個過濾器,AnonymousAuthenticationFilter,這個過濾器就是判斷前邊的過濾器是否認(rèn)證成功,如果沒有認(rèn)證成功,就創(chuàng)建一個默認(rèn)的用戶創(chuàng)建一個Authentication 做登錄。具體代碼看其源碼。
SpringSecurity 授權(quán)相關(guān)類
?==============================================================================================================================
控制復(fù)雜權(quán)限:基于rbac
自定義查詢權(quán)限的類根據(jù)用戶名查詢用戶的權(quán)限
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();//讀取用戶所有權(quán)限的url,需要查詢數(shù)據(jù)庫Set<String> urls = new HashSet<>();urls.add("/user/*");for(String url : urls){if(antPathMatcher.match(url, request.getRequestURI())){hasPermission = true ;break ;}}}return hasPermission;}}配置,注意,授權(quán)的配置要配置在免登錄就能訪問的服務(wù)器的最后
?github:https://github.com/lhy1234/spring-security
轉(zhuǎn)載于:https://www.cnblogs.com/lihaoyang/p/8607595.html
總結(jié)
以上是生活随笔為你收集整理的Spring Security构建Rest服务-1400-授权的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [LeetCode] 169. Majo
- 下一篇: 更改iis端口