Category Archives: Spring MVC

When the Spring Security Principal isn’t enough in a Spring MVC Controller

I had previously made a blog entry to retrieve the spring security principal via @AuthenticationPrincipal in a controller method. Getting the Spring Security Principal in a Spring MVC Controller method. That is useful if you need the username or the … Continue reading

Posted in Java, Spring, Spring MVC, Spring Security | 3 Comments

Mocking Spring Security Principal for testing Spring MVC Controllers

How do you mock a Spring security principal for testing spring MVC controllers? Create a spring security principal Create a spring authentication Add the spring security authentication to the security context The last step should not be needed because MockMVC … Continue reading

Posted in Java, MockMVC, Spring, Spring MVC, Spring Security | Tagged , , , , , , , , , , , , , , | 2 Comments

Testing Spring MVC Rest controllers

Spring MVC 3.2 introduced the ability to test controllers via MockMVC. In this example, we will test a controller that returns JSON via @ResponseBody. Here are the dependencies that you will need. This is an example of the spring context. … Continue reading

Posted in Java, MockMVC, Spring, Spring MVC | Tagged , , , , , , , | Leave a comment

Getting the Spring Security Principal in a Spring MVC Controller method

You will need to create a Spring configuration class and annotate it with @EnableWebMvcSecurity The documentation then says to add a method parameter to a Spring MVC controller method with @AuthenticationPrincipal and you are done, like the following: What they … Continue reading

Posted in Spring, Spring MVC, Spring Security | Tagged , , , , , | 16 Comments