Spring Security登录页面

时间:2011-09-09 12:19:03

标签: java jquery spring login spring-security

我开发了一个使用Spring Security默认登录页面的应用程序。但是我想实现自己的登录页面。我会放一个login.html而不是一个jsp页面。我想用它来使用JQuery。我检查了许多例子,但无法实现。我是Spring和Spring Security的新手,我使用的是Spring Security 3。  我应该遵循哪些步骤?

2 个答案:

答案 0 :(得分:45)

Spring Security中的自定义登录页面有四个要求:

  1. 有一个名为j_username的输入字段,其中包含用于身份验证凭据的名称。
  2. 有一个名为j_password的输入字段,其中包含用于身份验证凭据的密码。
  3. 这些值为POST的网址与Spring Security配置中login-processing-url元素的form-login属性中定义的网址相匹配。
  4. 必须在Spring Security配置中login-page元素的form-login属性中指定自定义登录表单的位置。
  5. <强>的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>
    

    Spring安全配置文件

        <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>
    

答案 1 :(得分:6)

我已经在我的项目中实施Spring安全性工作了几天,最终完成的配置如下:

<强>弹簧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>

用于注销使用网址 - “/ {yourAppPathInTheContainer} / j_spring_security_logout”

相关问题