Try the following:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { //not authorized } Object principalObject = authentication.getPrincipal(); if (principalObject == null) { //not authorized }
Or you can configure security, I think this is what you need in your context application:
<security:http auto-config="true" authentication-manager-ref="authenticationManager"> <security:intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <security:intercept-url pattern="/urlOfAView" access="IS_AUTHENTICATED_ANONYMOUSLY"/> <security:intercept-url pattern="/**" access="ROLE_USER"/> <security:form-login login-page="/login" authentication-failure-url="/login?login_error=1" default-target-url="/loginUser"/> <security:logout logout-success-url="/index"/> </security:http>
I am using 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.
source share