これを試してみてください:
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>
私は春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
">
幸運
あなたの答えをありがとう。 しかし、私の問題は、ユーザーが既にログインしている場合、ログインページを表示しないことです。ログインページのメインページ(ログイン後に来るページ)が表示されるはずです。 可能であれば、事前認証フィルタを使用してこの問題を解決してください。 ありがとうございます。 –