Spring Security PreAuthorize自定义方法Bean解析器未注册?

时间:2014-08-21 09:08:07

标签: spring security methods spring-security spring-java-config

我只是学习Spring,经历教程和测试可能性。我的目标之一是使用自定义方法和PreAuthorize注释来保护服务方法。不幸的是,持有自定义方法的Bean无法解决,我不知道为什么。也许有人可以一眼看出错误。

Bean持有自定义方法:

@Component("mySecurityService")
public class MySecurityService {
    public boolean hasPermission() {
        return true; //simple implementation just to look if has permission is called
    }
}

安全服务:

public interface OrderService {

  @PreAuthorize("@mySecurityService.hasPermission()")
  public AllOrdersEvent requestAllOrders(RequestAllOrdersEvent requestAllCurrentOrdersEvent);

  public OrderDetailsEvent requestOrderDetails(RequestOrderDetailsEvent requestOrderDetailsEvent);

  public OrderStatusEvent requestOrderStatus(RequestOrderStatusEvent requestOrderStatusEvent);

  public OrderCreatedEvent createOrder(CreateOrderEvent event);

  public OrderUpdatedEvent setOrderPayment(SetOrderPaymentEvent setOrderPaymentEvent);

  public OrderDeletedEvent deleteOrder(DeleteOrderEvent deleteOrderEvent);

}

Java安全配置:

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
       .withUser("letsnosh").password("noshing").roles("USER");
  }

  @Override
  @Bean
  public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
  }

  @Bean(name = "mySecurityService")
  MySecurityService createSecurityService(){return new MySecurityService();}

@Override
  protected void configure(HttpSecurity http) throws Exception {
     /*
   http.authorizeUrls()
    .antMatchers("/aggregators*//**//**").hasRole("USER")
    .anyRequest().anonymous()
    .and()
    .httpBasic();
      */

  }
}

错误:

 No bean resolver registered in the context to resolve access to bean 'mySecurityService'

1 个答案:

答案 0 :(得分:2)

您好我解决了这个问题。它与Spring Security版本相关联。

我从官方的Spring Rest Tutotrial获得版本: 3.2.0.M2

在这个版本中,我必须按如下方式声明安全上下文:

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void registerAuthentication(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("letsnosh").password("noshing").roles("USER");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeUrls()
        .antMatchers("/aggregators/**").hasRole("USER")
        .anyRequest().anonymous()
        .and()
        .httpBasic();
  }
}

这里引发了错误。

但使用较新版本的Spring Security: 3.2.5.RELEASE 我可以这样声明Config:

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }

    @Override
  protected void configure(HttpSecurity http) throws Exception {

   http.authorizeUrls()
        .antMatchers("/aggregators*//**//**").hasRole("USER")
        .anyRequest().anonymous()
        .and()
        .httpBasic();


  }

可以直接在MySecurityService类上使用@Component Annotaions或在返回MySecurityService实例的config类方法上使用@Bean注释来解析bean。

相关问题