Spring Boot Security: Custom AuditEvent listener configuration

17 01 2015

Security best practices requires all Authentication related events to be logged in defined format and sometimes event should be handled in special way.

Spring security has its own Security Event log implementation and default repository (in memory repository) If you need to provide your own implementation you need to add custom configuration class.

The class should implement the generic ApplicationListener interface for specified event type. In my example I listen for all events (AbstractAuthenticationEvent type). If you need less, you can specify one of its subclasses only.

Here I only log the event, but you can implement your business logic as required. Event object has a timestamp, remote IP address, user login and event type:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
import org.springframework.security.core.Authentication;

@Configuration
public class AuditEventLogConfiguration implements ApplicationListener<AbstractAuthenticationEvent> {

  private final Logger log = LoggerFactory.getLogger(this.getClass());
  
  @Override
  public void onApplicationEvent(AbstractAuthenticationEvent event) {
    //do whatever you need with your event
    log.info(event.toString());
  }
}

Did I help you?
I manage this blog and share my knowledge for free, sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Android: remove confirmation dialog window

7 11 2012

Another tutorial today is how to build confirmation dialog window. Android provides API for that. What I am going to build here is dialog window triggered by some action (button click) and react on confirm or cancel button click:

Confirmation dialog example

1. Create dialog’s id constant

This is simply dialog’s ID. it should be unique among any other dialog window that is handled by current activity. I set it like that:

protected static final int DIALOG_REMOVE_CALC = 1;
protected static final int DIALOG_REMOVE_PERSON = 2;

2. Build dialog

I use this method to build dialog window:

private Dialog createDialogRemoveConfirm(final int dialogRemove) {
	return new AlertDialog.Builder(getApplicationContext())
	.setIcon(R.drawable.trashbin_icon)
	.setTitle(R.string.calculation_dialog_remove_text)
	.setPositiveButton(R.string.calculation_dialog_button_ok, new DialogInterface.OnClickListener() {
		public void onClick(DialogInterface dialog, int whichButton) {
			handleRemoveConfirm(dialogRemove);
		}
	})
	.setNegativeButton(R.string.calculation_dialog_button_cancel, null)
	.create();
}

AlertDialog builder pattern is utilized here. I do not handle NegativeButton click action – by default the dialog is just being hidden. If dialog’s confirm button is clicked, my handleRemoveConfirm callback is called and action is performed based on dialog’s ID:

protected void handleRemoveConfirm(int dialogType) {
	if(dialogType == DIALOG_REMOVE_PERSON){
		calc.removePerson();
	}else if(dialogType == DIALOG_REMOVE_CALC){
		removeCalc();
	}
}

3. Show Dialog

I show dialog after my remove button click. The showDialog(int) is Android’s Activity’s method:

OnClickListener removeCalcButtonClickListener = new OnClickListener() {
	public void onClick(View v) {
		showDialog(DIALOG_REMOVE_CALC);
	}
};

the showDialog(int) method calls onCreateDialog (also defined in Activity’s class). Override it and tell your app what to do if the showDialog was requested:

@Override
protected Dialog onCreateDialog(int id) {
	switch (id) {
	case DIALOG_REMOVE_CALC:
		return createDialogRemoveConfirm(DIALOG_REMOVE_CALC);
	case DIALOG_REMOVE_PERSON:
		return createDialogRemoveConfirm(DIALOG_REMOVE_PERSON);
	}
}

Here I have two dialogs possible. Both are remove dialogs, however their confirm button handlers are different.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!





Android jUnit test for asynchronous tasks

24 03 2012

Android dialog is example of using asynchronous background tasks in Android. In my case it was downloading data from server and a dialog box with “Please wait…” text. Here is a good AsyncTask example, and below – how it goes in my case:

  1. AsyncTask is started in activity OnStart() along with showing dialog box
  2. Data is being downloaded in inner class extending AsyncTask
  3. After data is downloaded the dialog box is dismissed and further data processing initialized (in the OnPostExecute() method)

Everything works very well until it comes to jUnit testing. The cause is dialog box invocation that throws an exception:

java.lang.IllegalArgumentException: View not attached to window manager

The cause is that displaying and hiding dialog box causes updating the activity, so the activity must exist also when dialog box is being dismissed.

jUnit test is running so fast that the activity is being destroyed before the dialog box is dismissed in AsyncTask. Then it would be great to hold on the test execution (even if everything was already tested) until the dialog box will disapear and then destroy the whole activity. The solution is small lib – Awaitility.

Awaitility enables to wait until some condition is met or the timer is exceeded. So there is possibility to check if dialog box was dismissed and hold test execution until it happens – with few lines of code. See the simpliest samples from Awaitility site.

In my case I used just:

Awaitility.await().until(dataIsDownloaded());

along with a flag in tested activity, indicating if data downloading has finished. dataIsDownloaded() just checks if this flag is set to true. I put this lin of code in tearDown() method, as data downloaded is not needed for my test – i just have to wait for a dialog box to be dismissed until finishing the activity under tests.

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!








%d bloggers like this: