在整个学习过程当中,我最关心的内容有号几点,其中一点是【先后端分离的状况下如何不跳转页面而是返回须要的返回值】。
下面就说一下学习结果,以xml配置位李。html
在spring官方文档5.0.12.RELEASE第6.2.3节,有这么一段描述:spring
要进一步控制目标,能够使用authentication-success-handler-ref属性做为default-target-url的替代。 引用的bean应该是AuthenticationSuccessHandler的一个实例。 您能够在Core Filters一章以及命名空间附录中找到更多相关信息,以及有关如何在身份验证失败时自定义流的信息。
刚开始的时候我没有注意到这个内容,后来看了spring-security-5.0.xsd文件才找到这个配置。
在xsd文件中,对这个属性是这样描述的:json
<xs:attribute name="authentication-success-handler-ref" type="xs:token"> <xs:annotation> <xs:documentation> 引用应该用于处理a的AuthenticationSuccessHandler bean成功的认证请求。 不该与之配合使用default-target-url(或always-use-default-target-url)应始终做为实现处理导航到后续目的地 </xs:documentation> </xs:annotation> </xs:attribute>
因此实现一个AuthenticationSuccessHandler的实现类:后端
public class LoginAuthenticationSuccessHandler implements AuthenticationSuccessHandler { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { System.out.println("===========登录成功================"); PrintWriter printWriter = response.getWriter(); Map<String, String> msgMap = new HashMap<>(); msgMap.put("result", "0"); msgMap.put("msg", "登陆成功"); printWriter.write(new Gson().toJson(msgMap)); printWriter.flush(); printWriter.close(); } }
配置xml:cookie
<security:http> <security:intercept-url pattern="/**" access="isAuthenticated()"/> <security:form-login authentication-success-handler-ref="loginAuthenticationSuccessHandler"/> <security:logout delete-cookies="JSESSIONID" invalidate-session="true" success-handler-ref="myLogoutSuccessHandler"/> <security:csrf disabled="true"/> </security:http> ...... <bean id="loginAuthenticationSuccessHandler" class="com.nyl.securitylearn.security.handler.LoginAuthenticationSuccessHandler"/>
测试结果:session
在spring官方文档5.0.12.RELEASE第6.2.4 logout handling节,提到这个内容:app
logout元素经过导航到特定URL添加了对注销的支持。 默认的注销URL是/logout,但您能够使用logout-url属性将其设置为其余内容。 有关其余可用属性的更多信息,请参见命名空间附录。
查找到对应的命名空间章节43.1.26 <logout>,其中关于配置的说明:前后端分离
//这个说明 success-handler-ref success-handler-ref May be used to supply an instance of LogoutSuccessHandler which will be invoked to control the navigation after logging out.
根听说明须要实现LogoutSuccessHandler接口。ide
实现一个LogoutSuccessHandler的实现类:学习
public class MyLogoutSuccessHandler implements LogoutSuccessHandler { @Override public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { System.out.println("===========登出成功================"); PrintWriter printWriter = response.getWriter(); response.setHeader("Content-Type", "application/json;charset=utf8"); Map<String, String> msgMap = new HashMap<>(); msgMap.put("result", "0"); msgMap.put("msg", "退出成功"); printWriter.write(new Gson().toJson(msgMap)); printWriter.flush(); printWriter.close(); } }
配置xml:
<context:component-scan base-package="com.nyl.securitylearn"/> <security:http> <security:intercept-url pattern="/**" access="isAuthenticated()"/> <security:form-login authentication-success-handler-ref="loginAuthenticationSuccessHandler"/> <security:logout delete-cookies="JSESSIONID" invalidate-session="true" success-handler-ref="myLogoutSuccessHandler"/> <security:csrf disabled="true"/> </security:http>
测试:
在43.1.21 <form-login>节中,提到了这样一个配置authentication-failure-handler-ref :
authentication-failure-handler-ref 可用做authentication-failure-url的替代方法,使您能够在身份验证失败后彻底控制导航流。 该值应该是应用程序上下文中AuthenticationFailureHandler bean的名称。
xsd中的说明:
引用应该用于处理失败的AuthenticationFailureHandler bean验证请求。 不该与authentication-failure-url结合使用, 由于实现应始终处理导航到后续目的地
测试配置基本和登陆成功一致,不啰嗦了。
这样就实现了基本的先后端调用要求,避免了路径跳转。