Thursday, March 17, 2011

Running Spring Security's Tutorial Sample in Spring Tool Suite

Note
UPDATE If you are using Spring Security 3.1.2+ you should use the Gradle Eclipse Plugin instead of gradlew eclipse
Note: Vote on these JIRA's if you would like STS to be able to import Spring Security without use of the command line.
Introduction

This blog will teach you how to run the sample applications/tests for Spring Security 3.1.x in STS. It is assumed that you have already installed STS. Since STS is a flavor of Eclipse, the process for doing this in Eclipse is similar.

The main goal is to demonstrate how to setup a workspace with STS. In later posts I will describe how to run the other sample applications within STS. Once you can run Spring Security in an IDE, I will describe how to contribute back to Spring Security.

Screencast

If you want to see this in action, you can view the screen cast too.

STS Setup

Spring Security uses git to manage the source code. There are quite a few different git clients to choose from, but in this blog I will demonstrate how to obtain the source using EGit.

Some of the tests are written in Groovy. If you want to run the tests, you will need to install the Groovy Eclipse plugin.

To install the plugins:
  • Open up STS to a new workspace
  • You should see an option to Open Dashboard
  • At the bottom of the Dashboard View select the Extensions tab
  • In the Find box type in EGit and select the check box next to it
  • In the Find box type in Groovy Eclipse and select the check box next to it
  • Click the Install button in the lower right
  • Follow the instructions on any dialogs that pop up
  • After everything is installed, restart STS
Checking Out the Source Code
  • Once STS starts back up navigate to File->Import->Git->Projects from Git
  • Click the Clone... button
  • Fill out the URI with git://github.com/SpringSource/spring-security.git
  • Click the Next button to view the branches for this repository
  • Ensure that all the branches are selected
  • Click the Next button again to clone all the branches
  • Accept all the defaults and click the Finish button
  • The repository will be cloned and the local location will be displayed next to it
  • Remember the path to the repository as the Destination Directory
Generating the Eclipse Configuration Files
  • Open up a command prompt and navigate to the Destination Directory from the previous step
  • When using EGit, I have to add the execution bit the gradle wrapper. Since this does not happen when I use git from the command line, I suspect it is an EGit bug. To change the permissions in a Linux environment execute chmod +x gradlew from the command line.
  • Linux/Mac users run ./gradlew eclipse
  • Windows users run .\gradlew.bat eclipse
  • Gradle will automatically be downloaded, installed, download the required dependencies, and then the Eclipse configuration will be generated
Importing the Projects
  • Navigate back to Eclipse and click the Next button
  • Ensure Import Existing Projects is selected
  • Click the Next button
  • Click Select All
  • Click the Finish button
  • The project will be imported and there should be no errors.
Running the Code

At this point you should be able to run all of the unit tests by right clicking the project and navigating to Run As -> JUnit Tests. Of course that is not all that fun, so instead we will run the tutorial sample on tc Server. To run the tutorial follow these steps:
  • Right click spring-security-samples-tutorial and select Run As -> Run on Server
  • Select Finish
  • When prompted if you want to enable Spring Insight click No
  • The application will be opened up in Spring Tool Suite
  • When prompted to login, you can use the users defined in spring-security-samples-tutorial/src/main/webapp/WEB-INF/applicationContext-security.xml (i.e. rod / koala)

What Next?

You can read about Running Spring Security's CAS and PreAuth Samples in STS

42 comments:

Ornot said...

Not very encouraging: When I launch the sample app you describe (with tc Server, virgo hiccups with noClassDef), I reach a page containing the following text:

If you're logged in, you can list accounts.

Where list accounts is a link. Interestingly, clicking on that link allows me to view the accounts page WITHOUT being logged in. Hmm... :)

Rob Winch said...

I'm not sure about the noClassDef you are experiencing, but it sounds like you are trying to use Virgo as apposed to tc Server. Perhaps this is the cause? What is the full exception you are experiencing?

In terms of being able to access the list accounts page without being logged in, you have found a bug in the tutorial sample application. I have logged SEC-1757 and fixed the issue. If you perform a git pull you should get the updates and see the wording has been updated to reflect that listing the account can be done by anyone. Thanks for pointing this out :)

Dracula_L said...
This comment has been removed by the author.
Jcrada said...

Great Post!! the bat file name for windows is wrong

Levy said...

