首页 > 其他分享 >认证失败处理器与注销成功处理器

认证失败处理器与注销成功处理器

时间:2023-04-26 19:44:43浏览次数:42  
标签:web http springframework 认证 处理器 org import 注销 security

认证失败处理器

    

 

 

package com.example.springsecurity.handler;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class SGFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        System.out.println("认证失败了");
    }
}

 

package com.example.springsecurity.Config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationSuccessHandler successHandler;

    @Autowired
    private AuthenticationFailureHandler failureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
//                配置认证成功处理器
                .successHandler(successHandler)
//                配置认证失败处理器
                .failureHandler(failureHandler);

        http.authorizeRequests().anyRequest().authenticated();
    }
}

 

注销成功处理器

 

package com.example.springsecurity.handler;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class SGLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        System.out.println("注销成功");
    }
}

 

package com.example.springsecurity.Config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationSuccessHandler successHandler;

    @Autowired
    private AuthenticationFailureHandler failureHandler;

    @Autowired
    private LogoutSuccessHandler logoutSuccessHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
//                配置认证成功处理器
                .successHandler(successHandler)
//                配置认证失败处理器
                .failureHandler(failureHandler);

        http.logout()
                //配置注销成功处理器
                .logoutSuccessHandler(logoutSuccessHandler);

        http.authorizeRequests().anyRequest().authenticated();
    }
}

 

标签:web,http,springframework,认证,处理器,org,import,注销,security
From: https://www.cnblogs.com/x3449/p/17357076.html

相关文章

  • SpringSecurity从入门到精通:认证成功处理器&认证失败处理器
    认证成功处理器  认证失败处理器  ......
  • django token 认证 简单记录
    classUser(AbstractUser):username=models.CharField(max_length=20,unique=True,primary_key=True,verbose_name="用户名")email=models.EmailField(max_length=256,null=False,verbose_name="邮箱",blank=True)pass......
  • drf之三大组件(认证组件、权限组件、频率组件)
    目录环境准备创建相关的表,models.py创建登录样例配置路由认证组件BaseAuthentication创建认证组件创建测试样例总结1.创建认证组件2.局部使用(只在一个视图类中使用,使用后会影响当前视图类中管理的所有接口)3.全局使用(对所有接口都生效)4.局部禁用权限组件BasePermission环境......
  • adobe认证证书
    Adobe认证证书分为产品技能认证和职业技能认证:产品技能认证AdobeCertifiedProfessionalPhotoshop认证专家AdobeCertifiedProfessionalIllustrator认证专家AdobeCertifiedProfessionalInDesign认证专家AdobeCertifiedProfessionalPremierePro认证专家AdobeCertifiedP......
  • adobe国际认证证书有用吗?
    获得Adobe国际认证证书,对于是否是创意设计师都可以赋予他们以下优势:①证明专业技能:Adobe证书是一种权威的认证,可以证明设计师在使用Adobe软件方面具有专业技能。这可以帮助设计师在求职过程中脱颖而出,提高竞争力。②提高工作效率:Adobe软件是设计师必备的工具,掌握这些工具的高效使用......
  • P.22-认证配置详解、P.23-权限系统的作用、P.24-授权基本流程
    P.22-认证配置详解在SecurityConfig下进行修改@ConfigurationpublicclassSecurityConfigextendsWebSecurityConfigurerAdapter{//创建BCryptPasswordEncoder注入容器@BeanpublicPasswordEncoderpasswordEncoder(){returnnewBCryptPasswordEn......
  • P.19-token认证过滤器代码实现、P.20-配置认证过滤器、P.21-退出登录
    P.19-token认证过滤器代码实现自定义一个过滤器,这个过滤器会去获取请求头中的token,对token进行解析取出其中的userid。使用userid去redis中获取对应的LoginUser对象。然后封装Authentication对象存入SecurityContextHolder@ComponentpublicclassJwtAuthentica......
  • JWT 实现登录认证 + Token 自动续期方案 转载
    过去这段时间主要负责了项目中的用户管理模块,用户管理模块会涉及到加密及认证流程,加密已经在前面的文章中介绍了。今天就来讲讲认证功能的技术选型及实现。技术上没啥难度当然也没啥挑战,但是对一个原先没写过认证功能的菜鸡甜来说也是一种锻炼吧。技术选型要实现认证功能,很容易......
  • SpringBoot 使用 Sa-Token 完成权限认证
    一、设计思路所谓权限认证,核心逻辑就是判断一个账号是否拥有指定权限:有,就让你通过。没有?那么禁止访问!深入到底层数据中,就是每个账号都会拥有一个权限码集合,框架来校验这个集合中是否包含指定的权限码。例如:当前账号拥有权限码集合["user-add","user-delete","user-get"]......
  • token认证过滤器代码实现与配置认证过滤器
    token认证过滤器代码实现认证过滤器​我们需要自定义一个过滤器,这个过滤器会去获取请求头中的token,对token进行解析取出其中的userid。​使用userid去redis中获取对应的LoginUser对象。​然后封装Authentication对象存入SecurityContextHolder......