RSS

Category Archives: Spring

Spring Security Basics

Episode 63

Welcome to the next installment of the series about Spring-based web applications development. So far we have covered Angular JS fronted, Spring core, webservices, database access and unit tests. Today we are going to take care of our application security – basic authentication and authorization, users, roles, custom login form and method level security.

70d42d4aaa6aede4b84bde43e3dead63

Spring Security project started as Acegi security around 2004 and initially focused on custom authorization, using standard Java Enterprise Edition container managed authentication. Version 1.0.0 became official Spring sub-project in 2006 and year later was re-branded to Spring Security. Say hello to Alice, Bob and Eve.

Foundations

We should briefly clarify some security terminology, which might sometimes by confusing:

Identification is stating a subject identity, like user name, without yet providing any proof for that (Hi, I’m Alice). Read the rest of this entry »

 
Leave a comment

Posted by on March 30, 2017 in Spring, Technology

 

Tags: , , ,

Unit Testing Basics

Episode 62

Previous episode was about talking to database in Spring. Today we will continue exploring basics of building web applications, and talk about unit testing. This time, however, we are going to leave Spring alone for a moment and focus on plain unit tests using JUnit, Mockito and DataProvider.

4d821599ecc582a1ab14fe16e0594e27

As usual, all the code presented in this article is available on my GitHub in the same project as before. This tag corresponds to project state at the time of writing this article. Let’s start with some basic ideas, and why write unit tests in the first place.

Client does not pay for unit tests

Well, I have heard that once. I was in a project where code coverage was around 2%, and when I was actively trying to improve upon that, manager took me to a one on one discussion to a small room. Starting to cover old projects with tests is often difficult, but in the end the business pays much more for not having decent automatic test harness than saving on skipping it and only producing features. It simply pays back in the long run. There are less unnoticed bugs, design is cleaner, the amount of tedious repetitive work is dramatically reduced. Often it is much faster to Read the rest of this entry »

 
3 Comments

Posted by on March 23, 2017 in Clean Code, Spring, Technology

 

Tags: , , ,

Spring Data JPA Basics

Episode 61

In the previous episode, we covered basic concepts of web services in Spring framework. Today we will build upon that foundation and take a look on how to transfer some data between our Spring Boot application and the database. Getting started with this part requires just a bit of effort to set up the aforementioned database and configuring our application, so the two can talk to each other, but no worries I will go through this step by step.

wallup-13611.jpg

As usual, all the code presented in this article is available on my GitHub in the same project as before. This tag corresponds to project state at the time of writing this article. Before we dive into the configuration and code, let’s start with some underlying concepts, namely ORM and JPA.

Foundations

ORM, or Object Relational Mapping is a technique of mapping data between objects in object oriented languages and relational databases. Complications stem from the fact, that objects form graphs in the mathematical sense, and relational databases comprise of Read the rest of this entry »

 
6 Comments

Posted by on March 16, 2017 in Spring, Technology

 

Tags: , , ,

Spring Core Basics

Episode 58

This week I was planning to publish introduction to Amazon Web Services, but since I’m working on preparing Spring-based web applications development course for Wrocław University of Technology students with my two colleagues, I decided to change the schedule a bit. I planned to write few articles about Spring framework anyway, but in order to fit everything before particular classes in March and April I need to start exactly now.

a1dac3474b1a296e8ec8b14647ec7798.jpg

I wrote a Spring Hello World article over two years ago actually, but today I wanted to talk a bit more about some fundamental concepts: Inversion of Control and dependency injection. Let us take a closer look at application structure, configuration, components and wiring. There will be quite a lot of code snippets. If you would like to play with complete application working out of the box, visit my GitHub project Spring Angular Intro. The application is based on the one I prepared for Angular Intro article in September 2016, but now we will focus on back-end exclusively. This tag corresponds to project state at the time of writing this article.

Concepts

Classic approach to program structure is that we write custom, high-level code which calls lower level code.  The flow of control goes from our code to a generic framework or libraries. Inversion of control is a technique, where Read the rest of this entry »

 
2 Comments

Posted by on February 23, 2017 in Spring, Technology

 

Tags: , , , ,

From Java Source to Bare Metal, Part One: An Unexpected Request

Episode 49

Imagine you are developing a web application based on a typical modern technology stack. In essence, the goal is to create something that receives requests from the network, processes them, and responds to them with some kind of structured data. It can be a fancy HTML webpage to be displayed in client’s web browser or it can be a raw text object to be consumed by another application web API. Have you ever wondered what exactly is going on behind the scenes? What is happening between the moment when the code you wrote is executed and the moment when electrical impulses jumps the network cable sticking out of that metal box in the data center? Let me take you on the journey along numerous layers of abstraction in modern software stack that must be bypassed to make things happen.

There and Back Again: Abstractions

