认证成功的处理
登录成功后,如果除了跳转页面还需要执行一些自定义代码时,如:统计访问量,推送消息等操作时,可以自定义登录成功处理器。
自定义登录成功处理器
/** * @Author yqq * @Date 2022/05/17 17:06 * @Version 1.0 */publicclassLoginSuccessHandlerimplementsAuthenticationSuccessHandler{@OverridepublicvoidonAuthenticationSuccess(HttpServletRequest request,HttpServletResponse response,Authentication authentication)throwsIOException,ServletException{//拿到用户的信息UserDetails userDetails=(UserDetails)authentication.getPrincipal();System.out.println("用户名:"+ userDetails.getUsername());System.out.println("其他信息");//重定向到主页 response.sendRedirect("/main");}}
配置登录成功处理器
/** * 对Spring Security 配置 * @param http * @throws Exception */@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{//自定义表单登录 http.formLogin().loginPage("/login.html")//自定义登录页面.usernameParameter("username")//表单中的用户名项.passwordParameter("password")//表单中的密码项.loginProcessingUrl("/login")//表单提交路劲,提交后执行认证逻辑// .successForwardUrl("/main")//登录成功后的路劲.successHandler(newLoginSuccessHandler())//登录成功的处理器.failureForwardUrl("/fail");//登录失败后的路劲
测试
认证失败的处理
登录失败后,如果除了跳转页面还需要执行一些自定义代码时,如:统计失败次数,记录日志等,可以自定义登录失败处理器。
自定义登录失败处理器
/** * @Author yqq * @Date 2022/05/17 17:25 * @Version 1.0 */publicclassLoginFailureHandlerimplementsAuthenticationFailureHandler{@OverridepublicvoidonAuthenticationFailure(HttpServletRequest request,HttpServletResponse response,AuthenticationException exception)throwsIOException,ServletException{System.out.println("记录失败日志。。。。"); response.sendRedirect("/fail");}}
配置登录失败处理器
/** * 对Spring Security 配置 * @param http * @throws Exception */@Overrideprotectedvoidconfigure(HttpSecurity http)throwsException{//自定义表单登录 http.formLogin().loginPage("/login.html")//自定义登录页面.usernameParameter("username")//表单中的用户名项.passwordParameter("password")//表单中的密码项.loginProcessingUrl("/login")//表单提交路劲,提交后执行认证逻辑// .successForwardUrl("/main")//登录成功后的路劲.successHandler(newLoginSuccessHandler())//登录成功的处理器// .failureForwardUrl("/fail");//登录失败后的路劲.failureHandler(newLoginFailureHandler());//登录失败的处理器//需要认证的资源 http.authorizeRequests().antMatchers("/login.html").permitAll()//登录页默认授权不需要认证.antMatchers("/fail").permitAll()//失败页面不需要认证.anyRequest().authenticated();//其余所有请求都需要认证
测试
热门文章
- 「1月20日」最高速度21.7M/S,2025年Shadowrocket/Clash/V2ray/SSR每天更新免费节点订阅链接
- 「1月2日」最高速度19.9M/S,2025年SSR/V2ray/Clash/Shadowrocket每天更新免费节点订阅链接
- 「12月31日」最高速度20.5M/S,2025年Shadowrocket/V2ray/SSR/Clash每天更新免费节点订阅链接
- 家用小型饲料颗粒机500元一台(家用小型饲料颗粒机图片视频展示)
- 「11月19日」最高速度18.2M/S,2024年Shadowrocket/SSR/V2ray/Clash每天更新免费节点订阅链接
- 动物医院的单位性质是(动物医院单位性质代码)
- 「12月19日」最高速度19.9M/S,2024年SSR/Clash/Shadowrocket/V2ray每天更新免费节点订阅链接
- 动物美容院简笔画(动物美容师哪里有培训)
- 动物疫苗官网查询网站下载(动物疫苗查询系统)
- 宠物领养制度怎么写(宠物领养中心领养条件是什么)