java - 自定义页面登录表单以利用 spring 登录身份验证

标签 java spring-mvc authentication spring-security

我之前使用过 spring 的默认登录表单来验证用户身份。现在,我尝试像大多数网站一样在我的主页上的 div 内创建一个登录表单。 action 标签的值等于 /login 我猜这是 Spring 登录身份验证的默认 URL。但我收到 404 请求的资源不可用。如何使用自定义表单实现spring登录认证?下面是我的代码。

登录表单:

       <form action="${pageContext.request.contextPath}/login" method="POST">
            <div class="w100" style=""></div>
            <div class="w100 formHeading">
                <fmt:message key="login"></fmt:message>
            </div>
            <div class="w100">
                <label for="username">Username</label> 
                <input type="text"
                    id="username" name="username" /> 
                <label for="password">Password</label>
                <input type="password" id="password" name="password" />
                <div class="w100">
                    <button type="submit" class="btn">Log in</button>
                </div>
        </form>

安全配置:

<security:http auto-config="true" use-expressions="true">
    <security:intercept-url pattern="/static/**" access="permitAll" />
    <security:intercept-url pattern="/login" access="permitAll"  />
    <security:intercept-url pattern="/" access="permitAll" />
    <security:intercept-url pattern="/*" access="denyAll" />

    <security:form-login login-page="/login" default-target-url="/" />
    <security:csrf disabled="true" />
</security:http>
<security:global-method-security secured-annotations="enabled"></security:global-method-security>

最佳答案

以下内容适用于 Spring Security 的版本 3.x.x(4.x.x 有所不同,感谢 @Ritesh 指出):

处理登录表单数据的默认 URL 是 /j_spring_security_check (这与“登录页面”不同)。所以:

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

另请注意,默认情况下,输入字段应命名为 j_usernamej_password

关于java - 自定义页面登录表单以利用 spring 登录身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33154109/

相关文章:

java - SeekBar 用于录制 30 秒声音 - handlerReceiveCallback 错误

java - 使新字体可供 JVM 使用

java - 如何在 Spring MVC 中发送 HTML 电子邮件?

php - stackoverflow 的 openid for PHP 实现

c# - .Net Core 2.0 中的 Windows 和匿名身份验证

java - 继承类型转换编译错误

java - 我们如何使用 Google Cloud Java API 执行数据存储不平等和 "IN"过滤器?

java - GSS 异常 : No valid credentials provided (Mechanism level: Failed to find any Kerberos tgt)

eclipse - 类未找到 : org. springframework.web.servlet.DispatcherServlet

python - 如何在没有 Oauth 的情况下登录 twitter 脚本?