Spring security中怎么自定义成功和失败
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,本篇文章为大家展示了Spring security中怎么自定义成功和失败,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。实现步骤1. 复制上一示例的源码重命名包
千家信息网最后更新 2026年11月02日Spring security中怎么自定义成功和失败实现步骤
本篇文章为大家展示了Spring security中怎么自定义成功和失败,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
实现步骤
1. 复制上一示例的源码
重命名包名 case3 为 case4
重命名 Case3Application.java 为 Case4Application.java
2. 在 WebSecurityConfig 中配置登录页
在 config(HttpSecurity http) 方法中对 formLogin 选项进行配置。需要包含以下设置:
创建 SuccessHandler 实现 AuthenticationSuccessHandler 接口,并实现 onAuthenticationSuccess 方法,自定义返回内容;
创建 FailureHandler 实现 AuthenticationFailureHandler 接口,并实现 onAuthenticationFailure 方法,自定义返回内容;
在 formLogin 配置项上增加 successHandler 和 failureHandler 配置
相关代码如下:
package net.txt100.learn.springsecurity.base.case4.config;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.http.HttpStatus;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.core.Authentication;import org.springframework.security.core.AuthenticationException;import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;import org.springframework.security.crypto.password.PasswordEncoder;import org.springframework.security.web.authentication.AuthenticationFailureHandler;import org.springframework.security.web.authentication.AuthenticationSuccessHandler;import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/** * Title: WebSecurityConfig * Package: net.txt100.learn.springsecurity.base.case2.config * Creation date: 2019-08-11 * Description: * * @author Tonglei * @since 1.0 */@Configurationpublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Bean public PasswordEncoder passwordEncoder() { // 配置密码的保护策略,spring security 默认使用 bcrypt 加密算法。 // 此处只要显式声明 BCryptPasswordEncoder Bean 即可 return new BCryptPasswordEncoder(); } @Override protected void configure(HttpSecurity http) throws Exception { AuthenticationSuccessHandler successHandler = new AuthenticationSuccessHandler() { @Override public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { response.setContentType("application/json;charset=UTF-8"); JSON.writeJSONString(response.getOutputStream(), authentication); } }; AuthenticationFailureHandler failureHandler = new AuthenticationFailureHandler() { @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value()); response.setContentType("application/json;charset=UTF-8"); JSON.writeJSONString(response.getOutputStream(), exception); } }; http .csrf().disable() // 关闭 CSRF 保护功能,否则不支持 Post 请求 .authorizeRequests() // 针对 HttpServletRequest 进行安全配置 .antMatchers("/login.html").permitAll() // login.html 页面无需登录即可访问 .anyRequest().authenticated() // 对所有 Request 均需安全认证 .and().formLogin() .successHandler(successHandler) .failureHandler(failureHandler) .and().httpBasic(); // 定义如何验证用户,此项代表弹出浏览器认证窗口 }}
上述内容就是Spring security中怎么自定义成功和失败,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
配置
内容
方法
成功
安全
技能
接口
知识
保护
登录
认证
简明
简明扼要
代码
代表
功能
安全认证
密码
就是
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
深圳市人人互联网科技有限公司
圆梦互联网科技
工业互联网科技新基建
互联网医疗科技
上海亿帐通互联网科技
博兴的互联网电子科技公司
随着互联网科技发展
互联网经济与金融科技
昊天互联网科技有限公司
新兴科技互联网创新大赛颁奖典礼
亳州好赞互联网科技有限公司
互联网金融科技展
996高科技互联网公司
国产互联网科技公司
互联网科技机房
互联网科技职业技能培训学校
北京军民互联网科技公司
互联网科技品牌
华人运通互联网科技公司
互联网科技自媒体灵感
互联网公司 科技公司
沃里互联网科技
天下捷融互联网科技
语蝶互联网科技有限公司
东莞新华互联网科技学校
半导体是互联网高科技吗
全球科技与互联网公司市值变化
互联网科技平台产品运营
互联网科技机房
山东蛋芒互联网科技有限公司