spring security自定義決策管理器
首先介紹下Spring的決策管理器,其接口為AccessDecisionManager,抽象類為AbstractAccessDecisionManager。而我們要自定義決策管理器的話一般是繼承抽象類而不去直接實現(xiàn)接口。
在Spring中引入了投票器(AccessDecisionVoter)的概念,有無權(quán)限訪問的最終覺得權(quán)是由投票器來決定的,最常見的投票器為RoleVoter,在RoleVoter中定義了權(quán)限的前綴,先看下Spring在RoleVoter中是怎么處理授權(quán)的。
public int vote(Authentication authentication, Object object, Collection<ConfigAttribute> attributes) {
int result = ACCESS_ABSTAIN;
Collection<? extends GrantedAuthority> authorities = extractAuthorities(authentication);
for (ConfigAttribute attribute : attributes) {
if (this.supports(attribute)) {
result = ACCESS_DENIED;
// Attempt to find a matching granted authority
for (GrantedAuthority authority : authorities) {
if (attribute.getAttribute().equals(authority.getAuthority())) {
return ACCESS_GRANTED;
}
}
}
}
return result;
}
Collection<? extends GrantedAuthority> extractAuthorities(Authentication authentication) {
return authentication.getAuthorities();
}
Authentication中是用戶及用戶權(quán)限信息,attributes是訪問資源需要的權(quán)限,然后循環(huán)判斷用戶是否有訪問資源需要的權(quán)限,如果有就返回ACCESS_GRANTED,通俗的說就是有權(quán)限。
Spring提供了3個決策管理器,至于這三個管理器是如何工作的請查看SpringSecurity源碼
AffirmativeBased 一票通過,只要有一個投票器通過就允許訪問
ConsensusBased 有一半以上投票器通過才允許訪問資源
UnanimousBased 所有投票器都通過才允許訪問
下面來實現(xiàn)一個簡單的自定義決策管理器,這個決策管理器并沒有使用投票器
public class DefaultAccessDecisionManager extends AbstractAccessDecisionManager {
public void decide( Authentication authentication, Object object,
Collection<ConfigAttribute> configAttributes)
throws AccessDeniedException, InsufficientAuthenticationException{
SysUser user = (SysUser)authentication.getPrincipal();
logger.info("訪問資源的用戶為"+user.getUsername());
//如果訪問資源不需要任何權(quán)限則直接通過
if( configAttributes == null ) {
return ;
}
Iterator<ConfigAttribute> ite = configAttributes.iterator();
//遍歷configAttributes看用戶是否有訪問資源的權(quán)限
while( ite.hasNext()){
ConfigAttribute ca = ite.next();
String needRole = ((SecurityConfig)ca).getAttribute();
//ga 為用戶所被賦予的權(quán)限。 needRole 為訪問相應(yīng)的資源應(yīng)該具有的權(quán)限。
for( GrantedAuthority ga: authentication.getAuthorities()){
if(needRole.trim().equals(ga.getAuthority().trim())){
return;
}
}
}
throw new AccessDeniedException("");
}
}
decide這個方法沒有任何的返回值,需要在沒有通過授權(quán)時拋出AccessDeniedException。
如果有訪問某個資源需要同時擁有兩個或兩個以上權(quán)限的情況,這時候就要通過自定義AccessDecisionVoter來實現(xiàn)了,這個也很簡單在這里就不贅述了。如果要在頁面中使用hasRole()這樣的表達式就需要注入WebExpressionVoter了。
在SpringSecurity中自定義權(quán)限前綴
權(quán)限的前綴默認是ROLE_,網(wǎng)上的很多例子是說,直接在配置文件中加上下面的配置就可以了。
<bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"> <property name="rolePrefix" value="AUTH_"></property> </bean>
親測不管用的,我想應(yīng)該不是我配置的問題,而是在我們配置了http auto-config="true"Spring就已經(jīng)將AccessDecisionManager初始化好了,即便配置到之前也不行,因為這個初始化是Spring自己來完成的,它并沒有把你配置的roleVoter注入到AccessDecisionManager中。那我們就來手動的注入AccessDecisionManager吧。
在http配置中有個access-decision-manager-ref屬性,可以使我們手動注入AccessDecisionManager,下面是詳細配置
<sec:http auto-config="true" access-decision-manager-ref="accessDecisionManager">
<sec:access-denied-handler ref="accessDeniedHandler"/>
<sec:session-management invalid-session-url="/login.jsp" />
<sec:intercept-url pattern="/app.jsp" access="AUTH_GG_FBGBGG"/>
<sec:intercept-url pattern="/**" access="IS_AUTHENTICATED_FULLY" />
<sec:form-login login-page="/login.jsp" authentication-failure-url="/login.jsp"
default-target-url="/index.jsp"/>
</sec:http>
<bean id="accessDecisionManager" class="org.springframework.security.access.vote.AffirmativeBased">
<constructor-arg name="decisionVoters">
<list>
<ref bean="roleVoter"/>
<ref bean="authenticatedVoter"/>
</list>
</constructor-arg>
<property name="messageSource" ref="messageSource"></property>
</bean>
<bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter">
<property name="rolePrefix" value=""></property>
</bean>
<bean id="authenticatedVoter" class="org.springframework.security.access.vote.AuthenticatedVoter" />
在這里我們就不用自定義的AccessDecisionManager了,直接用Spring的AffirmativeBased,因為Spring本身提供的這些決策管理器就已經(jīng)很強大了。
配置很簡單,要想修改權(quán)限的前綴只需要修改roleVoter中的rolePrefix就可以了,如果不要前綴就讓它為“”。
authenticatedVoter是為了支持IS_AUTHENTICATED這種認證,authenticatedVoter提供的3種認證,分別是
IS_AUTHENTICATED_ANONYMOUSLY 允許匿名用戶進入
IS_AUTHENTICATED_FULLY 允許登錄用戶進入
IS_AUTHENTICATED_REMEMBERED 允許登錄用戶和rememberMe用戶進入
總結(jié)
以上所述是小編給大家介紹的spring security自定義決策管理器,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Java中數(shù)組轉(zhuǎn)list的兩種簡單實現(xiàn)方式
這篇文章主要介紹了兩種將數(shù)組轉(zhuǎn)換為List的方法,兩種方法分別是使用Arrays.asList()方法和使用ArrayList構(gòu)造函數(shù),文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-03-03
mybatis resultType自帶數(shù)據(jù)類型別名解讀
MyBatis為了簡化開發(fā),通過org.apache.ibatis.type.TypeAliasRegistry為常見類定義了別名,這些別名包括基本數(shù)據(jù)類型及其數(shù)組、集合類型等,如string對應(yīng)java.lang.String,int對應(yīng)java.lang.Integer等,此外,還有特殊前綴的別名如_int對應(yīng)int類型2024-10-10
Spring中的ContextLoaderListener詳細解析
這篇文章主要介紹了Spring中的ContextLoaderListener詳細解析,在web容器即Tomact容器啟動web應(yīng)用即servlet應(yīng)用時,會觸發(fā)ServletContextEvent時間,這個事件會被ServletContextListener監(jiān)聽,需要的朋友可以參考下2023-12-12
Springmvc RequestMapping請求實現(xiàn)方法解析
這篇文章主要介紹了Springmvc RequestMapping請求實現(xiàn)方法解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
解決 java.lang.NoSuchMethodError的錯誤
這篇文章主要介紹了解決 java.lang.NoSuchMethodError的錯誤的相關(guān)資料,需要的朋友可以參考下2017-06-06
使用JVMTI實現(xiàn)SpringBoot的jar加密,防止反編譯
這篇文章主要介紹了使用JVMTI實現(xiàn)SpringBoot的jar加密,防止反編譯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08

