(Spring Security)学习五:SuccessHandler成功登录及FailureHandler、LogoutSuccessHandler...等用法

引言

一般成功登录后,需要触发一些操作,比如我们经常收到你异地登录的消息等,都是登录成功后触发的事务。SpringSecurity框架涵盖这部分。

认证成功处理

OursAuthenticationSuccessHandler 实现 AuthenticationSuccessHandler接口,重写onAuthenticationSuccess方法。

@Component("oursAuthenticationSuccessHandler")
public class OursAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException {

    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        Object principal = authentication.getPrincipal();
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(new ObjectMapper().writeValueAsString(principal));
        out.flush();
        out.close();
    }
}

OursAuthenticationFailureHandler、OursLogoutSuccessHandler、OursAuthenticationEntryPoint雷同,通过实现相应的接口,自定义处理行为。

配置

    @Autowired
    private AuthenticationSuccessHandler oursAuthenticationSuccessHandler;
    @Autowired
    private AuthenticationFailureHandler oursAuthenticationFailureHandler;
    @Autowired
    private LogoutSuccessHandler oursLogoutSuccessHandler;
    @Autowired
    private AuthenticationEntryPoint oursAuthenticationEntryPoint;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .anyRequest().authenticated()
                .and().formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/index")
                //登录成功
                .successHandler(oursAuthenticationSuccessHandler)
                //登录失败
                .failureHandler(oursAuthenticationFailureHandler)
                .permitAll()
                .and().logout()
                .logoutUrl("/logout")
                //注销成功
                .logoutSuccessHandler(oursLogoutSuccessHandler)
                .permitAll()
                .and().headers().frameOptions().sameOrigin()
                .and().csrf().disable()
                .exceptionHandling()
                //未认证处理
                .authenticationEntryPoint(oursAuthenticationEntryPoint)
        ;
    }

以上代码将认证成功、认证失败、注销成功和未认证均做了体现。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
根据引用\[1\],问题中提到的错误是"Cannot resolve org.springframework.security:spring-security-config:5.3.2.RELEASE"。这个错误通常表示在项目的pom文件中找不到所需的Spring Security依赖。根据引用\[1\]中的pom文件片段,可以看到项目中已经添加了spring-boot-starter-security依赖,但是可能缺少了spring-security-config依赖。 解决这个问题的方法是在项目的pom文件中添加spring-security-config依赖。可以在<dependencies>标签中添加以下代码: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>5.3.2.RELEASE</version> </dependency> ``` 这样就可以解决"Cannot resolve org.springframework.security:spring-security-config:5.3.2.RELEASE"的错误了。 #### 引用[.reference_title] - *1* *3* [微服务A读配置中心报Could not locate PropertySource错误](https://blog.csdn.net/weixin_53802962/article/details/113811491)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [springcloud框架搭建遇见的问题及解决办法](https://blog.csdn.net/m0_37546844/article/details/109525353)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

希克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值