当前位置:网站首页>自定義登錄成功處理
自定義登錄成功處理
2022-04-21 23:15:00 【搞錢自律】
有時候頁面跳轉並不能滿足我們,特別是在前後端分離開發中就不需要成功之後跳轉頁面。只需要給前端返回一個JSON通知登錄成功還是失敗與否。這個試試可以通過自定義AuthenticationSuccessHandler實現
修改WebSecurityConfigurer
successHandler
package com.example.config;
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()) //認證成功時處理 前後端分離解决方案
.and()
.csrf().disable(); //禁止csrf 跨站請求保護
}
}
新增處理成功handler
package com.example.handler;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
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 MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
Map<String,Object> result = new HashMap<>();
result.put("msg","登錄成功");
result.put("status",200);
result.put("authentication",authentication);
response.setContentType("application/json;charset=UTF-8");
String s = new ObjectMapper().writeValueAsString(result);
response.getWriter().println(s);
}
}
啟動成功,測試

版权声明
本文为[搞錢自律]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212314421994.html
边栏推荐
- 6. Example of QT using MySQL
- Ros2 robot modeling URDF 8.1 URDF unified robot modeling language
- Basic concepts of audio and video and a simple introduction to ffmpeg
- 【openh264】SPS的 timing_info_present_flag
- golang力扣leetcode 386.字典序排数
- The pattern should be large and the vision should be broad, and the humanitarian spirit should be upheld [continuous updating, do not delete]
- MySQL pit father Guide_ sql_ Mode = "only_full_group_by"
- 早晚安打卡签到v2.0.1 公众号模块
- 痞子衡嵌入式:聊聊系统看门狗WDOG1在i.MXRT1xxx系统启动中的应用及影响
- 自定义模板问题求助,自动添加时间日期
猜你喜欢

Vs2019 configuring opencv4

Application of 12 principles of cdga agile development in enterprise data governance

音视频基本概念和FFmpeg的简单入门

8.4 control of wheel motion of mobile robot in rodf robot modeling

Its and LPI interrupt of GIC spec 5

GIC spec之ITS和LPI中断5

Teach you to easily solve CSRF Cross Site Request Forgery Attack

When the color contrast of beix3 is good, the color of the new version 3.1 is close to the original image
技术、产品、品牌都不是问题,对上汽奥迪而言,这2点或生死攸关

Comparison between member variables and local variables
随机推荐
【ACM】46. 全排列(1. 这里需要用到前面的元素进行排列,故不用startindex(组合、分割时才用);2. 注意处理数组中元素是否重复使用的问题(使用contains函数))
新独立版抖音口红机全修复版本附视频教程
痞子衡嵌入式:聊聊系统看门狗WDOG1在i.MXRT1xxx系统启动中的应用及影响
Hands on Robotics 7.3.2 hands on coordinate transformation homogeneous matrix generation (quaternion + Euler angle) homogeneous coordinate operation
【H.264】H.264 解析 工具、web解析
MySQL pit father Guide_ sql_ Mode = "only_full_group_by"
go-map
How does the applet integrate instant messaging with instant messaging
thinkphp开发卡密社区系统
7.3.1 homogeneous coordinate transformation & homogeneous transformation matrix
2022-04-21: given a blacklist containing non repeating integers in [0, n), write a function to return a random integer not in the blacklist from [0, n)
[H.264] H.264 parsing tool, web parsing
2022 electrician (elementary) examination simulated 100 questions and simulated examination
瑞芯微芯片AI部分开发记录 第二节 《yolov3-tiny及darknet介绍》
What is the core design of Dadi's performance advantage in the cache hit scenario?
2022电视盒子加购榜排名,超一多半的用户选购当贝盒子
The pattern should be large and the vision should be broad, and the humanitarian spirit should be upheld [continuous updating, do not delete]
8.3 create a mobile robot by hand in rodf robot modeling
[reprint] postman omysql connection database
APM industry awareness series - 12 - 13