Archive

Archive for the ‘Java’ Category

How to call a secured remote EJB on Glassfish 2

18/02/2016 Leave a comment

I’ve recently needed to call an EJB deployed on a Glassfish 2.1 server from a standalone Java application.

The EJB was secured, under a custom security realm. I had trouble finding answers on-line regarding how the secured EJBs are supposed to be called, and, after a lot of work I finally found the solution with the programmatic login. I suppose it’s a basic thing to know, but given how long I’ve spent googling for an answer, I figured an extra information source can’t be that bad.

Read more…

Advertisements

Optimizing response time for JSON queries

11/07/2014 Leave a comment

I’ve recently been in the situation where I needed to reduce the loading time of some JSON queries. I’ve remembered some very practical and easy to implement strategies to significantly reduce the response time.

Read more…

How to configure validation in Spring MVC controllers

30/05/2014 Leave a comment

Validation is the action through which the application checks that data respects logical (e.g. empty fields, invalid formatted values) and business (e.g. min/max values) constraints.

In order to configure validation for you model objects in Spring, you can follow these steps:

  1. Import the appropriate validation specification and validation implementation jars
  2. Define a class which implements the org.springframework.validation.Validator interface
  3. In the controller method signature, you must annotate the model attribute you’re trying to validate with @Valid (javax.validation.Valid)
  4. In the controller method, you must check whether errors have been identified
  5. If errors are found, reload the page and display the error messages.

In order to have an example on how this works, I’ve written a small test project, using Maven. I am using a jsp page which contains spring:form tags and an in-memory list of users, in order to simulate data access.

Read more…

How to configure Spring Security using annotations

24/04/2014 Leave a comment

As far as integrating Spring Security into a Spring enabled project, there are 2 possibilities, both presented in detail on-line, in the reference documentation:

1. using Java Configuration, see details here http://docs.spring.io/spring-security/site/docs/3.2.3.RELEASE/reference/htmlsingle/#jc. This is very useful for applications using the Servlet 3.0 specification, which already register all their servlets by using annotations and which don’t have a web.xml.

2. using XML Configuration, see details here http://docs.spring.io/spring-security/site/docs/3.2.3.RELEASE/reference/htmlsingle/#ns-getting-started. This approach is very useful for applications using specification preceeding Servlet 3.0 or simply for those who use the web.xml for configuration.

My use case is a combination of the both. I have created a web application using Spring and Spring MVC. I have configured my DispatcherServlet in the application’s web.xml. I wanted to use only Java configuration, so I’ve been using an AnnotationConfigWebApplicationContext. When I tried to add Spring Security, I wanted to keep the same approach: Spring Security filterregistered in the web.xml, while all the configuration code would be written in Java. The problem was that the reference documentation doesn’t show how to build such a project (or maybe I missed it). Googleing for a how-to also didn’t return any satisfactory results.

Read more…