国网电子商务平台官网/seo是怎么优化的
文章目录
- SpringSecurity 返回json
- 一、登录成功处理器
- 1.1 统一响应类HttpResult
- 1.2 登录成功处理器
- 1.3 配置登录成功处理器
- 1.4 登录
- 二、登录失败处理器
- 2.1 登录失败处理器
- 2.2 配置登录失败处理器
- 2.3 登录
- 三、退出成功处理器
- 3.1 退出成功处理器
- 3.2 配置退出成功处理器
- 3.3 退出
- 四、访问拒绝(无权限)处理器
- 4.1 访问拒绝处理器
- 4.2 配置访问拒绝处理器
- 4.3 被拒绝
- 五、自定义处理器
SpringSecurity 返回json
承接:1.SpringSecurity -快速入门、加密、基础授权-CSDN博客
一、登录成功处理器
前后端分离成为企业应用开发中的主流,前后端分离通过json进行交互,登录成功和失败后不用页面跳转,而是一段json提示
1.1 统一响应类HttpResult
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class HttpResult {private Integer code;private String msg;private Object data;public HttpResult(Integer code, String msg) {this.code = code;this.msg = msg;}
}
1.2 登录成功处理器
/*** 认证成功就会调用该接口里的方法*/
@Component
public class AppAuthenticationSuccessHandle implements AuthenticationSuccessHandler {// JSON序列化器,进行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// 定义返回对象httpResultHttpResult httpResult = HttpResult.builder().code(200).msg("登陆成功").build();String strResponse = objectMapper.writeValueAsString(httpResult);// 响应字符集response.setCharacterEncoding("UTF-8");
// 响应内容类型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
// 响应给前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}
1.3 配置登录成功处理器
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Resourceprivate AppAuthenticationSuccessHandle appAuthenticationSuccessHandle;@Overrideprotected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授权http请求.anyRequest() //任何请求.authenticated();//都需要认证http.formLogin().successHandler(appAuthenticationSuccessHandle) //认证成功处理器.permitAll();//允许表单登录}}
1.4 登录
登录成功后如下所示
二、登录失败处理器
2.1 登录失败处理器
/*** 认证失败就会调用下面的方法*/
@Component
public class AppAuthenticationFailHandle implements AuthenticationFailureHandler {// JSON序列化器,进行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {// 定义返回对象httpResultHttpResult httpResult = HttpResult.builder().code(401).msg("登录失败").build();String strResponse = objectMapper.writeValueAsString(httpResult);// 响应字符集response.setCharacterEncoding("UTF-8");
// 响应内容类型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
// 响应给前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}
2.2 配置登录失败处理器
@Resource
private AppAuthenticationFailHandle appAuthenticationFailHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授权http请求.anyRequest() //任何请求.authenticated();//都需要认证http.formLogin().successHandler(appAuthenticationSuccessHandle) //认证成功处理器.failureHandler(appAuthenticationFailHandle) // 认证失败处理器.permitAll();//允许表单登录
}
2.3 登录
输入一个错误的密码
如下图所示
三、退出成功处理器
3.1 退出成功处理器
/*** 退出成功处理器*/
@Component
public class AppLogoutSuccessHandle implements LogoutSuccessHandler{// JSON序列化器,进行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
// 定义返回对象httpResultHttpResult httpResult = HttpResult.builder().code(200).msg("退出成功").build();String strResponse = objectMapper.writeValueAsString(httpResult);// 响应字符集response.setCharacterEncoding("UTF-8");
// 响应内容类型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
// 响应给前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}
3.2 配置退出成功处理器
@Resource
private AppLogoutSuccessHandle appLogoutSuccessHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授权http请求.anyRequest() //任何请求.authenticated();//都需要认证http.formLogin().successHandler(appAuthenticationSuccessHandle) //认证成功处理器.failureHandler(appAuthenticationFailHandle) // 认证失败处理器.permitAll();//允许表单登录http.logout().logoutSuccessHandler(appLogoutSuccessHandle);//登录成功处理器
}
3.3 退出
四、访问拒绝(无权限)处理器
4.1 访问拒绝处理器
@Component
public class AppAccessDenyHandle implements AccessDeniedHandler {// JSON序列化器,进行序列化和反序列化@Resourceprivate ObjectMapper objectMapper;;@Overridepublic void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {// 定义返回对象httpResultHttpResult httpResult = HttpResult.builder().code(403).msg("您没有权限访问该资源!!").build();String strResponse = objectMapper.writeValueAsString(httpResult);// 响应字符集response.setCharacterEncoding("UTF-8");
// 响应内容类型JSON,字符集utf-8response.setContentType("application/json;charset=utf-8");
// 响应给前端PrintWriter writer = response.getWriter();writer.println(strResponse);writer.flush();}
}
4.2 配置访问拒绝处理器
@Resource
private AppAccessDenyHandle appAccessDenyHandle;@Override
protected void configure(HttpSecurity http) throws Exception {http.authorizeRequests()//授权http请求.anyRequest() //任何请求.authenticated();//都需要认证http.formLogin().successHandler(appAuthenticationSuccessHandle) //认证成功处理器.failureHandler(appAuthenticationFailHandle) // 认证失败处理器.permitAll();//允许表单登录http.logout().logoutSuccessHandler(appLogoutSuccessHandle);//登录成功处理器;http.exceptionHandling()//异常处理.accessDeniedHandler(appAccessDenyHandle);//访问被拒绝处理器
}
4.3 被拒绝
五、自定义处理器
SpringSecurity - 认证与授权、自定义失败处理、跨域问题、认证成功/失败处理器_我爱布朗熊的博客-CSDN博客
相关文章:

2.SpringSecurity - 处理器简单说明
文章目录 SpringSecurity 返回json一、登录成功处理器1.1 统一响应类HttpResult1.2 登录成功处理器1.3 配置登录成功处理器1.4 登录 二、登录失败处理器2.1 登录失败处理器2.2 配置登录失败处理器2.3 登录 三、退出成功处理器3.1 退出成功处理器3.2 配置退出成功处理器3.3 退出…...

AGI热门方向:国内前五!AI智能体TARS-RPA-Agent落地,实在智能打造人手一个智能助理
早在 1950 年代,Alan Turing 就将「智能」的概念扩展到了人工实体,并提出了著名的图灵测试。这些人工智能实体通常被称为 —— 代理(Agent)。 代理这一概念起源于哲学,描述了一种拥有欲望、信念、意图以及采取行动能力…...

运动品牌如何做到“全都要”?来看看安踏的答案
文 | 螳螂观察 作者 | 易不二 运动鞋服是兼具高景气和清晰格局的优质消费赛道。 中信证券给出的这一预测,欧睿国际也做出了更具体的测算:预计到2027年,中国运动服饰市场规模有望以约为8.7%的年复合增长率,突破5500亿元人民币。…...

LeetCode75——Day6
文章目录 一、题目二、题解 一、题目 151. Reverse Words in a String Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The words in s will be separated by at least one space. Return a string …...

http代理有什么好处,怎么通过http代理服务安全上网呢?
通过http代理上网是一种常见的网络代理方式。http代理是指通过代理服务器进行网络连接,以实现隐藏自己的真实IP地址、保护个人隐私等目的。下面我们将介绍通过http代理上网的好处以及如何使用http代理服务来安全上网。 一、通过http代理上网的好处 1. 保护个人隐私 …...

vue3后台管理框架之axios二次封装
在开发项目的时候避免不了与后端进行交互,因此我们需要使用axios插件实现发送网络请求。在开发项目的时候 我们经常会把axios进行二次封装。 目的: 1:使用请求拦截器,可以在请求拦截器中处理一些业务(开始进度条、请求头携带公共参数) 2:使用响应拦截器…...

你的Github账户可能被封禁!教你应对Github最新的2FA二次验证! 无地区限制, 无额外设备的全网最完美方案
1 2FA 的定义 双因素身份验证 (2FA) 是一种身份和访管理安全方法,需要经过两种形式的身份验证才能访河资源和数据,2FA使企业能够监视和帮助保护其最易受攻击的信息和网络。 2 2FA 的身份验证方法 使用双因素身份验证时有不同的身份验证方法。此处列出…...

【C语言】#define宏与函数的优劣对比
本篇文章目录 1. 预处理指令#define宏2. #define定义标识符或宏,要不要最后加上分号?3.宏的参数替换后产生的运算符优先级问题3.1 问题产生3.2 不太完美的解决办法3.3 完美的解决办法 4.#define的替换规则5. 有副作用的宏参数6. 宏与函数的优劣对比6.1 宏…...

flask基础开发知识学习
之前做了一些LLM的demo,接口用flask写的,但是涉及到后端的一些业务就感觉逻辑写的很乱,代码变成屎山,于是借助官方文档和GPT迅速补了一些知识,总结一下一个很小的模板 于是决定边学边重构之前的代码… 文章目录 代码结…...

内网和热点同时连接使用配置
解决如标题问题 查看当前永久路由信息 route print截图保存(重要) 截图保存(重要)查出来的永久路由,以防配置不成功时回退,回退方法就是下面的“添加永久路由” 删除当前的路由 0.0.0.0 是上面查出的网络地址 route delete 0.0.0.0内网IP信息 添加永久…...

C语言 形参、实参
定义 形参 形式上的参数,没有确定的值 实参 实际存在的,已经确定的参数,常量,变量,表达式,都是实参 区别 实参的值不随形参的变化而变化 在C语言中,数据传送是单向的,即只能把实…...

linux入门到精通-第四章-gcc编译器
目录 参考gcc概述gcc的工作流程 参考 gcc编译器 gcc概述 编辑器vi、记事本)是指我用它来写程序的 (编辑码),而我们写的代码语句,电脑是不懂的,我们需要把它转成电脑能懂的语句,编译器就是这样的转化工具。就是说,我…...

