Java AuthenticationSuccessHandler上的Spring MVC响应
假设存在要登录的REST API:Java AuthenticationSuccessHandler上的Spring MVC响应,java,spring-mvc,spring-security,Java,Spring Mvc,Spring Security,假设存在要登录的REST API: /api/user/login 在SpringSecurity中,很容易替换HTTP入口点/成功处理程序/失败处理程序,如下所示 <http pattern="/**" use-expressions="true" entry-point-ref="restAuthenticationEntryPoint"> <form-login authentication-success-handl
/api/user/login
在SpringSecurity中,很容易替换HTTP入口点/成功处理程序/失败处理程序,如下所示
<http
pattern="/**"
use-expressions="true"
entry-point-ref="restAuthenticationEntryPoint">
<form-login
authentication-success-handler-ref="mySuccessHandler"
authentication-failure-handler-ref="myFailureHandler"
login-processing-url="/api/user/login"
username-parameter="login"
password-parameter="password"/>
<logout
invalidate-session="true"
delete-cookies="WSESSIONID"
logout-url="/api/user/logout"/>
<csrf disabled="true"/>
<session-management>
<concurrency-control max-sessions="1" />
</session-management>
....
</http>
我正在寻找一种方法,在成功和失败状态下返回一个与Spring MVC控制器相同的对象。在这种情况下,响应编码必须与MVC控制器相同,并考虑从请求头接受 请问您为什么需要它?这是一个RESTFull服务,成功的结果是用户详细信息。另一方面,失败的结果是业务异常。客户端可能需要JSON/XML作为结果。为什么不在LoginController中“返回对象”?我使用Spring安全筛选器,身份验证过程不是Spring MVC的一部分。为什么不定义一个类,定义一个采用所需参数的方法,并在
onAuthenticationSuccess()之前调用该方法
或onAuthenticationFailure()
退出?
public class RestUrlAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws ServletException, IOException {
response.getOutputStream().write("success".getBytes());
clearAuthenticationAttributes(request);
}
}