Securing and customizing pages with Spring

Now that we are up and running with a basic authentication setup, let’s take it a step further. We would like to add our own login page now, instead of using the one that Spring generated for us. Then we will see how to restrict parts of the application for certain users. When we are done with that, we will put the icing on the cake by displaying the username next to each greeting on the screen.

1. Add custom logic to the security-context.xml file

<?xml version="1.0" encoding="UTF-8"?>
        <security:global-method-security secured-annotations="enabled" /> 
    <!--if you wish to use @PreAuthorize instead... -->
    <!--<security:global-method-security pre-post-annotations="enabled"/> -->
	<security:http auto-config="true" access-denied-page="/WEB-INF/jsp/accessdenied.jsp"> 
	   <!-- note that the order matters!! -->
           <security:intercept-url pattern="/login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY" />
           <security:intercept-url pattern="/WEB-INF/jsp/restrictedarea.jsp" access="ROLE_ADMIN" />
           <security:intercept-url pattern="/**" access="ROLE_USER" />
           <security:form-login login-page="/login.jsp" 
           <security:logout logout-url="/logout" logout-success-url="/login.jsp" />
	<bean class=""/>
				<security:jdbc-user-service data-source-ref="dataSource"/>

2. Create a custom login page called login.jsp

The login page we have seen was automatically generated by Spring. Let’s now create our own so we can customize it and add styling if we want later.

<%@ taglib prefix="c" uri="" %>
<%@ taglib prefix="fmt" uri="" %>
<%@ taglib prefix="form" uri="" %>
<%@ taglib prefix="sec" uri="" %>
<%@ page import="" %>
<%@ page import="" %>
<%@ page import="" %>
	<form name="f" action="<c:url value="/loginProcess" />" method="post">
				<td><input type="text" 
						 id="j_username" <c:if test="${not empty param.login_error}">value="<%= session.getAttribute(AuthenticationProcessingFilter.SPRING_SECURITY_LAST_USERNAME_KEY) %>"</c:if> /></td>
				<td><input type="password" name="j_password" id="j_password" /></td>
			<tr><td colspan="2" align="center"><input name="submit" id="submit" type="submit" value="Login" /></td></tr>
	<c:if test="${not empty param.login_error}">
		<font color="red">
			Login failure!<br />
			Reason: <%= ((AuthenticationException) session.getAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY)).getMessage() %>
<sec:authorize access="isAuthenticated()">
   You are currently logged in!

3. Create a page called accessdenied.jsp

Sorry, you do not have permission to access this page.<br/>
Click <a href="/springgreetings/logout">here</a> to login as an administrator.

4. Create a page called restrictedarea.jsp

It appears you were able to enter the restricted area.<br/>
That means you are an administrator. Lucky you :-) 

5. Update index.jsp to link to the new pages

We have created two new pages, restrictedarea.jsp and accessdenied.jsp and we just need to link to the pages now. We will use spring security tags to secure the restricted area so that only users with a role “admin” can go there. Any other users with inferior roles will see an “access denied” page when they attempt to enter the restricted area.

<%@ taglib prefix="sec" uri="" %>
<h2>Welcome to Spring Greetings!</h2>
<p><a href="/springgreetings/home/addgreeting.html">Add greeting</a><br/>
<a href="/springgreetings/home/greetings.html">Show all greetings</a><br/>
<sec:authorize access="hasRole('ROLE_ADMIN')">
<a href="/springgreetings/restrictedarea.jsp">Restricted area...</a><br/>
<a href="/springgreetings/logout">Logout</a>

Now redeploy the application to tomcat and see our new custom login page when you enter this url


Now add the username to the greetings…

Now instead of presenting each greeting with “@Anonymous says”, let’s write the username of the user that’s logged in.

First we will modify the class to add the username field

	@Column(name = "USERNAME")
	private String username;
	public String getUsername() {
		return username;
	public void setUsername(String username) {
		this.username = username;

Then we just need to modify this method in to fetch the user details (adding the imports AND

	@RequestMapping(value = "/greetings.html", method = RequestMethod.POST)
	public String addGreetingAndShowAll(@ModelAttribute("greetingform") GreetingForm greetingForm,
			Map<String, Object> model) {"entering addGreetingAndShowAll()");
		Greeting greeting = greetingForm.getGreeting();
		greeting.setGreetingDate(new Date());
		UserDetails userDetails = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		List<Greeting> greetings = greetingService.getAllGreetings();
		model.put("greetinglist", greetings);
	    	String selectedColorCode=greetingForm.getColor().getColorCode(); 
	    	if(selectedColorCode.equals("")) //if no color selected, then make default white
	    	model.put("colorcode", selectedColorCode);
	    	// This will resolve to /WEB-INF/jsp/greetings.jsp
	    	return "greetings";

And in greetings.jsp we replace this line of code

<br/><b>@Anonymous</b> says<br/>

with this one

<br/><b>@<c:out value="${greeting.username}" /></b> says<br/>

Before redeploying the application to tomcat, we need to drop the table from the Database Manager to avoid errors with the missing column. So we login to the database manager and execute “DROP TABLE GREETINGS;” and then the table will be newly generated when we redeploy and restart the application.

Securing methods in java classes using annotations…

Inside it is possible to secure methods as well. Here is how:

Since we defined in security-context.xml that annotations are enabled

 <security:global-method-security secured-annotations="enabled" />

then we can secure methods in java classes as follows:

	@RequestMapping(value = "/greetings.html", method = RequestMethod.POST)
	public String addGreetingAndShowAll(@ModelAttribute("greetingform") GreetingForm greetingForm,
			Map<String, Object> model) {		

We just need to add a default constructor to for some reason,

public GreetingController() {}

otherwise we get this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘greetingController’ defined in file [/usr/local/tomcat/webapps/springgreetings/WEB-INF/classes/com/bitbybit/web/controller/GreetingController.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.bitbybit.web.controller.GreetingController]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given

Here is the source code.


Post a Comment