2011-09-09 143 views
20

我開發了使用Spring Security的默認登錄頁面的應用程序。不過,我想實現我自己的登錄頁面。我會把一個login.html而不是一個jsp頁面。我想爲它使用JQuery。我檢查了很多例子,但無法實現。我是新來春春的安全,我使用Spring Security 3. 任何想法,哪些步驟我應該遵循?Spring Security的登錄頁

+0

嗨,我試圖與'純HTML +的jQuery +春Security'登錄頁面了。你有沒有想過如何包含Spring Security對CSRF的內置支持。 – smwikipedia

回答

44

有在Spring Security的自定義登錄頁面了四項要求:

  1. 有一個名爲j_username輸入字段將包含用於認證證書的名稱。
  2. 有一個名爲j_password輸入字段將包含用於認證證書的密碼。
  3. 這些值爲POST ed的url匹配Spring安全配置中form-login元素的login-processing-url屬性中定義的url。
  4. 自定義登錄表單的位置必須在form-login元素在你的春季安全配置login-page屬性來指定。

的login.html

<body> 
     <form action="/j_spring_security_check" method="POST"> 
     <label for="username">User Name:</label> 
     <input id="username" name="j_username" type="text"/> 
     <label for="password">Password:</label> 
     <input id="password" name="j_password" type="password"/> 
     <input type="submit" value="Log In"/> 
     </form> 
    </body> 

春季安全配置文件

<http use-expressions="true"> 
     <intercept-url pattern="/login*" access="isAnonymous()"/> 
     <intercept-url pattern="/**" access="isFullyAuthenticated()"/> 
     <form-login 
     login-page="/login.html" 
     login-processing-url="/j_spring_security_check.action" 
     authentication-failure-url="/login_error.html" 
     default-target-url="/home.html" 
     always-use-default-target="true"/> 
    </http> 
+0

我應該寫一LoginLogoutController或類似的東西,我怎麼會設置是否成功的一個用戶登錄重定向到哪裏? – kamaci

+0

感謝您的回答。我用螢火蟲調試它,它說:http:// localhost:8080/j_spring_security_check 302臨時移動以解決該重定向問題。 – kamaci

+1

如果你已經在'web.xml'中正確註冊了'springSecurityFilterChain',並且在其他任何過濾器之前註冊了'springSecurityFilterChain',它應該接受對'login-processing-url'指定的URL的任何請求。 –

6

我已經工作了幾天對我的項目和配置實施春季安全終於做到了爲以下幾點:

彈簧的security.xml

<security:http auto-config="true" disable-url-rewriting="true" use-expressions="true"> 

    <security:form-login 
     login-page="/login.html" 
     login-processing-url="/j_spring_security_check.action" 
     default-target-url="/index.html" 
     always-use-default-target="true" 
     authentication-failure-url="/login.html?error=true" /> 
    <security:intercept-url pattern="/login*" access="isAnonymous()" /> 
    <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" /> 
</security:http> 

<security:authentication-manager> 
    <security:authentication-provider> 
     <security:jdbc-user-service 
      data-source-ref="dataSource" 
      users-by-username-query="select username, password, enabled from smartcaldb.users where username=?" 
      authorities-by-username-query="select u.username, r.authority from smartcaldb.users u, smartcaldb.roles r where u.userid = r.userid and u.username =?" /> 
    </security:authentication-provider> 
</security:authentication-manager> 

彈簧-config.xml中

<mvc:annotation-driven /> 
<context:component-scan base-package="com.smartcal.**" /> 

<!-- setup database connectivity bean --> 

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" 
    destroy-method="close"> 
    <property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="${jdbc.username}" /> 
    <property name="password" value="${jdbc.password}" /> 
</bean> 

<context:property-placeholder location="/WEB-INF/jdbc.properties" /> 

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
    <constructor-arg ref="dataSource"/> 
</bean> 

的web.xml

<welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
</welcome-file-list> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     /WEB-INF/spring-config.xml 
     /WEB-INF/spring-security.xml 
    </param-value> 
</context-param> 

<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

<servlet> 
    <servlet-name>dispatcher</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>dispatcher</servlet-name> 
    <url-pattern>/login</url-pattern> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> 
    <init-param> 
     <param-name>contextAttribute</param-name> 
     <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value> 
    </init-param> 
</filter> 
<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

<error-page> 
    <error-code>403</error-code> 
    <location>/403</location> 
</error-page> 

的login.html

<body> 
    <form action="/smartcal/j_spring_security_check.action" method="POST"> 
     <label for="username">User Name:</label> 
     <input id="username" name="j_username" type="text" /> 
     <label for="password">Password:</label> 
     <input id="password" name="j_password" type="password" /> 
     <input type="submit" value="Log In" /> 
    </form> 
</body> 

用於註銷使用URL - 「/ {} yourAppPathInTheContainer/j_spring_security_logout」

相關問題