Spring 我的CustomUsernamePasswordAuthenticationFilter未应用
我有一个使用spring安全过滤器对用户进行身份验证的服务器。客户端以json格式发送其登录凭据以确保有效性。因此,我创建了自定义UsernamePasswordAuthenticationFilter,并在http标记中声明了它 登录页面已经在客户端应用程序中。客户机需要做的只是在其请求数据包中使用用户给定的凭据创建http post请求,并将其发送到服务器以验证其有效性。服务器将回复“有效”/“无效” spring-security.xml:Spring 我的CustomUsernamePasswordAuthenticationFilter未应用,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,我有一个使用spring安全过滤器对用户进行身份验证的服务器。客户端以json格式发送其登录凭据以确保有效性。因此,我创建了自定义UsernamePasswordAuthenticationFilter,并在http标记中声明了它 登录页面已经在客户端应用程序中。客户机需要做的只是在其请求数据包中使用用户给定的凭据创建http post请求,并将其发送到服务器以验证其有效性。服务器将回复“有效”/“无效” spring-security.xml: <http use-expressions
<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>
@Controller
公共类连接控制器{
@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";
}
}
问题:
我的客户希望通过包含登录凭据的post请求直接调用url/resources/login
。但控件总是进入入口点ref=“loginUrlAuthenticationEntryPoint”并返回我在控制器中给出的“EntryPoint”字符串。我想直接应用自定义过滤器而不调用“入口点引用”
我想这是我能解释我的问题的最好办法了。如果问题陈述中有任何疑问,请询问。请回复解决方案。我没有在这里包括CustomUsernamePasswordAuthenticationFilter.java文件。LoginRuthenAuthenticationEntryPoint将用户发送到/resources/entrypoint(如bean定义中所示) /资源/入口点映射为允许所有
<intercept-url pattern="/resources/entryPoint" access="permitAll" />
这就是请求转到entrypoint url-/resources/entrypoint的原因,而您的控制器中的entrypoint又是mapepd,以返回字符串“entrypoint”
这里有一个很好的spring安全教程-这是一个愚蠢的错误。问题在于我发布的url
url/resources/login
用于验证资源。它应该是url/j\u spring\u security\u check
。在UsernamePasswordAuthenticationFilter中设置的As filterProcessUrl是
。
除此URL之外的任何内容都将反映到入口点ref标记
我的客户端是android应用程序,使用HttpUrlConnection发布请求。HttpUrlConnection正在使用我的数据创建post请求,我尚未指定表单提交操作。在阅读了Paul John分享的几篇文章和一篇文章后,这一行:-
实际上是在调用安全过滤器。无论如何,问题解决了。发生在初学者身上:)hi@praveen有用吗?没用。但我的问题解决了。我会把它贴在回信里。谢谢你的教程链接。