Hi,
Nice post. Unfortunately, I get errors in most of the projects.
I use sts 2.7.2 and I get meny errors similar to this one: "Referenced file contains errors (http://www.springframework.org/schema/security/spring-security-3.1.xsd). For more information,
right click on the message in the Problems View and select "Show Details..."
In the detailes I see that some elements are not terminated.
I use MAC (I didn't choose it...) and JavaSE 1.6

Jason Jia said...

Very good post! However, I cannot make gradlew work.
After check out code via git, I tried to generate the Eclipse configuration Files, but end up with infinite waiting for loading:

C:\Users\Jason\git\spring-security>gradlew eclipse
Download http://repo1.maven.org/maven2/xml-resolver/xml-resolver/1.2/xml-resolve
r-1.2.pom
Download http://repo1.maven.org/maven2/org/apache/apache/3/apache-3.pom
> Loading

I'm using windows 7 with java 1.6.0_26

Rob Winch said...

@Jcrada

Thanks for the feedback. I have updated the post.

@Levy

Are you able to produce the errors with the latest 2.8.0.M2 release and the latest version of code? If so this is probably a MAC specific issue. If you could post the errors perhaps that would shed some light on the issues.

@Jason Jia

Are you behind a proxy of some sort? What happens if you try to copy the URL into your browser?

Levy said...

I get the same problem with sts 2.8.0.m2
under "markers" i get 26 xml errors selecting the first one, i get in the status bar:
Referenced file contains errors (http://www.springframework.org/schema/context/spring-context-3.0.xsd). For more information, right click on the message in the Problems View and select "Show Details..."
in the details dialog i get:
The errors below were detected when validating the file "spring-context-3.0.xsd" via the file "contacts-servlet.xml". In most cases these errors can be detected by validating "spring-context-3.0.xsd" directly. However it is possible that errors will only occur when spring-context-3.0.xsd is validated in the context of contacts-servlet.xml.

in the description I get:
"The element type "xsd:schema: must be terminated by the matcing end-tag "</xsd:schema". [location - line 80]

file://Users/Levy/git/spring-security/samples/contacts/src/main/webapp/WEB-INF/contacts-servlet.xml

Rob Winch said...

@Levy

I am not seeing these problems when using Linux. I do not own a Mac, so it will be difficult for me to troubleshoot on a Mac. Do you do anything different/additional to the instructions in the blog post? Have you tried running it to see if it works?

Levy said...

I managed to reproduce it on my linux (ubuntu 11.04)
Now I suspect somthing is wrong with the JDKs (on the machine I have and openJDK and sun JDK). The STS used SUN's JDK, but I am not sure what was used by gradlew in the first place and whether gradlew really cleaned everything.
Which JDK do you use?

Thanks!

Rob Winch said...

@Levy

I just verified it works with a fresh clone using Sun 1.6.0_27 JDK for both STS 2.8.0 and Gradle. Gradle will use JAVA_HOME and then try to discover the java install location based upon your path. I even tried to explicitly verify the contacts-servlet.xml file and it validated successfully.

Have you tried running the application? If so does it work? If it does not work, what errors do you get? Do you have problems with other Spring configuration files?

Levy said...

I prepared a fresh ubuntu 11.10 installation
removed Open JDK
Installed SUN SDK6 with JDK7 and glassfish3
fixed links and JAVA_HOME
installed STS 2.8.0.RELEASE
installed groovy eclipse extension
cloned spring security
run Gradle
imported projects from the git
and validation failed
program didn't event start to run

Unknown said...

Hi Rob, there is a step that you do in your video demonstration that you haven't included in your write-up. The missing step is running the JUnit tests for spring-security-config. I am assuming that this step, among other things, configures class and source paths for all imported projects.

Levy said...

Thanks for your try. However, Since I do not pass the validation, the problem stems from something else.

Rob Winch said...

@Levy

You mentioned it did not start up. Did you get an error at startup? What is the full stacktrace? You mentioned JBoss...have you tried using Tomcat to run the samples? Are there other errors at startup?

@Cengiz

Running the JUnits was just to show that they should work it does not setup the classpath or source folders for eclipse. Running the eclipse task with gradle should setup your classpath properly for eclipse. Are you having any specific problems? You might try running gradle cleanEclipse eclipse and see if that helps.

justAMen said...

Hi, I'm getting the following error when running server in STS. Can you explain what did i do wrong?

SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4660)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
08.11.2011 15:23:30 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class org.springframework.security.web.session.HttpSessionEventPublisher
java.lang.ClassNotFoundException: org.springframework.security.web.session.HttpSessionEventPublisher
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:415)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:397)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4660)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5226)
at org.apache.catalina.core.StandardContext$1.call(StandardContext.java:5221)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

Rob Winch said...

@justAMen

Which project are you trying to run? Did you ensure that spring-web jar is on your classpath? If you navigate to /.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps//WEB-INF/lib do you see the spring-web jar? Have you ensured you have build automatically checked? Did you try cleaning the project?

Andy said...

