Acegi RoleVoter

Adding new role to Acegi secured application may be painful sometime.

Tip of the day: by default RoleVoter is only accepting roles with names starting with prefix ROLE_ (take a look on javadoc here). You easily can change this prefix while defining RoleVoter bean (this is usually configured in file WEB-INF/security.xml)


That’s all for today about Acegi 😉

Capturing log-in event in Acegi

Adding some custom code that is supposed to be launched while user logs in it isn’t available out of the box but it can be done easily. In order to do this you have to implement interface org.springframework.context.ApplicationListener.

public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof AuthenticationSuccessEvent) {
        AuthenticationSuccessEvent authEvent = (AuthenticationSuccessEvent) event;
        String username = authEvent.getAuthentication().getName();
        // Any custom logic

Such listener has to be registered. In order to do this – just create a bean in xml configuration file where other Acegi related beans are declared (usually security.xml or applicationContext-security.xml).

<bean class="my.package.SomeExampleListener">
    <property name="someExampleManager" ref="someExampleManager"></property>

You can inject any required dependencies here.

That’s all!