Spring Security Hiccups
In any web-application some pages are secured (only logged in and authorized users with proper role can access) and some pages are not secured such as login or landing page. But sometimes we need a mix-n-match, such as some information user can view without proper access(login) but they need rights(such as login) to perform additional activities. here is an example - in amazon or any retail site you can view products, but you need to login to buy the product - the buy now button will ask for your credentials.
Images, css, Javascript dont require security
Images, css, Javascript dont require security
<sec:http pattern="/css/**" security="none"/>
<sec:http pattern="/gwt/**" security="none"/>
<sec:http pattern="/images/**" security="none"/>
<sec:http pattern="/img/**" security="none"/>
<sec:http pattern="/scripts/**" security="none"/>
Logon page needs anonymous access -
<sec:http auto-config="true" use-expressions="true">
<sec:intercept-url pattern="/logon.do" access="hasAnyRole('ROLE_ANONYMOUS')"/>
</sec:http>
Logout or home page need a user
<sec:intercept-url pattern="/logout.do" access="hasRole('ROLE_USER')"/>
<sec:intercept-url pattern="/home.do" access="hasRole('ROLE_USER')"/>
For those pages that need both logged-in and no logon
<sec:intercept-url pattern="/yourSecuredAndUnsecuredPage.do" access="permitAll"/>
The beauty of permitAll is that, the request goes through the secuirty filter, hence for logged-in user the SecuirtyContext holds an authnetication, whereas if you leave the page <sec:http pattern="/ somePage.do"security="none"/> then no filter is used and hence for the logged-in user it will say authentication is null, hence <sec:authorize access="authenticated" var="xxxxx" /> in jsp will always set xxxx=false. the logged-in user logic will not work.
So, for mix pages use the permitAll access.
Comments
Post a Comment