2015-10-19 5 views
14

Benim Web Security Konfigte bu kodu vardır: Yay güvenliği, "ROLE_" önekini tüm rollerin adına ekledi?

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    http 
      .authorizeRequests() 
      .antMatchers("/api/**") 
      .hasRole("ADMIN") 
      .and() 
      .httpBasic().and().csrf().disable(); 

} 

Yani benim veritabanında "YÖNETİCİ" rolle bir kullanıcı eklendi ve bu kullanıcı ile loggin uğraş ben hep 403 hatası alıyorum, sonra günlüğü etkin
2015-10-18 23:13:24.112 DEBUG 4899 --- [nio-8080-exec-1] o.s.s.w.a.i.FilterSecurityInterceptor : Secure object: FilterInvocation: URL: /api/user/login; Attributes: [hasRole('ROLE_ADMIN')] 

Neden Bahar Güvenlik "ROLE_ADMIN" yerine "ADMIN" arıyor: ve bahar ben bu satırı buldum?

+0

Olası kopyalar http://stackoverflow.com/questions/4951832/spring-security-role-prefix-and-custom Ben @Secured açıklama rol eklerseniz -kullanicitarafindanolusturulmus ayrıntıları hizmet) – Bruno

cevap

8

Bahar güvenlik varsayılan olarak öneki "ROLE_" ekler. Bu kaldırılmasını veya değiştirilmesini istiyorsanız

,

http://forum.spring.io/forum/spring-projects/security/51066-how-to-change-role-from-interceptor-url

DÜZENLEME bakmak: bulunan bu yanı: Spring Security remove RoleVoter prefix

+0

Sadece bir soru, Bahar varsayılan olarak ROLE_ ekler? Gibi "Güvenli" ("abc") eklerseniz, ROLE_abc için Bahar kontrol veya sadece abc? Eğer varsayılan 'ROLE_' değiştirebilir @SajibAcharya –

+0

. http://stackoverflow.com/questions/38134121/how-do-i-remove-the-role-prefix-from-spring-security-with-javaconfig/38970309#38970309 bakın – walsh

9

Bahar 4'te tanımlanan iki yöntem hasAuthority() ve hasAnyAuthority() vardır org.springframework.security.access.expression.SecurityExpressionRoot sınıfında. olmadan, yalnızca özel rolü adı Bu iki yöntem çekler ROLE_ önek ekleme. Tanım aşağıdaki gibidir:

public final boolean hasAuthority(String authority) { 
    return hasAnyAuthority(authority); 
} 
public final boolean hasAnyAuthority(String... authorities) { 
    return hasAnyAuthorityName(null, authorities); 
} 
private boolean hasAnyAuthorityName(String prefix, String... roles) { 
    Set<String> roleSet = getAuthoritySet(); 

    for (String role : roles) { 
     String defaultedRole = getRoleWithDefaultPrefix(prefix, role); 
     if (roleSet.contains(defaultedRole)) { 
      return true; 
     } 
    } 

    return false; 
} 
private static String getRoleWithDefaultPrefix(String defaultRolePrefix, String role) { 
    if (role == null) { 
     return role; 
    } 
    if (defaultRolePrefix == null || defaultRolePrefix.length() == 0) { 
     return role; 
    } 
    if (role.startsWith(defaultRolePrefix)) { 
     return role; 
    } 
    return defaultRolePrefix + role; 
} 

Örnek kullanım:

<http auto-config="false" use-expressions="true" pattern="/user/**" 
     entry-point-ref="loginUrlAuthenticationEntryPoint"> 
    <!--If we use hasAnyAuthority, we can remove ROLE_ prefix--> 
    <intercept-url pattern="/user/home/yoneticiler" access="hasAnyAuthority('FULL_ADMIN','ADMIN')"/> 
    <intercept-url pattern="/user/home/addUser" access="hasAnyAuthority('FULL_ADMIN','ADMIN')"/> 
    <intercept-url pattern="/user/home/addUserGroup" access="hasAuthority('FULL_ADMIN')"/> 
    <intercept-url pattern="/user/home/deleteUserGroup" access="hasAuthority('FULL_ADMIN')"/> 
    <intercept-url pattern="/user/home/**" access="hasAnyAuthority('FULL_ADMIN','ADMIN','EDITOR','NORMAL')"/> 
    <access-denied-handler error-page="/403"/> 
    <custom-filter position="FORM_LOGIN_FILTER" ref="customUsernamePasswordAuthenticationFilter"/> 
    <logout logout-url="/user/logout" 
      invalidate-session="true" 
      logout-success-url="/user/index?logout"/> 
    <!-- enable csrf protection --> 
    <csrf/> 
</http> <beans:bean id="loginUrlAuthenticationEntryPoint" 
      class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> 
    <beans:constructor-arg value="/user"/> 
</beans:bean> 
1

@olyanren üzücü, Bahar 4 yerine hasRole içinde hasAuthority() metodu kullanmak üzere(). Ben JavaConfig örneği ekliyorum:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    .authorizeRequests() 
    .antMatchers("/api/**") 
    .access("hasAuthority('ADMIN')") 
    .and() 
    .httpBasic().and().csrf().disable(); 
} 
[Bahar Güvenlik Rolü Önek ve Özel Kullanıcı Bilgileri Servisi] (içinde
İlgili konular