【问题标题】:Spring Security / rolesAllowed / antMatchersSpring Security / rolesAllowed / antMatchers
【发布时间】:2013-12-30 00:50:33
【问题描述】:

我正在使用 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 添加安全性?关于如何让它发挥作用的想法?

谢谢

【问题讨论】:

    标签: spring spring-security spring-data-rest


    【解决方案1】:

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

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

    现在 RolesAllowed 注释工作正常。

    【讨论】:

      猜你喜欢
      • 2021-02-23
      • 2019-09-27
      • 1970-01-01
      • 2018-07-17
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2015-02-23
      • 2020-02-27
      相关资源
      最近更新 更多