SpringSecurity源码解读(二) successHandler
来源:互联网 发布:java 搜索框架 编辑:程序博客网 时间:2024/06/10 17:33
前言:这个successHandler就是说认证成功之后该怎么跳转页面。这里需要先有个认识,有三个地方可以存储跳转的页面地址
1)request(对应属性targetUrlParameter)
2)配置文件(对应属性defaultTragetUrl)
3)缓存中
这里的流程涉及三个类,不过都是一个类以及它的子类和子类的子类而已,这里会倒序进行解读。
public class SavedRequestAwareAnthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{
protected final Log logger = LogFactory.getLog(this.getClass());
//这是SpringSecurity专门用来处理在SESSION中的REQUEST的类,这里涉及到缓存概念,不展开
//它在这里的功能在存储进行登陆前所在页面,方便登录后跳转回用户刚刚所处的页面
private RequestCacha requestCache = new HttpSessionRequestCache();
//AbstractAuthenticationProcessFilter里面调用的方法
public void onAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response,Authentication authentication) throws ServletException,IOException{
//从session中取出登陆前的请求对象,里面有登陆前页面的url
SavedRequest savedRequest = requestCache.getRequest(request,response);
//如果不存在缓存的request,那么从其他位置获取跳转的url信息,是父类实现的
if(saveRequest == null){
super.onAuthenticationSuccess(request,response,authentication);
return;
}
//在配置文件配置,通过request来传递跳转的页面
String targetUrlParameter = getTargetUrlParameter();
//判断条件(此时缓存存在):1.是否总是用默认的url(可在配置文件中配置) 2.是否通过request来获取url
if(isAlwaysUserDefaultTargetUrl()||(targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))){
//干掉缓存
requestCache.removeRequest(request,response);
super.onAuthenticationSuccess(request,response,authentication);
return;
}
//清除掉由登录失败存进session中的错误(关于登录失败存错误请看上一篇)
clear AuthenticationAttributes(request);
//根据缓存获取url
String targetUrl = saveRequest.getRedirectUrl;
logger.debug("Redirecting to DefaultSavedRequest Url :" + targetUrl);
getRedirectStrategy().sendRedirect(request,response,targetUrl);
}
}
public class SimpleUrlAuthenticationSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler implements AuthenticationSuccessHandler{
//没有自己的属性
public void onAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response,Authentication authentication) throws IOException,ServletException{
//方法在抽象父类中,用于重定向跳转页面
handler(request,response,authentication);
//看上面
1)request(对应属性targetUrlParameter)
2)配置文件(对应属性defaultTragetUrl)
3)缓存中
这里的流程涉及三个类,不过都是一个类以及它的子类和子类的子类而已,这里会倒序进行解读。
public class SavedRequestAwareAnthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler{
protected final Log logger = LogFactory.getLog(this.getClass());
//这是SpringSecurity专门用来处理在SESSION中的REQUEST的类,这里涉及到缓存概念,不展开
//它在这里的功能在存储进行登陆前所在页面,方便登录后跳转回用户刚刚所处的页面
private RequestCacha requestCache = new HttpSessionRequestCache();
//AbstractAuthenticationProcessFilter里面调用的方法
public void onAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response,Authentication authentication) throws ServletException,IOException{
//从session中取出登陆前的请求对象,里面有登陆前页面的url
SavedRequest savedRequest = requestCache.getRequest(request,response);
//如果不存在缓存的request,那么从其他位置获取跳转的url信息,是父类实现的
if(saveRequest == null){
super.onAuthenticationSuccess(request,response,authentication);
return;
}
//在配置文件配置,通过request来传递跳转的页面
String targetUrlParameter = getTargetUrlParameter();
//判断条件(此时缓存存在):1.是否总是用默认的url(可在配置文件中配置) 2.是否通过request来获取url
if(isAlwaysUserDefaultTargetUrl()||(targetUrlParameter != null && StringUtils.hasText(request.getParameter(targetUrlParameter)))){
//干掉缓存
requestCache.removeRequest(request,response);
super.onAuthenticationSuccess(request,response,authentication);
return;
}
//清除掉由登录失败存进session中的错误(关于登录失败存错误请看上一篇)
clear AuthenticationAttributes(request);
//根据缓存获取url
String targetUrl = saveRequest.getRedirectUrl;
logger.debug("Redirecting to DefaultSavedRequest Url :" + targetUrl);
getRedirectStrategy().sendRedirect(request,response,targetUrl);
}
}
public class SimpleUrlAuthenticationSuccessHandler extends AbstractAuthenticationTargetUrlRequestHandler implements AuthenticationSuccessHandler{
//没有自己的属性
public void onAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response,Authentication authentication) throws IOException,ServletException{
//方法在抽象父类中,用于重定向跳转页面
handler(request,response,authentication);
//看上面
阅读全文
0 0
- SpringSecurity源码解读(二) successHandler
- SpringSecurity 源码解读(一)
- SpringSecurity源码学习(二)
- SpringSecurity应用(二)
- SpringSecurity应用(二)
- RequireJS源码解读(二)
- ReactiveCocoa源码解读(二)
- SpringSecurity源码学习(一)
- SpringSecurity源码学习(三)
- SpringSecurity源码学习(五)
- SpringSecurity源码学习(六)
- SpringSecurity源码解读(三) AbstractAuthenticationProcessFilter和UsernamePasswordAuthenticationFilter补全
- Caffe源码解读(二):Blob类的源码解读
- Ext4.0源码解读(分享二)
- android-async-http源码解读(二)
- MPAndroidChart系列源码解读(二)
- jquery3.0源码解读(二)Extend
- SEDA源码解读(二)
- 推荐几个精致的 web UI框架
- 微信小程序开发Flex布局总结
- Android学习之MediaPlayer 音频播放器
- SharePoint 2016 配置工作流环境
- 关注民情民生
- SpringSecurity源码解读(二) successHandler
- 后缀数组模版O(N*2个log)求Sa[i]+字符串查找
- 在Ubuntu/Linux环境下使用MySQL:安装MySQL
- React笔记
- unity profiler深入学习4:gpu usage profiler
- 1.11. 集成方法
- java程序实现开机自启动功能
- Spring Boot关于代码结构两点建议
- InputStreamReader和OutputStreamWriter转换流