Try this:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
//not authorized
}
Object principalObject = authentication.getPrincipal();
if (principalObject == null) {
//not authorized
}
Or you can configure the security, I think this is what you need in your applicationContext:
<security:http auto-config="true" authentication-manager-ref="authenticationManager">
<!-- Don't set any role restrictions on login.jsp and index.jsp -->
<security:intercept-url pattern="/login"
access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<security:intercept-url pattern="/urlOfAView"
access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<!-- Restrict access to ALL other pages -->
<security:intercept-url pattern="/**" access="ROLE_USER"/>
<!-- Set the login page and what to do if login fails -->
<security:form-login login-page="/login"
authentication-failure-url="/login?login_error=1" default-target-url="/loginUser"/>
<!-- Set the logout page and where to go after logout is successful -->
<security:logout logout-success-url="/index"/>
</security:http>
I use Spring 3.1 so my namespaces are:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
">
Good luck