[英]Spring Security Login form
我需要你的幫助。 我需要使用Spring Security的默認登錄表單。 當我啟動webapp時,它應該打開登錄頁面,並在成功登錄后將用戶重定向到我的jsp頁面,而無需尋找角色(管理員或只是用戶)。 我嘗試了這個:
<http auto-config="true" create-session="stateless"
use-expressions="true">
<intercept-url pattern="/_ah*" access="permitAll" />
<intercept-url pattern="/pages/*" access="hasRole('ROLE_ADMIN')" />
<intercept-url pattern="/index*" access="hasRole('ROLE_ADMIN')" />
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="working_sql_script"
authorities-by-username-query="other_working_sql_script" />
</authentication-provider>
</authentication-manager>
但是它在啟動時會打開空白頁面,我需要在瀏覽器中輸入登錄頁面: http://webapp/spring_security_login
。 如何配置它以在啟動時打開登錄頁面並在登錄后重定向到我的JSP? 另外,我在web.xml中寫道
<welcome-file-list>
<welcome-file>/WEB-INF/pages/index.jsp</welcome-file>
</welcome-file-list>
但啟動時仍會打開空白頁。
您需要在應用程序中定義一個頁面,而沒有角色限制。 並將該頁面設置為默認表單登錄。 驗證成功后,重定向到其他頁面。
我的spring-security.xml部分代碼如下
<http pattern="/login" security="none"/>
<http pattern="/accessDenied" security="none"/>
<http auto-config="true">
<remember-me/>
<!-- Don't set any role restrictions on login.jsf -->
<intercept-url pattern="/login" access="IS_AUTHENTICATED_ANONYMOUSLY" />
<intercept-url pattern="/pages/admin/*" access="ROLE_SADMIN" />
<intercept-url pattern="/pages/applicants/*" access="ROLE_SADMIN,ROLE_APPLICANT" />
<!-- Set the login page and what to do if login fails -->
<form-login login-page="/login" authentication-failure-url="/login"
default-target-url="/manageHome" authentication-success-handler-ref="customAuthenticationSuccessHandler"/>
<logout success-handler-ref="customLogoutSuccessHandler" />
<access-denied-handler ref="customAccessDeniedHandler"/>
</http>
感謝所有試圖幫助我的人。 我解決了這個問題。 並為其他會遇到此類問題的開發人員寫這篇文章。 我創建了歡迎頁面,所有用戶均可訪問,並且匿名。 頁面代碼:
<a href="index">Catalog</a>
<br>
<a href="registration">Registration</a>
我的spring-security文件包含以下內容:
<http auto-config="true" create-session="stateless"
use-expressions="true">
<intercept-url pattern="/welcome" access="isAnonymous()" />
<intercept-url pattern="/registration" access="isAnonymous()" />
<intercept-url pattern="/index" access="isAuthenticated()" />
<form-login default-target-url="/main"/>
</http>
和受保護的主頁:
<h2>Catalog</h2>
<a href="logout">Logout</a>
<a href="goods">Goods</a>
<a href="vendors">Vendors</a>
Spring Security僅在登錄后才允許訪問此頁面。 Spring Security生成的登錄頁面。 希望這會對某人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.