3

を使用してWebアプリケーションを構築していますSpring 3 security

ログインページmyapp.com/loginは制限なくアクセスできます。

そこにログインすると、別のページに進むことができますmyapp.com/home

再度ロードmyapp.com/loginすると、このセッション中にユーザーがすでにログインしていることはわかりませんが、URL を変更すると、以前にログインしたユーザーとしてmyapp.com/homeアクセスできます。

ユーザーがログインしているかどうかを調べるためにさまざまな方法を試しましたが、成功しませんでした。

私が使用しているフロントエンド技術はJSP.

私はこれらを試しました:

    <sec:authorize ifAnyGranted="ROLE_ANONYMOUS">
        <td><a href="<c:url value="/login"/>">Login</a></td>
    </sec:authorize>
    <sec:authorize ifNotGranted="ROLE_ANONYMOUS">
        <!-- shall go to the homepage or better logout the user? -->
        <td><a href="<c:url value="/j_spring_security_logout"/>">Logout</a></td>
    </sec:authorize>

    <sec:authorize var="loggedIn" access="isAuthenticated()"/>

    <c:choose>
      <c:when test="${loggedIn}">
        <td><a href="<c:url value="/j_spring_security_logout"/>">Logout2</a></td>
      </c:when>
    </c:choose>

上記のコードは機能していないようです。これはなぜですか?

applicationContext-security.xml

<beans xmlns:security="http://www.springframework.org/schema/security"
       xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                 http://www.springframework.org/schema/security
                 http://www.springframework.org/schema/security/spring-security-3.1.xsd">

    <security:http pattern="/resources/**" security="none"/>
    <security:http pattern="/login" security="none" auto-config="true"/>
    <security:http pattern="/denied" security="none"/>

    <security:http auto-config="true" access-denied-page="/denied" servlet-api-provision="false">
        <security:intercept-url pattern="/login**" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <security:intercept-url pattern="/edit/**" access="ROLE_EDIT"/>
        <security:intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
        <security:intercept-url pattern="/**" access="ROLE_USER"/>
        <security:form-login login-page="/login"  authentication-failure-url="/denied"
                             default-target-url="/"/>
        <security:logout  logout-success-url="/login" />
    </security:http>

    <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="adam" password="adampassword" authorities="ROLE_USER"/>
                <security:user name="jane" password="janepassword" authorities="ROLE_USER, ROLE_ADMIN"/>
                <security:user name="sue" password="suepassword" authorities="ROLE_USER, ROLE_EDIT"/>
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager>

</beans>

ここで動作を確認できます。

http://147.32.204.138:5001/GENEPI/login

4

1 に答える 1

0

構成は次のようにする必要があると思います。

<http auto-config='true'>
    <intercept-url pattern="/home*" access="ROLE_USER,ROLE_ADMIN,ROLE_EDIT"/>
    <intercept-url pattern="/" access="ROLE_ANONYMOUS" />
    <form-login login-page='/login.jsp'/>
  </http>

/home ディレクトリに匿名でログインしようとすると、login.jsp にリダイレクトされるはずです。home/ には、href が j_spring_security_logout であるログアウト リンクがある場合があります。また、次のようなさまざまなロールの新しい傍受パターンを追加することもできます。

<intercept-url pattern="/edit*" access="ROLE_ADMIN,ROLE_EDIT"/>

次のようなコードのどこでも、ログインしているユーザーを確認できます。

SecurityContextHolder.getContext().getAuthentication().getPrincipal()
于 2013-05-05T03:02:41.807 に答える