【问题标题】:Spring Security @PreAuthorize access to autowired bean using SpEL language [duplicate]Spring Security @PreAuthorize 使用 SpEL 语言访问自动装配的 bean [重复]
【发布时间】:2019-04-02 09:49:50
【问题描述】:

我想使用 SpEL 语言在 Spring Security @PreAuthorize 下访问我的自动装配 bean。

@Component
@Transactional
public class TodoDao implements ITodoDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Autowired
    private AuthenticationFacade authenticationFacade;

    @Override
    @PreAuthorize("...") // I want to access to one of my autowired bean here
    public void changeTodoStatus(Todo todo) {
        Object user = authenticationFacade.getAuthentication().getPrincipal();
        todo.setDone(!todo.isDone());
        sessionFactory.getCurrentSession().update(todo);
    }
}

【问题讨论】:

    标签: java spring spring-security annotations spring-el


    【解决方案1】:

    在您的 bean 名称前使用“@”:

    @Component
    @Transactional
    public class TodoDao implements ITodoDao {
    
        @Autowired
        private SessionFactory sessionFactory;
    
        @Autowired
        private AuthenticationFacade authenticationFacade;
    
        @Override
        @PreAuthorize("@authenticationFacade.(#toDo)") // I want to access to one of my autowired bean here
        public void changeTodoStatus(Todo todo) {
            Object user = authenticationFacade.getAuthentication().getPrincipal();
            todo.setDone(!todo.isDone());
            sessionFactory.getCurrentSession().update(todo);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-25
      • 1970-01-01
      • 2020-06-03
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      相关资源
      最近更新 更多