<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                首先介紹下Spring的決策管理器,其接口為AccessDecisionManager,抽象類為AbstractAccessDecisionManager。而我們要自定義決策管理器的話一般是繼承抽象類而不去直接實現接口。 在Spring中引入了投票器(AccessDecisionVoter)的概念,有無權限訪問的最終覺得權是由投票器來決定的,最常見的投票器為RoleVoter,在RoleVoter中定義了權限的前綴,先看下Spring在RoleVoter中是怎么處理授權的。 ~~~ 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中是用戶及用戶權限信息,attributes是訪問資源需要的權限,然后循環判斷用戶是否有訪問資源需要的權限,如果有就返回ACCESS_GRANTED,通俗的說就是有權限。 Spring提供了3個決策管理器,至于這三個管理器是如何工作的請查看SpringSecurity源碼 AffirmativeBased?一票通過,只要有一個投票器通過就允許訪問 ConsensusBased?有一半以上投票器通過才允許訪問資源 UnanimousBased?所有投票器都通過才允許訪問 下面來實現一個簡單的自定義決策管理器,這個決策管理器并沒有使用投票器 ~~~ 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()); //如果訪問資源不需要任何權限則直接通過 if( configAttributes == null ) { return ; } Iterator<ConfigAttribute> ite = configAttributes.iterator(); //遍歷configAttributes看用戶是否有訪問資源的權限 while( ite.hasNext()){ ConfigAttribute ca = ite.next(); String needRole = ((SecurityConfig)ca).getAttribute(); //ga 為用戶所被賦予的權限。 needRole 為訪問相應的資源應該具有的權限。 for( GrantedAuthority ga: authentication.getAuthorities()){ if(needRole.trim().equals(ga.getAuthority().trim())){ return; } } } throw new AccessDeniedException(""); } } ~~~ decide這個方法沒有任何的返回值,需要在沒有通過授權時拋出AccessDeniedException。 如果有訪問某個資源需要同時擁有兩個或兩個以上權限的情況,這時候就要通過自定義AccessDecisionVoter來實現了,這個也很簡單在這里就不贅述了。如果要在頁面中使用hasRole()這樣的表達式就需要注入WebExpressionVoter了。 在SpringSecurity中自定義權限前綴 權限的前綴默認是ROLE_,網上的很多例子是說,直接在配置文件中加上下面的配置就可以了。 ~~~ <bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"> <property name="rolePrefix" value="AUTH_"></property> </bean> ~~~ 親測不管用的,我想應該不是我配置的問題,而是在我們配置了http auto-config="true"Spring就已經將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本身提供的這些決策管理器就已經很強大了。 配置很簡單,要想修改權限的前綴只需要修改roleVoter中的rolePrefix就可以了,如果不要前綴就讓它為“”。 authenticatedVoter是為了支持IS_AUTHENTICATED這種認證,authenticatedVoter提供的3種認證,分別是 IS_AUTHENTICATED_ANONYMOUSLY 允許匿名用戶進入 IS_AUTHENTICATED_FULLY 允許登錄用戶進入 IS_AUTHENTICATED_REMEMBERED 允許登錄用戶和rememberMe用戶進入
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看