【问题标题】:spring boot ldap group and restricted endpointsspring boot ldap 组和受限端点
【发布时间】:2016-12-26 23:22:25
【问题描述】:

我想将某些休息端点限制为仅适用于某个组中的 LDAP 用户。

我按照指南 https://spring.io/guides/gs/authenticating-ldap/ 设置了运行良好的 LDAP 身份验证。那么如何限制某些休息端点呢?

我试过了

@PreAuthorize("hasRole('developers')")
@RequestMapping("/foo")
public String foo(HttpServletRequest request) {
    return "Welcome to FOO " + request.getRemoteUser();
}

但它仍然允许不在开发者组中的用户访问该端点

【问题讨论】:

    标签: java rest spring-security spring-boot ldap


    【解决方案1】:

    您可以将WebSecurityConfigurerAdapter 配置修改为:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().fullyAuthenticated()
                .and()
                .antMatchers("/foo").hasRole("developers")
                .and()
            .formLogin();
    }
    

    我不太确定语法以及第一个规则是否会覆盖您的第二个规则,但它会类似于那个。

    或者,您可以尝试逐个方法配置安全性,例如this sample

    【讨论】:

    • 不,它仍然允许所有用户访问 /foo
    • 如果您更改顺序,它似乎可以工作。 http.authorizeRequests().antMatchers("/foo").hasRole("DEVELOPERS").antMatchers("/**").fullyAuthenticated().and().formLogin().and().httpBasic();
    【解决方案2】:

    @EnableGlobalMethodSecurity(securedEnabled=true) 需要添加到 webSecurityConfig。一旦我这样做了,我就可以使用@Secured("ROLE_DEVELOPERS"),然后该方法仅限于该角色。

    【讨论】:

      猜你喜欢
      • 2017-10-20
      • 1970-01-01
      • 1970-01-01
      • 2019-03-11
      • 2019-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      相关资源
      最近更新 更多