我正在使用Spring security 4基于XML的配置 .
这是我的配置:
<security:http use-expressions="true" authentication-manager-ref="authenticationManager" entry-point-ref="authenticationEntryPoint">
<security:intercept-url pattern="/**" access="hasRole('ROLE_USER')"/>
<security:form-login authentication-success-handler-ref="authenticationSuccessHandler"
authentication-failure-handler-ref="authenticationFailureHandler"
/>
<security:logout success-handler-ref="logoutSuccessHandler"/>
<security:csrf disabled="true"/>
</security:http>
<security:authentication-manager id="authenticationManager">
<security:authentication-provider>
<security:user-service>
<security:user name="username" authorities="ROLE_USER" password="password"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>
<bean id="authenticationEntryPoint" class="package.CustomBasicAuthenticationEntryPoint">
authenticationEntryPoint
具有以下实现:
public class CustomBasicAuthenticationEntryPoint implements AuthenticationEntryPoint {
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
}
}
The problem is 当我尝试进行身份验证时:
http://localhost:8080/myApp/api/j_spring_security_check
with body: j_password=password&j_username=username
由于我的自定义入口点,我总是有401错误状态 . 在我看来,Spring安全性并没有调用身份验证管理器 . 我错过了什么吗?
谢谢你的帮助 .
Update
感谢您的回答,我一直在使用Spring Security 3.2,我将j_username,j_password和j_spring_security_check更改为用户名,密码和登录名 . 我仍然有同样的问题:401代码状态:即使我尝试使用表单(POST)进行身份验证,Spring Security也会调用自定义authenticationEntryPoint .
4 回答
对于Spring Security版本4,默认登录处理URL为:
http://localhost:8080/myApp/login
(
j_spring_security_check
在早期版本中使用过 . )请注意,这是表单登录,与HTTP基本身份验证无关 .
在Spring 4表单字段中,默认名称从
j_username
和j_password
更改为username
和password
. 您应该在请求中更改它,或者在xml配置中明确设置它 . login-processing-url默认值也从j_spring_security_check
更改为login
. Here你可以找到一些关于它的信息 .问题摘要:
login
,请参阅Spring Scurity Reference:username
和password
,参见Spring Security Reference:POST
,请参阅UsernamePasswordAuthenticationFilter#setPostOnly:form-login
,这不是HTTP Basic Authentication,请参阅Spring Security Reference:<http-basic /> </ HTTP> 然后,基本身份验证将优先,并将用于在用户尝试访问受保护资源时提示登录 . 如果您希望使用表单登录,则仍可在此配置中使用表单登录,例如通过嵌入在其他网页中的登录表单 .
在我的
web.xml
文件中,Spring Security过滤链的url-pattern为:/api/*
我必须在我的配置文件(登录表单)中将login-processing-url:
"/api/"
添加相同的url-pattern以使其工作:谢谢你的回答 .