我的 CustomUsernamePasswordAuthenticationFilter 不适用

My CustomUsernamePasswordAuthenticationFilter is not applying

我有一台使用 spring 安全过滤器对用户进行身份验证的服务器。客户端以 json 格式发送其登录凭据以确保有效性。所以我创建了我的自定义 UsernamePasswordAuthenticationFilter 过滤器并在 http 标签内声明它。

登录页面已经在他们的客户端应用程序中。客户端需要做的就是创建 http post 请求,并在其请求数据包中使用用户给定的凭据并将其发送到服务器以验证有效性。服务器会回复"valid"/"invalid".

spring-security.xml:

<http use-expressions="true" auto-config="false"
    entry-point-ref="loginUrlAuthenticationEntryPoint">

    <custom-filter ref="CustomUsernamePasswordAuthenticationFilter"
        position="FORM_LOGIN_FILTER " />
    <access-denied-handler error-page="/resources/accessDenied" />
    <intercept-url pattern="/resources/entryPoint" access="permitAll" />
    <intercept-url pattern="/resources/**" access="hasRole('ROLE_ADMIN')"/>
    <intercept-url pattern="/**" access="permitAll" />
</http>

<beans:bean id="CustomUsernamePasswordAuthenticationFilter"
    class="com.controller.CustomUsernamePasswordAuthenticationFilter">
    <beans:property name="authenticationManager" ref="authenticationManager" />
    <beans:property name="authenticationSuccessHandler"
        ref="successHandler" />
    <beans:property name="authenticationFailureHandler"
        ref="failureHandler" />
    <beans:property name="filterProcessesUrl" value="/j_spring_security_check" />
    <beans:property name="usernameParameter" value="j_username" />
    <beans:property name="passwordParameter" value="j_password" />
</beans:bean>

<authentication-manager alias="authenticationManager">
    <authentication-provider>
        <user-service>
            <user name="pra" password="321" authorities="ROLE_ADMIN,ROLE_USER" />
            <user name="user" password="password" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

<beans:bean id="successHandler"
    class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
    <beans:property name="defaultTargetUrl" value="/resources/login" />
</beans:bean>


<beans:bean id="failureHandler"
    class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">
    <beans:property name="defaultFailureUrl" value="/resources/accessDenied" />
</beans:bean>

<beans:bean id="loginUrlAuthenticationEntryPoint"
    class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
    <beans:property name="loginFormUrl" value="/resources/entryPoint" />
</beans:bean>

mvc 控制器:

@Controller

public class HookUpController {

@RequestMapping( value = "/", method = RequestMethod.POST )
@ResponseStatus(HttpStatus.OK)
@ResponseBody
private String homePage(){

    return "Home Page";
}

@RequestMapping( value = "/resources/entryPoint", method = RequestMethod.GET )
@ResponseStatus(HttpStatus.OK)
@ResponseBody
private String entryPoint(){

    return "EntryPoint";
}

@RequestMapping( value = "/resources/accessDenied", method = RequestMethod.POST )
@ResponseBody
private String accessDenied(){      
    return "invalid";
}

@RequestMapping(value = "/resources/login", method = RequestMethod.POST)
@ResponseBody
private String login() {
    return "valid";
}

}

问题:

我的客户想直接调用 url/resources/login 并使用包含登录凭据的 post 请求。但是控制总是进入 entry-point-ref="loginUrlAuthenticationEntryPoint" 并返回 "EntryPoint" 字符串,正如我在控制器中给出的那样。我想直接应用自定义过滤器而不调用 "entry-point-ref".

我认为这是关于我的问题的最佳解释。如果对问题陈述有任何疑问,请提出。并请回复解决方案。我没有在此处包含 CustomUsernamePasswordAuthenticationFilter.java 文件。

loginUrlAuthenticationEntryPoint 将用户发送到 /resources/entrypoint(在您的 bean 定义中)。

/resources/entrypoint被映射为允许所有

<intercept-url pattern="/resources/entryPoint" access="permitAll" />

这就是请求进入入口点 url - /resources/entrypoint 的原因,而入口点又在您的控制器中映射到 return 字符串 "EntryPoint"

这是一个很好的 spring 安全教程 - http://javakart.blogspot.ae/2012/12/spring-security-tutorial-form-login-example.html

这是个愚蠢的错误。问题是 url url/resources/login 我正在 post 验证资源。应该是url/j_spring_security_check。由于在 UsernamePasswordAuthenticationFilter 中设置的 filterProcessUrl 是 <beans:property name="filterProcessesUrl" value="/j_spring_security_check" />。 除此 URL 之外的任何内容都将反映到 entry-point-ref 标签。

我的客户端是 android 应用,post 使用 HttpUrlConnection 发送请求。 HttpUrlConnection 正在使用我的数据创建 post 请求,但我没有指定表单提交操作。在阅读了一些 post 和 Paul John 分享的内容后,这一行:-<form name='loginForm' action="j_spring_security_check" method='POST'> 实际上是在调用安全过滤器。无论如何问题解决了。发生在初学者 :)