Spring Security

Spring Security XML Namespace Configuration Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>In this <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><b>Spring Security Tutorial<&sol;b><&sol;a> Series we will take a look how to use Spring Security with XML Namespace Configuration with Example of authentication and access-control using &lt&semi;http&gt&semi;&comma; &lt&semi;intercept-url&gt&semi; namespaces&period; In my earlier article We have seen <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-java-based-configuration-with-example&sol;">Spring Security Java Configuration Annotation Example<&sol;a> of Authentication and access control using &commat;Secured&comma; &commat;PreAuthorize and JSR-250 &commat;RolesAllowed annotations&period; As we know that Spring Security is a very powerful and highly customizable authentication and access-control framework&period;<br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<h2><b>Spring Security XML Namespace Configuration<&sol;b><&sol;h2>&NewLine;<p>XML Namespace configuration has been available since Spring Security 2&period;0&period; In Spring Framework&comma; A namespace element is nothing but it is a more concise way of configuring an individual bean or&comma; more powerfully&comma; to define an alternative configuration syntax&period; It actually hides the underlying bean definition complexity from the user&period; For using the spring security namespace in your application context&comma; you need to have the spring-security-config jar on your classpath&period; And finally you need to add the schema declaration to your application context file as below&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;beans xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" &NewLine;xmlns&colon;> &NewLine;&nbsp&semi; &NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"> &NewLine; &NewLine;<span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span> &NewLine;<ul style&equals;"text-align&colon; left&semi;"> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li> &NewLine; &Tab;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li> &NewLine;<&sol;ul> &NewLine;<&sol;div> &NewLine;<h2><b>web&period;xml Configuration <&sol;b><b>&lpar;Setup DelegatingFilterProxy and springSecurityFilterChain&rpar;<&sol;b><&sol;h2> &NewLine;FilterChainProxy add a single entry to web&period;xml and deal entirely with the application context file for managing our web security beans&period; It is wired using a DelegatingFilterProxy&comma; just like in the example above&comma; but with the filter-name set to the bean name "springSecurityFilterChain"&period; In this case&comma; the bean is named "springSecurityFilterChain"&comma; which is an internal infrastructure bean created by the namespace to handle web security&period; &NewLine;<h3>Define the single proxy filter<&sol;h3> &NewLine;– <b>springSecurityFilterChain<&sol;b> is a mandatory name &NewLine;-Refers to an existing Spring bean with same name &NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;web-app xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" xmlns&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee" xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee http&colon;&sol;&sol;java&period;sun&period;com&sol;xml&sol;ns&sol;javaee&sol;web-app&lowbar;3&lowbar;0&period;xsd" id&equals;"WebApp&lowbar;ID" version&equals;"3&period;0"&gt&semi; &NewLine; &lt&semi;display-name&gt&semi;17-Spring-Security-XML-Namespace-Config&lt&semi;&sol;display-name&gt&semi; &NewLine; &lt&semi;servlet&gt&semi; &NewLine; &lt&semi;servlet-name&gt&semi;doj&lt&semi;&sol;servlet-name&gt&semi; &NewLine; &lt&semi;servlet-class&gt&semi; &NewLine; org&period;springframework&period;web&period;servlet&period;DispatcherServlet &NewLine; &lt&semi;&sol;servlet-class&gt&semi; &NewLine; &lt&semi;load-on-startup&gt&semi;1&lt&semi;&sol;load-on-startup&gt&semi; &NewLine; &lt&semi;&sol;servlet&gt&semi; &NewLine; &lt&semi;servlet-mapping&gt&semi; &NewLine; &lt&semi;servlet-name&gt&semi;doj&lt&semi;&sol;servlet-name&gt&semi; &NewLine; &lt&semi;url-pattern&gt&semi;&sol;&lt&semi;&sol;url-pattern&gt&semi; &NewLine; &lt&semi;&sol;servlet-mapping&gt&semi; &NewLine; &NewLine; &lt&semi;filter&gt&semi; &NewLine; &lt&semi;filter-name&gt&semi;springSecurityFilterChain&lt&semi;&sol;filter-name&gt&semi; &NewLine; &lt&semi;filter-class&gt&semi;org&period;springframework&period;web&period;filter&period;DelegatingFilterProxy&lt&semi;&sol;filter-class&gt&semi; &NewLine; &lt&semi;&sol;filter&gt&semi; &NewLine; &NewLine; &lt&semi;filter-mapping&gt&semi; &NewLine; &lt&semi;filter-name&gt&semi;springSecurityFilterChain&lt&semi;&sol;filter-name&gt&semi; &NewLine; &lt&semi;url-pattern&gt&semi;&sol;&ast;&lt&semi;&sol;url-pattern&gt&semi; &NewLine; &lt&semi;&sol;filter-mapping&gt&semi; &NewLine;&lt&semi;&sol;web-app&gt&semi; &NewLine;<&sol;pre>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img title&equals;"Spring Security Filter Configuration" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;02&sol;Spring-Security-Filter-Configuration&period;jpg" width&equals;"640" height&equals;"371" border&equals;"0" &sol;><&sol;div>&NewLine;<h2><b><br &sol;>&NewLine;<&sol;b> <b>Spring Security Namespace Configuration in the Application Context file<&sol;b><&sol;h2>&NewLine;<p>Once you’ve added <em><strong>DelegatingFilterProxy<&sol;strong> <&sol;em>and <em><strong>springSecurityFilterChain<&sol;strong> <&sol;em>to your web&period;xml&comma; you’re ready to start editing your application context file&period; Web security services are configured using the &lt&semi;http&gt&semi; element&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;beans xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" &NewLine; xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" &NewLine; xmlns&colon;context&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context" &NewLine; xmlns&colon;mvc&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc" &NewLine; <b>xmlns&colon;> &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;mvc&sol;spring-mvc&period;xsd &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&sol;spring-beans&period;xsd &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context&sol;spring-context&period;xsd"&gt&semi; &NewLine; &NewLine; &lt&semi;context&colon;component-scan base-package&equals;"com&period;doj&period;app&period;controller"&sol;&gt&semi; &NewLine; &NewLine; &lt&semi;bean class&equals;"org&period;springframework&period;web&period;servlet&period;view&period;InternalResourceViewResolver"&gt&semi; &NewLine; &lt&semi;property name&equals;"suffix" value&equals;"&period;jsp"&sol;&gt&semi; &NewLine; &lt&semi;property name&equals;"prefix" value&equals;"&sol;WEB-INF&sol;view&sol;"&sol;&gt&semi; &NewLine; &lt&semi;&sol;bean&gt&semi; &NewLine;<b> &NewLine; &lt&semi;security&colon;http&gt&semi; &NewLine; &lt&semi;security&colon;intercept-url access&equals;"hasRole&lpar;'USER'&rpar;" pattern&equals;"&sol;&ast;&ast;" &sol;&gt&semi; &NewLine; &lt&semi;security&colon;form-login&sol;&gt&semi; &NewLine; &lt&semi;security&colon;logout&sol;&gt&semi; &NewLine; &lt&semi;&sol;security&colon;http&gt&semi; &NewLine; &NewLine; &NewLine; &lt&semi;security&colon;authentication-manager&gt&semi; &NewLine; &lt&semi;security&colon;authentication-provider&gt&semi; &NewLine; &lt&semi;security&colon;user-service&gt&semi; &NewLine; &lt&semi;security&colon;user name&equals;"admin" password&equals;"adminpassword" authorities&equals;"ROLE&lowbar;USER&comma; ROLE&lowbar;ADMIN" &sol;&gt&semi; &NewLine; &lt&semi;security&colon;user name&equals;"user" password&equals;"password" authorities&equals;"ROLE&lowbar;USER" &sol;&gt&semi; &NewLine; &lt&semi;&sol;security&colon;user-service&gt&semi; &NewLine; &lt&semi;&sol;security&colon;authentication-provider&gt&semi; &NewLine; &lt&semi;&sol;security&colon;authentication-manager&gt&semi; &NewLine;<&sol;b> &NewLine;&lt&semi;&sol;beans&gt&semi; &NewLine; &NewLine;<&sol;pre>&NewLine;<p>As according to the above application context file&comma; we want all URLs within our application to be secured&comma; requiring the role ROLE&lowbar;USER to access them&comma; we want to log in to the application using a form with username and password&comma; and that we want a logout URL registered which will allow us to log out of the application&period; The configuration above defines two users&comma; their passwords and their roles within the application&period;<&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>&lt&semi;security&colon;http&gt&semi; <&sol;b>&colon; This element is the parent for all web-related namespace functionality&period; It is responsible for creating a FilterChainProxy and the filter beans which it uses&period; Common problems like incorrect filter ordering are no longer an issue as the filter positions are predefined&period;<&sol;li>&NewLine;<li><b>&lt&semi;security&colon;intercept-url&gt&semi;<&sol;b> &colon; This element defines a pattern which is matched against the URLs of incoming requests using an ant path style syntax&period;&NewLine;<ul>&NewLine;<li><b>Note&colon;<&sol;b> You can use multiple <b>&lt&semi;security&colon;intercept-url&gt&semi;<&sol;b> elements to define different access requirements for different sets of URLs&comma; but they will be evaluated in the order listed and the first match will be used&period; So you must put the most specific matches at the top&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;li>&NewLine;<li><b>&lt&semi;security&colon;authentication-manager&gt&semi;<&sol;b> &colon; This element creates a <b><i>ProviderManager<&sol;i><&sol;b> and registers the authentication providers with it&period;<&sol;li>&NewLine;<li><b>&lt&semi;security&colon;authentication-provider&gt&semi;<&sol;b> &colon; This element creates a <b><i>DaoAuthenticationProvider<&sol;i><&sol;b> bean&period;<&sol;li>&NewLine;<li><b>&lt&semi;security&colon;user-service&gt&semi;<&sol;b> &colon; This element creates an <b><i>InMemoryDaoImpl<&sol;i><&sol;b>&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2><b>Adding Custom Login Form<&sol;b><&sol;h2>&NewLine;<p>Thus far Spring Security generates login form automatically&comma; based on the features that are enabled and using standard values for the URL which processes the submitted login&comma; the default target URL the user will be sent to after logging in and so on&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;security&colon;http pattern&equals;"&sol;login" > &NewLine;From Spring Security 3&period;1 it is now possible to use multiple http elements to define separate security filter chain configurations for different request patterns&period; It is also possible to have all requests matching a particular pattern bypass the security filter chain completely&comma; by defining a separate http element for the pattern like this &sol;login&comma; "&sol;resources&sol;"&comma; "&sol;scripts&sol;"&comma; "&sol;css&sol;" etc&period; &NewLine;<h2><b>Handling Logouts<&sol;b><&sol;h2> &NewLine;When using the<b> &lt&semi;security&colon;logout&sol;&gt&semi;<&sol;b>&comma; logout capabilities are automatically applied&period; &NewLine;The default is that accessing the URL &sol;logout will log the user out by&colon; &NewLine;<ul style&equals;"text-align&colon; left&semi;"> &NewLine; &Tab;<li>Invalidating the HTTP Session<&sol;li> &NewLine; &Tab;<li>Cleaning up any RememberMe authentication that was configured<&sol;li> &NewLine; &Tab;<li>Clearing the SecurityContextHolder<&sol;li> &NewLine; &Tab;<li>Redirect to &sol;login&quest;logout<&sol;li> &NewLine;<&sol;ul> &NewLine;&nbsp&semi; &NewLine;<pre class&equals;"highlight">&lt&semi;security&colon;logout &NewLine; invalidate-session&equals;"true" &NewLine; delete-cookies&equals;"AppCookies" &NewLine; logout-url&equals;"&sol;logout" &NewLine; logout-success-url&equals;"&sol;home"&sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<h2><b>Configure Authentication Providers<&sol;b><&sol;h2>&NewLine;<p>There are three built in options&colon;<&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li>LDAP&comma;<&sol;li>&NewLine;<li>JDBC&comma;<&sol;li>&NewLine;<li>in-memory &period;<&sol;li>&NewLine;<&sol;ol>&NewLine;<h2><b>In-Memory Authentication Namespace Configuration<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">&lt&semi;security&colon;authentication-manager&gt&semi; &NewLine; &lt&semi;security&colon;authentication-provider&gt&semi; &NewLine; &lt&semi;security&colon;user-service&gt&semi; &NewLine; &lt&semi;security&colon;user name&equals;"admin" password&equals;"adminpassword" authorities&equals;"ROLE&lowbar;USER&comma; ROLE&lowbar;ADMIN" &sol;&gt&semi; &NewLine; &lt&semi;security&colon;user name&equals;"user" password&equals;"password" authorities&equals;"ROLE&lowbar;USER" &sol;&gt&semi; &NewLine; &lt&semi;&sol;security&colon;user-service&gt&semi; &NewLine; &lt&semi;&sol;security&colon;authentication-provider&gt&semi; &NewLine;&lt&semi;&sol;security&colon;authentication-manager&gt&semi; &NewLine;<&sol;pre>&NewLine;<h2><b>JDBC Authentication Namespace Configuration<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">&lt&semi;authentication-manager&gt&semi; &NewLine;&lt&semi;authentication-provider&gt&semi; &NewLine; &lt&semi;jdbc-user-service data-source-ref&equals;"dataSource"&sol;&gt&semi; &NewLine;&lt&semi;&sol;authentication-provider&gt&semi; &NewLine;&lt&semi;&sol;authentication-manager&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>Where "<b>dataSource<&sol;b>" is the name of a DataSource bean in the application context&comma; pointing at a database containing the standard Spring Security user data tables&period;<br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<h2><b>Password Encoder<&sol;b><&sol;h2>&NewLine;<p>You can also customize how passwords are encoded by exposing a PasswordEncoder as a bean&period; We can encode passwords using a hash – sha&comma; md5&comma; bcrypt etc&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;security&colon;http pattern&equals;"&sol;login" >&lt&semi;bean name&equals;"bcryptEncoder" class&equals;"org&period;springframework&period;security&period;crypto&period;bcrypt&period;BCryptPasswordEncoder"&sol;&gt&semi;<&sol;b> &NewLine; &lt&semi;security&colon;authentication-manager&gt&semi; &NewLine; &lt&semi;security&colon;authentication-provider&gt&semi; &NewLine; <b>&lt&semi;security&colon;password-encoder ref&equals;"bcryptEncoder"&sol;&gt&semi; &NewLine; &lt&semi;security&colon;user-service&gt&semi; &NewLine; &lt&semi;security&colon;user name&equals;"admin" password&equals;"4e7421b1b8765d8f9406d87e7cc6aa784c4ab97f" authorities&equals;"ROLE&lowbar;USER&comma; ROLE&lowbar;ADMIN" &sol;&gt&semi; &NewLine; &lt&semi;security&colon;user name&equals;"user" password&equals;"d7e6351eaa13189a5a3641bab846c8e8c69ba39f" authorities&equals;"ROLE&lowbar;USER" &sol;&gt&semi; &NewLine; &lt&semi;&sol;security&colon;user-service&gt&semi;<&sol;b> &NewLine; &lt&semi;&sol;security&colon;authentication-provider&gt&semi; &NewLine; &lt&semi;&sol;security&colon;authentication-manager&gt&semi; &NewLine;<&sol;pre>&NewLine;<h2><b>Method Security<&sol;b><&sol;h2>&NewLine;<p>From Spring version 2&period;0 Spring Security provides support to adding security to your service layer methods&period; Spring Security uses AOP for security at the method level&period;<br &sol;>&NewLine;– annotations based on Spring annotations or JSR-250 annotations<br &sol;>&NewLine;– Java configuration to activate detection of annotations<br &sol;>&NewLine;- From 3&period;0 you can also make use of new expression-based annotations&period;<br &sol;>&NewLine;Typically secure your services<br &sol;>&NewLine;– Do not access repositories directly&comma; bypasses security &lpar;and transactions&rpar;<&sol;p>&NewLine;<h2><b>EnableGlobalMethodSecurity<&sol;b><&sol;h2>&NewLine;<p><b>&lt&semi;global-method-security&gt&semi;- <&sol;b>This element is used to enable annotation-based security in your application&period; The following declaration would enable support for Spring Security’s &commat;Secured&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;global-method-security secured-annotations&equals;"enabled" &sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">import org&period;springframework&period;security&period;annotation&period;Secured&semi; &NewLine; &NewLine;public class ItemManagerService &lbrace; &NewLine;&commat;Secured&lpar;"IS&lowbar;AUTHENTICATED&lowbar;FULLY"&rpar; &NewLine;public Item findItem&lpar;long itemNumber&rpar; &lbrace; &NewLine;&period;&period;&period; &NewLine;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>We can also use<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>&commat;Secured&lpar;"ROLE&lowbar;MEMBER"&rpar;<&sol;li>&NewLine;<li>&commat;Secured&lpar;&lbrace;"ROLE&lowbar;MEMBER"&comma; "ROLE&lowbar;USER"&rcub;&rpar;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p><b>Note&colon;<&sol;b> Spring 2&period;0 syntax&comma; so not limited to roles&period; SpEL not supported&period;<br &sol;>&NewLine;<b><&sol;b><&sol;p>&NewLine;<h2><b>Enable Method Security - JSR-250<&sol;b><&sol;h2>&NewLine;<p>Support for JSR-250 annotations can be enabled using&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;global-method-security jsr250-annotations&equals;"enabled" &sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">import javax&period;annotation&period;security&period;RolesAllowed&semi; &NewLine; &NewLine;public class ItemManagerService &lbrace; &NewLine;&commat;RolesAllowed&lpar;&lbrace;"ROLE&lowbar;MEMBER"&comma; "ROLE&lowbar;USER"&rcub;&rpar; &NewLine;public Item findItem&lpar;long itemNumber&rpar; &lbrace; &NewLine;&period;&period;&period; &NewLine;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Note&colon;<&sol;b> Only supports role-based security – hence the name<&sol;p>&NewLine;<h2><b>Method Security with SpEL<&sol;b><&sol;h2>&NewLine;<p>To use the new expression-based syntax&comma; you would use<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;global-method-security pre-post-annotations&equals;"enabled" &sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">public interface ItemManagerService &lbrace; &NewLine; &NewLine;&commat;PreAuthorize&lpar;"isAnonymous&lpar;&rpar;"&rpar; &NewLine;public Item findItem&lpar;Long itemNumber&rpar;&semi; &NewLine; &NewLine;&commat;PreAuthorize&lpar;"isAnonymous&lpar;&rpar;"&rpar; &NewLine;public Item&lbrack;&rsqb; findItems&lpar;&rpar;&semi; &NewLine; &NewLine;&commat;PreAuthorize&lpar;"hasAuthority&lpar;'ROLE&lowbar;USER'&rpar;"&rpar; &NewLine;public Item post&lpar;Item item&rpar;&semi; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>Adding Security Pointcuts using protect-pointcut<&sol;b><&sol;h2>&NewLine;<p>The use of protect-pointcut is particularly powerful&comma; as it allows you to apply security to many beans with only a simple declaration&period; Consider the following example&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;global-method-security&gt&semi; &NewLine;&lt&semi;protect-pointcut expression&equals;"execution&lpar;&ast; com&period;doj&period;app&period;&ast;Service&period;&ast;&lpar;&period;&period;&rpar;&rpar;" &NewLine; access&equals;"ROLE&lowbar;USER"&sol;&gt&semi; &NewLine;&lt&semi;&sol;global-method-security&gt&semi; &NewLine;<&sol;pre>&NewLine;<p>This will protect all methods on beans declared in the application context whose classes are in the <b>com&period;doj&period;app<&sol;b> package and whose class names end in "<b>Service<&sol;b>"&period; Only users with the ROLE&lowbar;USER role will be able to invoke these methods&period;<&sol;p>&NewLine;<h3><a href&equals;"https&colon;&sol;&sol;github&period;com&sol;DOJ-SoftwareConsultant&sol;Spring-Security-XML-Namespace-Config" target&equals;"&lowbar;blank" rel&equals;"noopener"><b>Download Java Based Configuration Example from Git<&sol;b><&sol;a><&sol;h3>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img title&equals;"Spring Security XML Namespace Configuration Example" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;02&sol;Spring-Security-xml-namespace-configuration-example&period;png" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">&NewLine;<p><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Spring Security Related Posts<&sol;b><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Interview Questions and Answers<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-java-based-configuration-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Java Based Configuration with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-xml-namespace-configuration-example&sol;"><span style&equals;"color&colon; red&semi;">Spring Security XML Namespace Configuration Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-hello-world-example&sol;"><span style&equals;"color&colon; red&semi;">Spring Security XML Based Hello World Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-form-based-login-example&sol;"><span style&equals;"color&colon; red&semi;">Spring Security form-based login example <&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-login-form-using&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Login Form Based Example Using Database<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-http-basic&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Authentication Example Using HTTP Basic <&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-authorized-access&sol;"><span style&equals;"color&colon; red&semi;"> Spring Security Authorized Access Control Example <&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;customize-http-403-access-denied-page&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Customized Access Denied Page<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-custom-error-message&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Custom Error Message<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-logout-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring Security Logout Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-fetch-logged-in-username&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Fetch Logged in Username<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-password-hashing&sol;"><span style&equals;"color&colon; red&semi;">Spring Security Password Hashing<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-java-based-configuration-with-example&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-annotation-based-hello-world-example&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '59'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago