Попробуйте это:
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
//not authorized
}
Object principalObject = authentication.getPrincipal();
if (principalObject == null) {
//not authorized
}
Или вы можете настроить безопасность, я думаю, что это то, что вам нужно в 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>
Я использую Spring 3.1, так что мои пространств имен:
<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
">
Удача
Спасибо за ваш ответ. Но моя проблема в том, что если пользователь уже зарегистрирован, страница входа в систему не должна отображаться. Вместо главной страницы входа в систему (после страницы входа). Так что, пожалуйста, помогите мне решить эту проблему, используя фильтр предварительной проверки подлинности, если это возможно. Спасибо. –