Spring Security access control example
In Spring Security, access control or authorization is easy to implement. See following code snippet :
<http auto-config="true"> <intercept-url pattern="/admin*" access="ROLE_ADMIN" /> </http>
It means, only user with authority of “ROLE_ADMIN” is allow to access URI /admin*. If non authorized user try to access it, a “http 403 access denied page” will be displayed.
See equivalent version in Spring EL. It is more flexible and contains many useful ready made functions like “hasIpAddress“, make sure check all available el functions in this official Spring el access control documentation.
<http auto-config="true" use-expressions="true"> <intercept-url pattern="/admin*" access="hasRole('ROLE_ADMIN')" /> </http>
In this tutorial, we show you how to use Spring Security to implement access control to url “/admin*“, where only user authorized with “ROLE_ADMIN” is allow to access this page.
1. Project Dependencies
Access control is included in core Spring Security jar. Refer to this Spring Security hello world example for list of the required dependencies.
2. Spring MVC
Spring MVC controller and return a “hello” view, it should be self-explanatory.
File : WelcomeController.java
package com.mkyong.common.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class WelcomeController { @RequestMapping(value = "/admin", method = RequestMethod.GET) public String welcomeAdmin(ModelMap model) { model.addAttribute("message", "Spring Security - ROLE_ADMIN"); return "hello"; } }
File : hello.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <html> <body> <h3>Message : ${message}</h3> <a href="<c:url value="j_spring_security_logout" />" > Logout</a> </body> </html>
3. Spring Security
Full Spring security configuration, only user “eclipse” is allow to access “/admin” page.
<beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.3.xsd"> <http auto-config="true"> <intercept-url pattern="/admin*" access="ROLE_ADMIN" /> <logout logout-success-url="/admin" /> </http> <authentication-manager> <authentication-provider> <user-service> <user name="mkyong" password="password" authorities="ROLE_USER" /> <user name="eclipse" password="password" authorities="ROLE_ADMIN" /> </user-service> </authentication-provider> </authentication-manager> </beans:beans>
4. Demo
URL : http://localhost:8080/SpringMVC/admin
1. Default login form is displayed.

2. If user “mkyong” is logged in, “http 403 is access denied page” will be displayed, because “mkyong” is “ROLE_USER“.

3. If user “eclipse” is logged in, “hello.jsp” will be displayed, because “eclipse” is “ROLE_ADMIN“.

Default 403 page is ugly, read this example – How to customize http 403 access denied page in spring security.






Hi
I have read through few Spring security articles and was able to create a web application based on it. However I am stuck now at one point where I am finding no solution.
The problem is how do we integrate Apache WS-XML RPC with Spring Security. Spring security works fine with normal HTTP. However when I have say a XmlRpcServlet which acts as a controller for handling all xmlrpc calls then how do we go
1. Intercept all calls by XmlRpcServlet then pass that call it to the Spring Security filter for authentication/authorization
2. Intercept all calls by Spring security filter authenticate/authorize and the pass it to the XmlRpcServlet for further processing
For me the 1st opinion looks more practical but nt getting an hand on to it. DO you have any idea ? I really nid ur help
[...] Spring Security Published: August 22, 2011 , Updated: August 22, 2011 , Author: mkyongprintIn last Spring security access control example, if non authorized user try to access a protected page, default “http 403 access [...]