Skip to content

@EventListener doesn't work when @PreAuthorize is present on any interface method implemented by the same class [SPR-13866] #18439

@spring-projects-issues

Description

@spring-projects-issues

Ruslan Stelmachenko opened SPR-13866 and commented

The @org.springframework.context.event.EventListener annotation doesn't work when @org.springframework.security.access.prepost.PreAuthorize is present on any method of interface implemented by the same class.

Also it is true if @PreAuthorize is present on class method which implements some interface method without annotations.

For example:

public interface MyService {

	@PreAuthorize("hasAuthority('READ_WORKDAY')")
	public void restrictedMethod();

}

@Service
public class MyServiceImpl implements MyService {

	@Override
	public void restrictedMethod() {
	}

	@EventListener
	public void processBlackListEvent(BlackListEvent event) {
		System.out.println("MyServiceImpl processBlackListEvent: " + event);
	}

}

With this code processBlackListEvent method is never called when publishing BlackListEvent. Also we can place @PreAuthorize annotation to the implementation method and remove it from interface. In this case @EventListener also doesn't work.

If we remove @PreAuthorize then it starting to work again.

The old way with implementing ApplicationListener interface and overriding onApplicationEvent method also works fine.


Affects: 4.2.2

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions