基本概念

暂无。

官方文档

https://docs.spring.io/spring-security/site/docs/5.3.1.BUILD-SNAPSHOT/reference/html5/#nsa-form-login

https://docs.spring.io/autorepo/docs/spring-security/3.2.4.RELEASE/apidocs/org/springframework/security/web/authentication/SavedRequestAwareAuthenticationSuccessHandler.html

API

SavedRequestAwareAuthenticationSuccessHandler:身份验证成功策略,可以利用身份验证成功策略,该策略DefaultSavedRequest可能已由会话存储在会话中ExceptionTranslationFilter。当此类请求被拦截并需要进行身份验证时,将存储请求数据以记录身份验证过程开始之前的原始目的地,并允许在重定向到相同URL时重构请求。如果合适,此类负责执行重定向到原始URL的操作。

成功进行身份验证后,它将根据以下情况决定重定向目标:

  • 如果该alwaysUseDefaultTargetUrl属性设置为true,defaultTargetUrl 则将用于目标。任何DefaultSavedRequest存储在会话将被删除。
  • 如果targetUrlParameter已在请求中设置,则该值将用作目的地。任何DefaultSavedRequest都将再次被删除。
  • 如果在SavedRequest中找到了RequestCache(由设置为在ExceptionTranslationFilter身份验证过程开始之前记录原始目标),则将重定向到该原始目标的Url。SavedRequest收到重定向的请求后,该对象将保持缓存并被拾取(请参阅参考资料SavedRequestAwareWrapper)。
  • 如果SavedRequest找不到,它将委派给基类。

需求分析

1.通过登录页登录后,跳转到后台首页 。

例如,直接打开login.htm登录,登录成功后应跳转到admin/adminIndex.htm

2.直接访问后台其他需要权限的页面,因为权限控制的原因会被跳转到登录页,登录成功后,应在此跳转到想直接访问的页面。

例如,admin/b.htm需要权限才可以访问,未登录的无权限用户直接访问改页面,会被跳转到登录页login.htm,登陆成功后,应自动跳转到admin/b.htm页。

解决方案

当在ExceptionTranslationFilter中拦截时,会调用HttpSessionRequestCache保存原始的请求信息。

在UsernamePasswordAuthenticationFilter过滤器登录成功后,会调用SavedRequestAwareAuthenticationSuccessHandler。

自定义一个MyAuthenticationSuccessHandler类,继承自SavedRequestAwareAuthenticationSuccessHandler,并在其中的onAuthenticationSuccess将页面重定向至需要的URL。

/*** @Author ShenTuZhiGang* @Version 1.0.0* @Date 2020-03-21 13:10*/@Component
public class CustomSavedRequestAwareAuthenticationSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {@Overridepublic void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws ServletException, IOException {RequestCache requestCache = new HttpSessionRequestCache();SavedRequest savedRequest = requestCache.getRequest(httpServletRequest,httpServletResponse);if(savedRequest != null){//url = savedRequest.getRedirectUrl();}else{getRedirectStrategy().sendRedirect(httpServletRequest,httpServletResponse,"/index");}super.onAuthenticationSuccess(httpServletRequest, httpServletResponse, authentication);}
}

Spring Security配置文件中需要设置authentication-success-handler-ref 

<bean id="myAuthenticationSuccessHandler" class="com.jiyufei.security.security.MyAuthenticationSuccessHandler"></bean>
<sec:http auto-config="true" use-expressions="false"><sec:intercept-url pattern="/admin/login.htm" access="IS_AUTHENTICATED_ANONYMOUSLY"/><sec:intercept-url pattern="/error/*" access="IS_AUTHENTICATED_ANONYMOUSLY"/><sec:intercept-url pattern="/admin/*.htm" access="ROLE_ADMIN,ROLE_USER"/><sec:intercept-url pattern="/*.htm" access="IS_AUTHENTICATED_ANONYMOUSLY"/><sec:form-login login-page="/admin/login.htm" username-parameter="mail" password-parameter="password"authentication-success-handler-ref="myAuthenticationSuccessHandler" authentication-failure-url="/admin/login.htm?err=1" login-processing-url="/admin/check.htm"/></sec:http>

Spring Boot WebSecurity 配置类中需要配置.successHandler(customSavedRequestAwareAuthenticationSuccessHandler)

/*** @Author ShenTuZhiGang* @Version 1.0.0* @Date 2020-03-07 16:48*/
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MyZSTUWebSecurityConfig extends WebSecurityConfigurerAdapter {@AutowiredIUserService iUserService;@AutowiredCustomFilterInvocationSecurityMetadataSource customFilterInvocationSecurityMetadataSource;@AutowiredCustomAccessDecisionManager customAccessDecisionManager;@AutowiredAuthenticationAccessDeniedHandler authenticationAccessDeniedHandler;@AutowiredCustomSavedRequestAwareAuthenticationSuccessHandler customSavedRequestAwareAuthenticationSuccessHandler;@AutowiredCustomAuthenticationFailureHandler customAuthenticationFailureHandler;@AutowiredCustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;@BeanPasswordEncoder passwordEncoder(){return NoOpPasswordEncoder.getInstance();}@Overridepublic void configure(WebSecurity web){web.ignoring().antMatchers("/index.html","/student/**","/wx/**","/qq/**");}@Overrideprotected void configure(AuthenticationManagerBuilder auth)throws Exception{auth.userDetailsService(iUserService);}@Overrideprotected void configure(HttpSecurity http)throws Exception{http.authorizeRequests().withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {@Overridepublic <O extends FilterSecurityInterceptor> O postProcess(O object) {object.setSecurityMetadataSource(customFilterInvocationSecurityMetadataSource);object.setAccessDecisionManager(customAccessDecisionManager);return object;}}).and().formLogin()//.loginPage("/login").loginProcessingUrl("/login").usernameParameter("username").passwordParameter("password").failureHandler(customAuthenticationFailureHandler)//本需求关键句.successHandler(customSavedRequestAwareAuthenticationSuccessHandler).permitAll().and().logout().permitAll().and().csrf().disable().exceptionHandling().accessDeniedHandler(authenticationAccessDeniedHandler);}
}

参考文章

https://www.jianshu.com/p/e1f41b27e902

https://my.oschina.net/jiyufei/blog/1635118

Spring Security——实现登录后跳转到登录前页面相关推荐