The mistakes under had been detected when validating the document "spring-context-3.0.xsd" by way of the document "contacts-servlet.xml"
I have played the online games especially the wow for a long times and I have some good advices for everyone to have the bestTera Gold in more easy ways, I also have the best ways for you of the Cheapest WOW Gold, please don't hesitate !

Seungho Seo said...

I tried to clone this project. But I did not get any source from it. Isn't this project available any more?

Rob Winch said...

@Seungho,

The Spring Security source has moved to github.com I have updated the post to the new github.com url.

MALENADA HUDUGA said...

Rob, Nice Article!.I could successfully import all the projects. But, then I see a bunch of compilation errors. I see all the jar files in the build path of each project picked from .gradle/cache directory. In the window preferences, i see some default path for the Git settings.
Please let me know how can I make this work.
Thx, -Sudhi

Jeroen Rakhorst said...

Hi there, great post.
Very clear explanation on how to install and run on the tc server. I did run into a problem, somehow tc server can't find any of the linked libraries.
After some googling and trying stuff, i found out that somehow the deployment assembly isn't completely correct.
If you click right mouse button on (for example) spring-security-samples-tutorial, click properties and then choose Deployment Assembly, you can see that the jars aren't included. Next choose add, Java Build Path Entries and select all the jars (excluding servlet-api and the junit and test jars). After this little change in the project properties it works perfectly. I run on windows 7/64bit with jdk7 and sts-2.8.1.RELEASE.
Hopefully you can use this information.

Bob said...

@jeroen

This helped me.

Thanks

lili5058 said...

Thank you Jeroen for your post . . . I didn't know about the Deployment Assembly and spent two frustrating hours trying to track down the problem.

After reading the post, it now works!! Thanks.

kingrak said...

I got these error messages in console and 404 http error on tcServer page loaded in STS.
----
Apr 10, 2012 11:18:19 PM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:spring-security-samples-tutorial'

did not find a matching property.
Apr 10, 2012 11:18:19 PM com.springsource.insight.collection.tcserver.ltw.TomcatWeavingInsightClassLoader start
INFO: Context [localhost|tutorial] will not be woven
Apr 10, 2012 11:18:19 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1546)
..

Apr 10, 2012 11:18:19 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Error configuring application listener of class org.springframework.security.web.session.HttpSessionEventPublisher
java.lang.NoClassDefFoundError: org/springframework/context/ApplicationEvent
at java.lang.Class.getDeclaredConstructors0(Native Method)
...
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationEvent
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1701)
... 20 more
Apr 10, 2012 11:18:19 PM org.apache.catalina.core.StandardContext listenerStart
SEVERE: Skipped installing application listeners due to previous error(s)
Apr 10, 2012 11:18:19 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Error listenerStart
Apr 10, 2012 11:18:19 PM org.apache.catalina.core.StandardContext startInternal
SEVERE: Context [/tutorial] startup failed due to previous errors
Apr 10, 2012 11:18:19 PM org.apache.catalina.startup.HostConfig deployDirectory
...

Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Arnab said...

I got the same errors as kingrak in the last post.

I also noticed that the following projects were having an error for the test folder. It appears missing on the following projects: spring-security-config, spring-security-remoting, spring-security-web.

So even running the JUnits don't work.

Not sure what I have missed.

Any help would be appreciated.

My configuration:
Mac OSX Snow Leopard
Java - 1.6
STS - 2.9.2

Arnab said...
This comment has been removed by the author.
Arnab said...
This comment has been removed by the author.
Rob Winch said...

It is difficult to help you without knowing the error. You may move the discussion to the Spring Security forums too as I respond there and it is a bit easier to communicate there.

Rob Winch said...

Note for 3.1.1 see this thread http://forum.springsource.org/showthread.php?128391-Spring-security-sample-tutorial-throws-error-within-STS

MJ said...

@Arnab
and anyone else who is using 3.1.X and
used ./gradlew eclipse to build the project...
I was able to move past this by doing the following in sts 3.0.0:

For spring-security-config, spring-security-remoting, spring-security-web modules, right click on the module->configure-> convert to Gradle project. This removed all of the problems.

wow9gamer said...

I have to add the execution bit the gradle wrapper. Since this does not happen when I use git from the command line, I suspect it is an EGit bug. To change the permissions in a Linux environment executeLeague of Legends Account

Cheap LOL Coaching

Unknown said...

I just followed the steps from the "Running Spring Security's Tutorial Sample in Spring Tool Suite" page. I am lookinbg at my STS(Verxion 3.3.0) and see the Eclipse .project file but there is nothing about it being a web application in the .project file. When I click on any project in the /spring-security-3.2.x/samples folder or /spring-security-3.2.x/web, for example, there is no "run as->run on server" menu choice.