具有自己的表单登录的Spring Security Filter无法正常工作

时间:2018-05-24 14:57:44

标签: java spring spring-security

我对stackoverflow和Spring安全性完全陌生。我有一个具有登录功能的网站。使用Spring Security的默认登录表单(基于DefaultLoginPageGeneratorFilter类)。

现在,我想基于.jsp文件实现自己的自定义设计表单。它看起来如下:

...    
<form name="f" method="post" action="/login">
        <h2>    Sign In</h2>
        <label>User Name:</label><br> <input id="username" type="text" name="<%=UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_USERNAME_KEY%>" placeholder="Enter Username"><br> <label>Password:</label><br>
        <input type="password" name="<%=UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY%>" placeholder="Enter Password"><br>
        <button>SIGN IN</button>
        </br> <a href="">Forgot Password?</a>
        <h2>New User</h2>
        <button name="submit" type="submit">SIGN UP</button>
</form>
...

但是,我的表单显示在网站上,但如果我点击提交按钮,则返回404。我认为我在spring.xml中的配置不正确:

<beans:import resource="../security-formAuth.xml" />

<http use-expressions="true">
    <intercept-url pattern="/Webapp/login.jsp"
        access="isAnonymous() or isAuthenticated()" />
    <intercept-url pattern="/**" access="isAuthenticated()" />

<custom-filter ref="formAuthenticationFilter" before="FORM_LOGIN_FILTER"/>

    <form-login login-page='/Webapp/login.jsp'
        default-target-url="/Webapp/login.jsp" authentication-failure-url="/Website/index.html" />

</http>


<beans:bean class="game.security.filter.PasswordLoginFilter"
    id="formAuthenticationFilter">
    <beans:property ref="authenticationProvider" name="authenticationManager" />
    <beans:property name="filterProcessesUrl" value="/login" />
    <beans:property name="authenticationSuccessHandler">
        <beans:bean
            class="org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler">
            <beans:property name="defaultTargetUrl" value="/Website/categoryLevels.html" />
        </beans:bean>
    </beans:property>
    <beans:property name="authenticationFailureHandler">
        <beans:bean
            class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
            <beans:property name="defaultFailureUrl" value="/login.jsp" />
        </beans:bean>
    </beans:property>
</beans:bean>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="user1" password="test" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

看起来我的过滤器PasswordLoginFilter根本没有被调用:

    public class PasswordLoginFilter extends UsernamePasswordAuthenticationFilter {

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
            throws AuthenticationException {

        return super.attemptAuthentication(request, response);
    }

}

我已经尝试了几个小时的解决方案,但我无法自己解决问题。如果您需要任何其他文件,请告诉我。

感谢您的帮助到目前为止:)

0 个答案:

没有答案