2015-10-19 18 views
14

Tôi có mã này trong an ninh Config Web của tôi:Tiền tố bổ sung bảo mật mùa xuân "ROLE_" cho tất cả tên vai trò?

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

} 

Vì vậy, tôi đã thêm một người sử dụng với vai trò "ADMIN" trong cơ sở dữ liệu của tôi và tôi luôn nhận được 403 lỗi khi tôi tryed loggin với người dùng này, sau đó tôi cho phép đăng nhập cho mùa xuân và tôi thấy dòng này:

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')] 

Tại sao bảo mật mùa xuân tìm "ROLE_ADMIN" thay vì "ADMIN"?

+0

có thể trùng lặp của [Xuân An Vai trò Prefix và Custom tài khoản Chi tiết dịch vụ] (http://stackoverflow.com/questions/4951832/spring-security-role-prefix-and-custom -user-details-service) – Bruno

Trả lời

8

Bảo mật mùa xuân thêm tiền tố "ROLE_" theo mặc định.

Nếu bạn muốn điều này xóa hoặc thay đổi, hãy nhìn vào

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

EDIT: thấy điều này cũng như: Spring Security remove RoleVoter prefix

+0

Chỉ là một câu hỏi, Spring có thêm ROLE_ theo mặc định nếu tôi thêm một vai trò trong chú thích @Secured không? Giống như Nếu tôi thêm @Secured ("abc"), Spring có kiểm tra ROLE_abc hoặc chỉ abc không? –

+0

@SajibAcharya bạn có thể thay đổi mặc định 'ROLE_'. vui lòng xem http://stackoverflow.com/questions/38134121/how-do-i-remove-the-role-prefix-from-spring-security-with-javaconfig/38970309#38970309 – walsh

9

Trong mùa xuân 4, có hai phương pháp hasAuthority()hasAnyAuthority() định nghĩa trong lớp học org.springframework.security.access.expression.SecurityExpressionRoot. Hai phương pháp này chỉ kiểm tra tên vai trò tùy chỉnh của bạn mà không cần thêm tiền tố ROLE_. Định nghĩa như sau:

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; 
} 

Ví dụ sử dụng:

<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

Như @olyanren buồn, bạn có thể sử dụng phương pháp hasAuthority() trong mùa xuân 4 thay vì hasRole(). Tôi thêm JavaConfig dụ:

@Override 
protected void configure(HttpSecurity http) throws Exception { 
    .authorizeRequests() 
    .antMatchers("/api/**") 
    .access("hasAuthority('ADMIN')") 
    .and() 
    .httpBasic().and().csrf().disable(); 
} 
Các vấn đề liên quan