|
| 1 | +## In-container Java Development: IntelliJ Community Edition |
| 2 | + |
| 3 | +### Pre-requisites |
| 4 | + |
| 5 | +* [Docker for OSX or Docker for Windows](https://www.docker.com/products/docker) |
| 6 | +* [IntelliJ Community Edition](https://www.jetbrains.com/idea/download/) |
| 7 | +* [Java Development Kit](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) |
| 8 | + |
| 9 | + |
| 10 | +### Getting Started |
| 11 | + |
| 12 | +In IntelliJ, clone the repository. Click on `Check out from Version Control` > `Github` |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +If this the first time to use IntelliJ with Github, log into your Github account. |
| 17 | + |
| 18 | + |
| 19 | +Clone the repository [registration-docker](https://github.com/spara/registration-docker.git). |
| 20 | + |
| 21 | + |
| 22 | +Click on `Import project from external model`, select `Maven`. Click `Next` |
| 23 | + |
| 24 | + |
| 25 | + |
| 26 | +Check `Search for projects recursively`. Click `Next` |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | +Select the project and click `Next` |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +Select the JDK and click `Next` |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +Click `Finish` |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +Click on `Project View` to open the project. |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +### Building the application |
| 47 | + |
| 48 | +The application is a basic Spring MVC application that receives user input from a form, writes the data to a database, and queries the database. |
| 49 | + |
| 50 | +The application is built using Maven. To build the application click on icon on the bottom left of the IntelliJ window and select `Maven Projects`. |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | +The `Maven Projects` window will open ont the right side. Maven goals of `clean` and `install` need to be set to build the application. |
| 55 | + |
| 56 | +To set the `clean` goal, click on `Lifecycle` to display the tree of goals. Right click on `clean` and select `Create 'UserSignup [clean]'...` |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +Click `OK` in the `Create Run/Debug Configuration` window. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +Configure the `install` goal similarly. Click on `install` in the Lifecycle tree. Select `Create 'UserSignup[install]'...` |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +Click `OK` in the `Create Run/Debug Configuration` window. |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +To build the application run `clean` |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +Then run `install` |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +When the application builds you will see a success message in the log window. |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +### Running the application |
| 85 | + |
| 86 | +Open a terminal and go to the application directory. Start the application with docker-compose |
| 87 | + |
| 88 | +<pre>> docker-compose up </pre> |
| 89 | + |
| 90 | +Docker will build the images for Apache Tomcat and MySQL and start the containers. It will also mount the application directory (`./app/target/UserSignup`) as a data volume on the host system to the Tomcat webapps directory in the web server container. |
| 91 | + |
| 92 | +Open a browser window and go to: |
| 93 | +'localhost:8080'; you should see the Tomcat home page |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +When the Tomcat image was built, the user roles were also configured. Click on the `Manager App` button to see the deployed applications. When prompted for username and password, enter `system` and `manager` respectively to log into the Tomcat Web Application Manager page. |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +You can use the Manager page to `Start`, `Stop`, `Reload` or `Undeploy` web applications. |
| 102 | + |
| 103 | +To go to the application, Click on `/UserSignup` link. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +### Debugging the Application |
| 108 | + |
| 109 | +In the application, click on `Signup` to create a new user. Fill out the registration form and click `Submit` |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | +Click `Yes` to confirm. |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +Test out the login. |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +Oh no! |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | +#### Configure Remote Debugging |
| 126 | + |
| 127 | +Tomcat supports remote debugging the Java Platform Debugger Architecture (JPDA). Remote debugging was enabled when the tomcat image (registration-webserver) was built. |
| 128 | + |
| 129 | +To configure remote debugging in IntelliJ, click on `Run` > `Edit Configuration ...` |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | +Add a new remote configuration. |
| 134 | + |
| 135 | + |
| 136 | + |
| 137 | +In the `Run\Debug Configurations` window, set the `Name` of the configuration and in `Settings` set the port to '8000' the default Tomcat JPDA debuging port. Clcik on `OK` to save the configuration. |
| 138 | + |
| 139 | + |
| 140 | + |
| 141 | +#### Finding the Error |
| 142 | + |
| 143 | +Since the problem is with the password, lets see how the password is set in the User class. In the User class, the setter for password is scrambled using [rot13](https://en.wikipedia.org/wiki/ROT13) before it is saved to the database. |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | +Try registering a new user using the debugger. In the menu click on `Run` > `Debug...` |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +Choose the remote Tomcat debug configuration. The Debugger console will be displayed at the bottom of the IntelliJ window. |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +Set a breakpoint for in the User class where the password is set. |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +Register a new user with the username of 'Moby' and with 'm0by' as the password, click `Submit`, click `yes` |
| 160 | + |
| 161 | + |
| 162 | + |
| 163 | +IntelliJ will display the code at the breakpoint and the value of password in the variables window. Note that the value is `m0by` |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | +Click on `Resume Program` to let the code run or press `F8` to step over the breakpoint. |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | +Next, set a breakpoint on the getPassword in the User class to see the value returned for password. You can also toggle off the breakpoint for setPassword. |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | +Try to log into the application. Look at the value for password in the debugging console, note that it is `z0ol` which is `m0by` using ROT13. |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +In this MVC application the UserController uses the findByLogin method in the UserServiceImpl class which uses the findByUsername method to retrieve the information from the database. It then checks to see if the password from the form matches the user password. Since the password from the login form is not scrambled using ROT13, it does not match the user password and you cannot log into the application. |
| 180 | + |
| 181 | +To fix this, apply ROT13 to the password by adding |
| 182 | + |
| 183 | +``` |
| 184 | +import com.docker.UserSignup.utit.Rot13 |
| 185 | +
|
| 186 | +String passwd = Rot13.rot13(password); |
| 187 | +``` |
| 188 | + |
| 189 | + |
| 190 | +Set a breakpoint in UserServiceImpl on the findByLogin method. Log in again and look at the values for the breakpoint. The 'passwd' variable is `z0ol` which matches the password for the user moby. |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | +Continue (`F8`) and you should successfully log in. |
| 195 | + |
| 196 | + |
0 commit comments