带角色的url安全问题

时间:2016-07-24 03:30:49

标签: spring spring-security

在扩展WebSecurityConfigurerAdapter的类中 我有这个代码通过url为不同的角色添加安全性。

    http.authorizeRequests().antMatchers(HttpMethod.GET, "/rest/setup/defaultpassword/**").hasRole("USER");
    http.authorizeRequests().antMatchers(HttpMethod.GET, "/rest/setup/commerces/**").hasRole("USER");
    http.authorizeRequests().antMatchers(HttpMethod.GET, "/rest/setup/tax").hasRole("USER");

    http.authorizeRequests().antMatchers("/rest/setup/tax").hasRole("ADMIN");
    http.authorizeRequests().antMatchers("/login").permitAll(); //
    http.authorizeRequests().antMatchers("/rest/**").authenticated();
    http.csrf().disable();
    http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint);

    http.formLogin().successHandler(authenticationSuccessHandler);
    http.formLogin().failureHandler(authenticationFailureHandler);
    http.logout().logoutUrl("/logout");
    http.logout().logoutSuccessUrl("/");

当我使用用户角色登录时,我可以访问:/ rest / setup / tax

当我使用管理员角色登录时,我可以访问/ rest / setup / tax

http://localhost:8080/rest/setup/tax 403(禁止)

我搜索只提供get for user角色以及admin one的所有内容。

1 个答案:

答案 0 :(得分:0)

表AUTHORITIES将有两列,例如用户名,权限,列权限中指定的角色应该带有前缀ROLE_,例如ROLE_ADMIN& ROLE_USER。因此,您的代码应指定hasRole("ROLE_USER")hasRole("ROLE_ADMIN")

<强>更新

  1. RoleVoter.java
  2. https://github.com/spring-projects/spring-security/blob/master/core/src/main/java/org/springframework/security/access/vote/RoleVoter.java

    1. 关于ROLE_前缀
    2. 的类似问题

      Spring security added prefix "ROLE_" to all roles name?