Programming is all about abstractions. We endlessly put one layer above the other in order to deal with tremendous complexity of software and hardware. Often, we ignore most of the layers to focus on solving the actual problem at hand. In principle, sending a JSON object over the network might seem like an easy task with modern tools and frameworks. You generate the project, tweak just a little bit here and there, write one method with few annotations and there you go, it works. But the engineering problem of making this possible in an easy, fast, secure, reliable, scalable and manageable way is gargantuan. Looking at the big picture, it’s probably an effort of hundreds of thousands of software developers, architects, electronic and electrical engineers, computer scientist and mathematicians, spanned over several decades of work. All that, to let you do the job in a single pomodoro.

248900

Of course, to develop decent software, you don’t necessary have to understand exactly how all this works, it’s probably not even possible for a single human being to grasp all that in every detail. I believe however Read the rest of this entry »

 
5 Comments

Posted by on December 22, 2016 in Spring, Technology

 

Tags: , , , , ,

Angular Intro

Episode 34

Talk is cheap. Show me the code.

– Linus Torvalds

          I’ve talked about recruitment and clean code recently, but I haven’t shown you any (or almost any) actual code for some time. Time to fix this. In November 2014 I’ve written articles about GWT and Vaadin. Now I’d like to present you something similar on AngularJS, which seems to be the most popular JavaScript framework nowadays. And despite being JavaScript, it’s actually pretty good. Let’s take a look.

Overview

AngularJS-huge

          Angular is a JavaScript open source front-end, single page, web application framework maintained by Google.

          It provides a standard structure for front-end project part, like GWT, much as Spring provide standard structure for back-end part. Its simplification, because you can do the front-end in Spring too via Spring MVC, and the structure for entire project is also partly defined by Maven or other convention over configuration type build tool, but it’s an idea to start with.

          It is single page framework, also like GWT, meaning Read the rest of this entry »

 
10 Comments

Posted by on September 8, 2016 in News, Spring, Technology

 

Tags: , ,

Dreams of Spring

Episode 21

Going back to my project, which was a bit on hold for some time, I finally some time to to move things forward a bit. Just a bit, as you may recall I made a decision to use Spring as a framework and GWT for view. I’ve worked with both for quite some time, but apparently never tried to do anything from scratch. Let’s fix that and start with Spring.

cropped-logo_springbypivotal_horizontal1-645x300

Inspired by Josh Long’s talk on last Jax conference, I’ve checked the Spring Boot. There is a nice website which asks you for:

  • Few descriptive details of your project
  • If you want to use Maven or Gradle. As I mentioned earlier, I will go with Maven.
  • If you want Jar or War. As usually War is the solution.
  • Java or Groovy. Uhm.
  • Java and Spring Boot version

And then several groups of dependencies you can include in your project, like:

  • AOP and Security
  • Data: JDBC, JPA, MongoDB etc
  • I/O: Batch, JMS etc
  • Web: Web application, Websockets, WebServices…
  • Template engines
  • Social: want Facebook integration out of the box? No problem.

For online hello world you probably only need to check web. After filling in all stuff simply hit Generate Project, download, unzip, and put in your Eclipse workspace. Then import from existing Maven project and here it is. You don’t need to download anything and figuring out where to put this and what to add to some eerie xml files. Maven will handle it for you.

To see something in runtime, just put any html page in src/main/ webapp, Add application to your build-in Tomcat, publish and start. Watch console, If everything went smoothly, you should see server starting in less than a second, and then a lovely Spring Boot ascii artish logo followed by some info outputs over a few seconds. You may now check http://localhost:8080/appname/pagename.html and it should present you the very page you have created earlier. Congratulation, you have now Spring application that does nothing useful but starts twenty times longer than simple dynamic web application! What an achievement. Let’s do something with that.

First let’s see what was generated for us.

  • java, annotated with @Configuration, @ComponentScan and @EnableAutoConfiguration and containing good old main method. You don’t actually need any server to run Spring, you can just run this class as Java Application atop of pure JVM.
  • java – used to bind Spring as http request handler when run on server.
  • Some empty junit test for Application class.
  • pom.xml, Maven project definitions and dependencies

Time to add something. We will create a very simple MVC to display “Hello World!” from the depths of backend.

Our back-end in this case will be class marked with @Component annotation, which allows Spring to manage its injection in the Controller class, annotated with, well, @Controller.

service

Controller has a field which will be initiated with our back-end buddy. It also has a method to handle request, and we will annotate this with @RequestMapping(“/*”) meaning it will catch all http requests to our pretty little application. Now we need to return from the method something that will indicate where the request should land, and that’s the job of ModelAndView object initialized with name of page to display.

To tie back-end and front-end together, we will add a model attribute with value provided from our component.

controler

As quick and dirty view we will create a JSP (sorry for that) page, that will reference model with el expression ${message}. Simple as that.

Publish, restart server, type http://localhost:8080/appname/blah in the browser and here it is, your first Spring MVC web application, sweet and shining.

fb125-how-to-train-your-dragon-2-toothless-cute-art-wallpaper

 
3 Comments

Posted by on November 2, 2014 in BookSentry, Spring, Technology

 

Tags: , , ,