当前位置:网站首页>自定义登录失败处理
自定义登录失败处理
2022-04-23 09:46:00 【搞钱自律】
新增自定义登录失败handler
package com.example.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/** * 自定义认证失败处理 */
public class MyAuthenticationFailureHandler implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
Map<String,Object> result = new HashMap<>();
result.put("msg","登录失败"+exception.getMessage());
result.put("status",500);
response.setContentType("application/json;charset=UTF-8");
String s = new ObjectMapper().writeValueAsString(result);
response.getWriter().println(s);
}
}
修改WebSecurityConfigurer
主要是failureHandler
package com.example.config;
import com.example.handler.MyAuthenticationFailureHandler;
import com.example.handler.MyAuthenticationSuccessHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
//【注意事项】放行资源要放在前面,认证的放在后面
http.authorizeRequests()
.mvcMatchers("/index").permitAll() //代表放行index的所有请求
.mvcMatchers("/loginHtml").permitAll() //放行loginHtml请求
.anyRequest().authenticated()//代表其他请求需要认证
.and()
.formLogin()//表示其他需要认证的请求通过表单认证
//loginPage 一旦你自定义了这个登录页面,那你必须要明确告诉SpringSecurity日后哪个url处理你的登录请求
.loginPage("/loginHtml")//用来指定自定义登录界面,不使用SpringSecurity默认登录界面 注意:一旦自定义登录页面,必须指定登录url
//loginProcessingUrl 这个doLogin请求本身是没有的,因为我们只需要明确告诉SpringSecurity,日后只要前端发起的是一个doLogin这样的请求,
//那SpringSecurity应该把你username和password给捕获到
.loginProcessingUrl("/doLogin")//指定处理登录的请求url
.usernameParameter("uname") //指定登录界面用户名文本框的name值,如果没有指定,默认属性名必须为username
.passwordParameter("passwd")//指定登录界面密码密码框的name值,如果没有指定,默认属性名必须为password
// .successForwardUrl("/index")//认证成功 forward 跳转路径,forward代表服务器内部的跳转之后,地址栏不变 始终在认证成功之后跳转到指定请求
// .defaultSuccessUrl("/index")//认证成功 之后跳转,重定向 redirect 跳转后,地址会发生改变 根据上一保存请求进行成功跳转
.successHandler(new MyAuthenticationSuccessHandler()) //认证成功时处理 前后端分离解决方案
// .failureForwardUrl("/loginHtml")//认证失败之后 forward 跳转
// .failureUrl("/login.html")//认证失败之后 redirect 跳转
.failureHandler(new MyAuthenticationFailureHandler())//用来自定义认证失败之后处理 前后端分离解决方案
.and()
.csrf().disable(); //禁止csrf 跨站请求保护
}
}

版权声明
本文为[搞钱自律]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43472934/article/details/124335228
边栏推荐
- MacOS下使用CLion编译调试MySQL8.x
- MySQL of database -- overview and installation
- PHP笔记(一):开发环境配置
- 3、 6 [Verilog HDL] gate level modeling of basic knowledge
- 理解作用域
- Flink 流批一体在小米的实践
- 防疫登记小程序
- Alibaba cloud architects interpret the four mainstream game architectures
- [codeforces - 208e] blood cousins
- P1446 [hnoi2008] cards (Burnside theorem + DP count)
猜你喜欢

Nvidia最新三维重建技术Instant-ngp初探

SAP salv14 background output salv data can directly save files and send emails (with sorting, hyperlink and filtering format)

The sap export excel file opens and shows that the file format and extension of "XXX" do not match. The file may be damaged or unsafe. Do not open it unless you trust its source. Do you still want to

Go language learning notes - structure | go language from scratch

Alibaba cloud architects interpret the four mainstream game architectures

Leetcode题库78. 子集(递归 c实现)

ABAP CDs view with association example

ABAP 7.4 SQL Window Expression

Leetcode0587. Install fence

SAP ECC connecting SAP pi system configuration
随机推荐
高薪程序员&面试题精讲系列91之Limit 20000加载很慢怎么解决?如何定位慢SQL?
Less than 100 secrets about prime numbers
Set the maximum width of the body, but why does the background color of the body cover the whole page?
MySQL - Chapter 1 (data types in MySQL)
Single sign on SSO
Redis 异常 read error on connection 解决方案
[COCI] lattice (dichotomy + tree divide and conquer + string hash)
Number theory blocking (integer division blocking)
NLLLoss+log_ SoftMax=CE_ Loss
亚马逊云科技入门资源中心,从0到1轻松上云
Comparison of overloading, rewriting and hiding
Creation of raid0 and RAID5 and Simulation of how RAID5 works
1 + X cloud computing intermediate -- script construction, read-write separation
Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem
SAP debug debug for in, reduce and other complex statements
JS case to find the maximum value, reverse the array, bubble sort
Code source daily question div1 (701-707)
Leetcode0587. Install fence
KVM installation and deployment
SQL used query statements