  1. Struts 拦截器权限控制【通过拦截器实现登录后跳转到登录前页面】

    应用情形:在web项目中,经常会遇到用户未登录或SESSION失效时用户发出非法的权限操作,如新闻的评论.文件的下载等等,在此我们可以使用struts拦截器对该用户发出的请求进行拦截,拦截后判断用户是 ...

  2. Django里自定义用户登陆及登陆后跳转到登陆前页面的实现

    因为下一步要和公司的UM帐号作集成,所以分离出登陆模块,及实现其基本功能是必不可少的. 登陆倒容易,但要实现在登陆后,跳转到登陆前的网页,且显示用户的登陆状态,花了点时间查找代码, 测试了五六种方式, ...

  3. cas → 注销登录后跳转到登录页

    cas单点登出后,默认会跳到它自带的注销界面(cas-server部署见:cas5.3:CAS Server搭建 ),业务需求往往需要跳转到自定义页面,具体实现如下: 在 application.pr ...

  4. php登陆框_PHP 登录完成跳转上一访问页面

    需求描述: 访问网站页面时,有的页面需要授权才能访问,这时候就会要求用户登录,跳转到登录页面login.php,怎么实现登录后返回到刚才访问的页面. 解决思路1: 在跳转到登录页面之前要将当前访问页面 ...

  5. Spring Security Oauth2 授权码模式下 自定义登录、授权页面

    主要说明:基于若依springcloud微服务框架的2.1版本 嫌弃缩进不舒服的,直接访问我的博客站点: http://binarydance.top//aticle_view.html?aticle ...

  6. Spring Security技术栈学习笔记(八)Spring Security的基本运行原理与个性化登录实现

    正如你可能知道的两个应用程序的两个主要区域是"认证"和"授权"(或者访问控制).这两个主要区域是Spring Security的两个目标."认证&qu ...

  7. Spring Security和Angular教程(二)登录页面

    Spring Security和Angular教程(二) 登录页面 在本节中,我们将继续讨论如何在"单页面应用程序"中使用带有Angular的Spring Security.在这里 ...

  8. 织梦会员登录PHP,DEDECMS会员登录后跳转首页的简单实现方法

    DEDECMS会员登录后跳转首页的简单实现方法 互联网   发布时间:2009-06-06 15:06:24   作者:jb51   我要评论 关键字描述:简单 实现 方法 首页 会员 登录 & ...

  9. php页面怎么去登录,php中登录后跳转回原来要访问的页面实例

    在很多网站用户先访问一个要登录的页面,但当时没有登录后来登录了,等待用户登录成功之后肯定希望返回到上次访问的页面,下面我就来给大家介绍登录后跳转回原来要访问的页面实例 最简单的办法就是直接使用php ...

最新文章

  1. java 顺序 读写 Properties 配置文件 支持中文 不乱码
  2. TestNG+Maven+IDEA环境搭建
  3. android 版本28 通知栏图标,【专题分析】应用图标、通知栏适配
  4. 如何暂停一个正在运行的线程?
  5. VB案例:宁越电子琴
  6. 一加9系列曝光:骁龙875旗舰芯片加持 可能存在超过四种机型
  7. c语言如何将8个字符串串联_C ++中的字符串串联:串联字符串的4种方法
  8. 【转】多态与 new [C#]
  9. 2018-09-10-整车开发流程名词解释
  10. C语言程序设计实验报告——实验六
  11. 『暗香记忆』十世成佛
  12. 徐磊英语语法整理1-3 词性,语序
  13. spi通信问题-有波形但无法获取正确数据:MOSI和SCK
  14. nginx配置和优化详解
  15. 球面经纬度坐标转换为空间直角坐标
  16. linux ps -aux 怎么看内存,Linux下ps aux命令 与 查看CPU配置
  17. 2022年焊工(初级)试题及答案
  18. 《Migrating to Cloud-Native Application Architectures》学习笔记之Chapter 3. Migration Cookbook...
  19. 让人面到崩溃的特斯拉.NET工程师面试题
  20. Linux系统中计划任务及其相关命令操作(at和crontab)

热门文章

  1. python股票交易编程最好的书_用Python 进行股票分析 有什么好的入门书籍或者课程吗?...
  2. 脑子笨的人可以学计算机吗,脑子笨的人有什么特征 反应慢的人其实很聪明
  3. android builder模式 插件,如何在Kotlin中实现Builder模式?
  4. php artisan 出错,Laravel 5.2 php artisan migrate:回滚错误
  5. java chars_Java getChars() 方法 - Java 基础教程
  6. 在WPS中提取出的照片在哪找_WPS技巧 | 找不到合适的配图?教你一招搞定
  7. 翻译职称计算机能力,2018年职称计算机word2003考点辅导:用好Office2003中的翻译功能...
  8. php旋转数组找出最小的,LeetCode 153 寻找旋转排序数组中的最小值
  9. 空值替换为0_「Excel」是零值还是空值,你可以自由掌控
  10. android camera 实时滤镜,【Camera】Android平台Camera实时滤镜实现方法