HCIP静态路由综合实验
题目: 步骤: 第一步:搭建上图所示拓扑; 第二步:为路由器接口配置IP地址; R1: [R1]display current-configuration intinterface GigabitEthernet0/0/0ip address 192.168.1.1 255.255.255.252 interfa…...

nginx前端配置(新)
基础配置 server {listen 80;server_name your-frontend-domain.com;# 根目录为前端网页文件所在目录root /path/to/your/frontend/files;# 默认文档(例如 index.html)index index.html;location / {try_files $uri $uri/ /index.html; #try_files 指…...

js,jquery,vue设置html标签隐藏不显示
前端 <p id"myElement"> </p>使用js将idmyElemnt的标签隐藏 使用 style.display 属性: 通过设置 style.display 属性为 "none",可以隐藏标签。 var element document.getElementById("myElement");element…...

口袋参谋:如何实时监控对手数据?
在如此激烈的淘宝天猫上开店,如何才能获取对手的数据呢? 俗话说的好,知己知彼百战百胜,那么这句话同样也适用于淘宝天猫上。 只有掌握对手推广策略以及数据,我们才有机会反超,因此做好竞品监控是运营店…...

Q-learning如何与ABC等一些元启发式算法能够结合在一起?
1、出现的问题 Q-learning能和元启发式算法(如ABC、PSO、GA、SSA等)结合在一起,实现工作流调度问题? Q-learning和ABC (Artificial Bee Colony) 等元启发式算法可以结合在一起以解决特定类型的问题。Q-learning是一种强化学习算法…...

mysql 过滤多列重复的值(保留其中一条),对单列或者多列重复的值去重
建立测试数据表 CREATE TABLE test (id int(11) NOT NULL AUTO_INCREMENT,account varchar(255) DEFAULT NULL,password varchar(255) DEFAULT NULL,deviceId varchar(255) DEFAULT NULL,PRIMARY KEY (id) ) ENGINEInnoDB AUTO_INCREMENT11 DEFAULT CHARSETutf8mb4;INSERT INT…...

面向红队的自动化引擎工具
gogo 介绍 面向红队的、高度可控的可拓展的自动化引擎。特征如下: 自由的端口配置 支持主动/主动指纹识别 关键信息提取,如标题、证书以及自定义提取信息的正则 支持nuclei poc,poc目录:https://chainreactors.github.io/wiki/…...

Python库学习(十):Matplotlib绘画库
1. 介绍 Matplotlib 是一个用于绘制图表和可视化数据的 Python 库。它提供了丰富的绘图工具,可以用于生成各种静态、交互式和动画图表。Matplotlib 是数据科学、机器学习和科学计算领域中最流行的绘图库之一。 1.1 关键特性 以下是 Matplotlib 的一些关键特性&…...

coverity工具 代码审计
第39篇:Coverity代码审计/代码扫描工具的使用教程_希潭实验室ABC123的博客-CSDN博客...

女鹅冬天的第一件羽绒服,当然要时尚经典的
90白鸭绒,高密度充绒量和蓬松度 让这件羽绒服更加饱满更有型 三防工艺,立领连帽设计 下摆抽绳,帽子上的魔术贴设计 无一不将保暖落实在实处 宽松版型立体感很强,对身材的包容性也是非常不错...

智慧渔业方案:AI渔政视频智能监管平台助力水域禁渔执法
一、方案背景 国内有很多水库及河流设立了禁渔期,加强渔政执法监管对保障国家渔业权益、维护渔业生产秩序、保护渔民群众生命财产安全、推进水域生态文明建设具有重要意义。目前,部分地区的监管手段信息化水平低下,存在人员少、职责多、任务…...

LXC、Docker、 Kubernetes 容器以及Hypervisor的区别
LXC、Docker、 Kubernetes 容器以及Hypervisor的区别 SaaS: Software-as-a-Service(软件即服务) PaaS: Platform-as-a-Service(平台即服务) IaaS: Infrastructure-as-a-Service(基础设施即服务) 1、Docke…...

电子杂志制作不求人:简单易用的工具推荐
如果你想要制作一份精美的电子杂志,但是又不想花费太多的时间和金钱,也不想求及朋友帮忙制作,那么可以试试这个网站制作电子杂志,展现出的效果跟专业级设计师的效果没什么区别哦 赶快收藏吧-------FLBOOK在线制作电子杂志平台&a…...

Excel冻结窗格
1、冻结表格首行 点击菜单栏中的“视图”,选择“窗口”选项卡中的“冻结窗格”下的小三角,再选择“冻结首行”; 2.冻结表格首列 点击菜单栏中的“视图”,选择“窗口”选项卡中的“冻结窗格”下的小三角,再选择“冻结…...

Flink自定义sink并支持insert overwrite 功能
前言 自定义flink sink,批模式下,有insert overwrite 需求或需要启动任务或任务完成后时,只执行一次某些操作时,则可参考此文章 组件: flink: 1.15 参考文档:https://nightlies.apache.org/flink/flink-docs-release-1.15/docs/dev/table/sourcessinks/ 分析 inser…...

CSS Vue/RN 背景使用opacity,文字在背景上显示
Vue <div class"training_project_tip"> <div class"tip">展示的文字</div> </div> .training_project_tip { font-size: 12px; font-weight: 400; text-align: left; color: #ffffff; margin-top: 8px; position: relative; dis…...

vue3自定义指令批量注册
第一步、在src目录下新建directives文件文件夹 用来存放不同的指令,以dbounce指令为例: 第二步、在directives目录下创建debounce.js文件,文件内容如下: // 防抖 const debounceClick {mounted(el, binding) {let timerel.addE…...

山西电力市场日前价格预测【2023-10-18】
日前价格预测 预测说明: 如上图所示,预测明日(2023-10-18)山西电力市场全天平均日前电价为348.72元/MWh。其中,最高日前电价为505.50元/MWh,预计出现在18: 00。最低日前电价为288.10元/MWh,预计…...