繁体   English   中英

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

[英]Spring Security PreAuthorize Custom Method Bean resolver is not registered?

我只是学习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'

您好,我解决了这个问题。 它已连接到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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM