我想将某些休息端点限制为仅适用于特定组中的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();
}
但它仍然允许不在开发人员组中的用户访问该端点
答案 0 :(得分:0)
您可以将WebSecurityConfigurerAdapter
配置修改为:
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().fullyAuthenticated()
.and()
.antMatchers("/foo").hasRole("developers")
.and()
.formLogin();
}
我不完全确定语法,如果第一条规则会覆盖你的第二条规则,但它会与之相似。
或者,您可以尝试按方法配置安全性,例如this sample。
答案 1 :(得分:0)
@EnableGlobalMethodSecurity(securedEnabled=true)
添加到webSecurityConfig。一旦我这样做,我就可以使用@Secured("ROLE_DEVELOPERS")
,然后该方法仅限于该角色。