【问题标题】:Issue with security by url with role带有角色的 url 的安全问题
【发布时间】:2016-11-27 15:47:31
【问题描述】:

在扩展 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/tax403(禁止)

我搜索只提供用户角色的获取和管理员角色的所有内容。

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

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

    更新:

    1. RoleVoter.java

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

    1. 所以对于 ROLE_ 前缀的类似问题

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

    【讨论】:

    • hasRole([role]) 文档说:hasRole 如果当前主体具有指定角色,则返回 true。默认情况下,如果提供的角色不以“ROLE_”开头,它将被添加。这可以通过修改文档 DefaultWebSecurityExpressionHandler 上的 defaultRolePrefix 来自定义。
    • Bernard,我们实现了Spring Security 4.0.1.RELEASE,并且在指定没有 ROLE_ 前缀的角色时遇到了类似的问题。原因是 RoleVoter 类使用前缀 ROLE_。我建议阅读上面的更新链接。注意:你也可以去掉 ROLE_ 前缀,但我不推荐它。
    • 对不起,只是一个小修正。以上内容应为“我们使用Spring Security 4.0.1.RELEASE 实现了我们的项目”。
    • 有趣的是我添加了 ROLE_ 应用程序没有启动。完全错误:4.04 的 justpaste.it/wn96 似乎不起作用。
    • 伯纳德,我不知道怎么帮你。您可以在Spring-Security 上给 github 示例尝试github.com/spring-projects/spring-security/tree/master/samples。它可能会帮助您提供一些见解。
    猜你喜欢
    • 2011-05-30
    • 2014-11-04
    • 1970-01-01
    • 2011-01-01
    • 2014-07-23
    • 2015-01-11
    • 1970-01-01
    • 2012-03-21
    • 2012-08-29
    相关资源
    最近更新 更多