1

我正在使用 Spring Data REST,并且我的存储库中有一个 find 方法:

public List<Contact> findByLastNameOrderByLastNameAsc(@Param("lastName") String lastName);

我正在尝试为该方法增加安全性,但没有运气。在我的数据库中,我有 1 个角色为“ROLE_USER”的用户。当服务启动时,登录表单出现,我可以使用数据库中的凭据登录。

这是我的网络安全配置:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

    auth.jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery("select username,identification,enabled from users where username = ?");


  }

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
    .authorizeRequests()
    .antMatchers("/contacts/findByLastNameOrderByLastNameAsc").hasRole("ADMIN")
    .antMatchers("/contacts/**").fullyAuthenticated()
    .antMatchers("/contacts/**").hasRole("USER")
    .anyRequest().authenticated()

    .and()
     .formLogin();
}

当我尝试在我的存储库中调用该服务时,我没有看到任何身份验证错误。使用我的浏览器,即使数据库中的用户没有“管理员”角色,URL 也可以正常显示。

我尝试将“RolesAllowed”添加到我的存储库中的方法,但没有运气:

@RolesAllowed(value = { "ADMIN" })
public List<Contact> findByLastNameOrderByLastNameAsc(@Param("lastName") String lastName);

我是否要正确地为 Spring Data 提供的 REST API 添加安全性?关于如何让它发挥作用的想法?

谢谢

4

1 回答 1

5

FWIW:我忘了添加 jsr250 支持。所以,我添加了这个配置:

@Configuration
@EnableGlobalMethodSecurity(jsr250Enabled = true)
public class MethodSecurityConfig {

}

现在 RolesAllowed 注释工作正常。

于 2013-12-13T16:38:00.033 回答