簡體   English   中英

Spring安全登錄和注銷

[英]Spring security login and logout

我想使用spring security創建一個Web應用程序,但我覺得有些不對勁或缺失。

這是我的代碼:

security.xml文件:

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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.xsd">

<http use-expressions="true" disable-url-rewriting="true">
    <intercept-url pattern="/index.htm" access="hasRole('ROLE_ADMIN')" />
    <form-login login-processing-url="/login" login-page="/login.htm"
        username-parameter="userName" password-parameter="password"
        default-target-url="/index.htm" always-use-default-target="true"
        authentication-failure-url="/login.htm?auth=fail" />
    <logout logout-url="/logout.htm" logout-success-url="/login?out=1"
        delete-cookies="JSESSIONID" invalidate-session="true" />
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="drs" password="123456" authorities="ROLE_ADMIN" />
            <user name="dr" password="123456" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>
</beans:beans>

控制器:

@Controller
public class SecurityController {
    @RequestMapping(value = "/logout.htm", method = RequestMethod.GET)
    public String logoutPage() {
        return "logoutPage";
    }

    @RequestMapping(value = "/login.htm", method = RequestMethod.GET)
    public String login() {
        return "loginPage";
    }
}

login.jsp的:

<form:form action="${pageContext.request.contextPath}/login" method="POST">
    <c:if test="${not empty param.err}">
        <div>
            <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}" />
        </div>
    </c:if>
    <c:if test="${not empty param.out}">
        <div>You've logged out successfully.</div>
    </c:if>
    <c:if test="${not empty param.time}">
        <div>You've been logged out due to inactivity.</div>
    </c:if>

    Username:<br>
    <input type="text" name="userName" value="" />
    <br>
    <br> Password:<br>
    <input type="password" name="password" value="" />
    <input value="Login" name="submit" type="submit" />
</form:form>

logout.jsp:

<a href="${pageContext.request.contextPath}/login.htm">Login</a>

這項工作很好,唯一的問題是當我點擊登出時什么都沒做,我仍然擁有登錄后的權限。通常它應該回到登錄界面,要求用戶進行身份驗證以訪問該頁面。 我錯過了什么?

還有一個我無法想象的問題。 當我在登錄表單中更改時:

<form:form action="${pageContext.request.contextPath}/login" method="POST">

至:

<form name='loginform' action="<c:url value='j_spring_security_check' />" method='POST'>

我收到一個錯誤:

HTTP狀態404 - /Dronomy_2.1/j_spring_security_check

誰能幫我?

在啟用CSRF注銷方面,代碼中的兩個關鍵位置是:

logout-url="/logout.htm"

@RequestMapping(value = "/logout.htm", method = RequestMethod.GET)

我猜你不需要自己提供退出頁面。 要執行注銷,您需要用戶的瀏覽器對/logout.htm執行POST。 在此POST中,您需要包含csrf值。 此POST是使用logout-url =“/ logout.htm”配置的spring安全端點,而不是控制器中的注銷。

spring文檔提供了一些有關如何執行此操作的選項。 一種是在用戶可以注銷的所有頁面中包含一個表單,並在用戶單擊注銷菜單鏈接時使用Javascript提交表單。

如果這樣做,您可以刪除我上面列出的請求映射。

我剛剛更改了security.xml

<http use-expressions="true" disable-url-rewriting="true">
    <intercept-url pattern="/listUsers.htm" access="hasRole('ROLE_ADMIN')" />
    <intercept-url pattern="/play.htm" access="hasRole('ROLE_USER')" />
    <form-login login-page="/login.htm" login-processing-url="/j_spring_security_check"
        username-parameter="userName" password-parameter="password"
        default-target-url="/index.htm" always-use-default-target="true"
        authentication-failure-url="/login.htm?auth=fail" />
    <logout logout-url="/logout.htm" logout-success-url="/login.htm" />
</http>

添加

</c:if>
                <form method="post"
                    action="<c:url value='j_spring_security_check' />">

並從控制器中刪除重定向到注銷。 現在工作正常ty :)

這項工作很好,唯一的問題是當我點擊注銷什么也沒做

您只是將用戶導航到登錄頁面。 會話未失效。 SecurityContext對象未清除。

您需要使用spring security / j_spring_security_logout來記錄用戶

<a href="/j_spring_security_logout">LogOut</a>

查看此示例

為您的登錄表單嘗試

<form name='loginform' action="<c:url value='/j_spring_security_check' />" method='POST'>

通常由於上下文路徑計算而發生404錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM