Posts

Explore Spring 4 Features: WebSocket (Part I)

First of all Wish you a Very Happy New Year WebSocket with Spring 4 One of the interesting features of Spring 4 is introduction of WebSockets in Spring. Now the first Question which arises is what is WebSocket? Definition: WebSocket is a protocol which allows for communication between the client and the server/endpoint using a single TCP connection. This protocol is an important new capability for web applications: full-duplex, two-way communication between client and server. It aims at making web more interactive including Java applets, XMLHttpRequest. An abstract of the actual process which happens behind the scene for WebSocket is: During initialization of this protocol,HTTP is used only for the initial handshake, which relies on a mechanism built into HTTP to request a protocol upgrade (or in this case a protocol switch) to which the server can respond with HTTP status 101 (switching protocols) if it agrees. Assuming the handshake succeeds the TCP socket unde...

Explore Spring 4 Features - Part 3

In this Post I will Capture the following features: 1) Controller Advice with Custom Annotation 2) Capturing Attributes in ModelAndView with the help of ModelMap while forwarding request from One Controller to another.(Not a Spring 4 feature) Part1: By Using @ControllerAdvice annotation we can apply an Advice across all the controllers in an application. Here our objective is to apply an advice not to all the controllers in an Application Context, but to them annotated with a custom annotation @NoControllerAdviseAnnotated in this case. The advice that would be applied out here is @ExceptionHandler(Exception.class) for Exception class. Our Code for ControllerAdvice is as follows: @ControllerAdvice(annotations=NoControllerAdviseAnnotated.class) public class CustomControllerAdvice { @ExceptionHandler(Exception.class) public ModelAndView handleException(Exception exception,WebRequest request) { ModelAndView mv = new ...

Explore Spring 4 Features - Part 2

Just Starting from the point where I left in the last POST The content of aspect-config.xml is: Here MyAspect is a AspectJ class: public aspect MyAspect { private DataInjectObject injectedData; public DataInjectObject getInjectedData() { return injectedData; } public void setInjectedData(DataInjectObject injectedData) { this.injectedData = injectedData; } pointcut incept1() : execution(* com.springjavaconfig.controller.BaseControllerNew.secureMethodNew(..,DataInjectObject)); pointcut incept2() : execution(* com.springjavaconfig.controller.BaseController.getEmployeeForm()); Object around() :incept2() { System.out.println("################################## \n\n"); System.out.println("####### ASPECTS IN ACTION ####### \n\n"); System.out.println("################################## \n\n"); System.out.println(" ****** Injected DATA:"+injectedData); return proce...

Explore Spring 4 Features

The recent Spring Version is 4 , which is fully integrated with Java 8 . It has Got a lot of features in it bucket now. In this Blog I am going to explore some of its features with some insight. First thing that I will start with is: The Spring Java Config . Recently I have been working on Spring Java Config, I have been trying to migrate one of my existing Spring Projects (Which had Spring-Security 3.2, Aspects and other features) into a fully Java Based Configuration . My objective is,there is going to be no XML configuration, even web.xml . Spring Java config, though it is not a part of Spring 4. It has been introduced in Spring 3.2 . I will be creating a project with no .xml files bearing any Spring based configuration. All the configuration will be moved to Java. In the cloud there are actually a lot of posts bearing a lot of useful information regarding this Spring Java Based configuration and lot of them are really useful but I really had hard time in c...

Issues Configuring Spring Method Level Security (Annotations)

I this post, I am going to discuss about some issues that I faced while configuring method level security with Annotation like @PreAuthorize, @PostAuthorize etc. The scenario is that I am trying to secure the calls to my controller methods through Annotation Based Method level security of Spring Security module. One of the parameters of the @PreAuthorize is the method parameter of Spring controller which I am passing with the help of Expression language and the parameter is being confugured through WebArgumentResolver of Spring as discussed in my previous post. @PreAuthorize("hasPriviledge( #dataInj, 'TxnClm5013','read')") @RequestMapping(value="/SecureMethodNew" , method=RequestMethod.GET) public ModelAndView secureMethodNew(HttpServletRequest request,@DataInject("dataInject") String dataInject123, @DataInjectNew DataInjectObject dataInj ) throws Exception { I am going to discuss my discussion into two sections: ...

ArgumentResolver Spring

Recently I came across Argument Resolver in Spring. It has got two versions: 1) Spring 3.2 &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp2) Spring 4.0 The scenario was such that for every handler method, I need a particular Object in every method. So, with the help of Spring Argument Resolver Architecture, I annotate that Particular Type with a Custom annotation for identifying that Particular Type and populating it. Without annotation By inspecting Only the type of the Parameter too, this end could be achieved. The configuration is as follows: Spring 3.2: &ltbean class="org.springframework.web.servlet.mvc.annotation...

Spring Security - Annotation Based Method level Security Handler - Part2

Now lets explore the code a little deeper to get a better understanding of how Spring in a best way performs all the Task. So from the above configuration it is quite evident that at the heart lies the CustomMethodSecurityExpressionHandler , which incorporates a permissionEvaluator i.e. DefaultPermissionEvaluator and priviledgeEvaluator i.e. CustomPriviledgeEvaluator The code for permissionEvaluator is: public class DefaultPermissionEvaluator implements PermissionEvaluator { @Override public boolean hasPermission(Authentication auth, Object arg1, Object arg2) { // TODO Auto-generated method stub System.out.println(" *******1) The Authentication is:"+auth+"\n"+arg1+"\n"+arg2); return true; } @Override public boolean hasPermission(Authentication auth, Serializable arg1, String arg2, Object arg3) { // TODO Auto-generated method stub System.out.println(" ******* 2) The Authentication is:"+auth+":...