SpringBoot Security权限控制自定义failureHandler实例
作者:EdurtIO 发布时间:2022-12-03 08:46:58
标签:SpringBoot,Security,failureHandler
创建hander文件夹
在 java 源码目录下创建hander文件夹, 在该文件夹下创建CustomAuthenticationFailHander类文件
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.edurt.hander;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* CustomAuthenticationFailHander <br/>
* 描述 : CustomAuthenticationFailHander <br/>
* 作者 : qianmoQ <br/>
* 版本 : 1.0 <br/>
* 创建时间 : 2018-03-20 下午4:08 <br/>
*/
@Component(value = "customAuthenticationFailHander")
public class CustomAuthenticationFailHander extends SimpleUrlAuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
System.out.println("登录失败!!!");
this.returnJson(response, exception);
}
/**
* 直接返回需要返回的 json 数据
*/
private void returnJson(HttpServletResponse response,
AuthenticationException exception) throws IOException {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
response.getWriter().println("{\"ok\":0,\"msg\":\"" + exception.getLocalizedMessage() + "\"}");
}
/**
* 直接返会错误页面
*/
private void returnErrorPage(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws IOException, ServletException {
String strUrl = request.getContextPath() + "/loginErrorPath";
request.getSession().setAttribute("status", 0);
request.getSession().setAttribute("message", exception.getLocalizedMessage());
request.getSession().setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, exception);
// 使用该方法会出现错误
// request.getRequestDispatcher(strUrl).forward(request, response);
response.sendRedirect(strUrl);
}
}
修改WebSecurityConfig配置
修改WebSecurityConfig配置文件支持自定义Handler
@Autowired
private CustomAuthenticationFailHander customAuthenticationFailHander;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
// 允许直接访问/路径
.authorizeRequests().antMatchers("/").permitAll()
// 使其支持跨域
.requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
// 其他路径需要授权访问
.anyRequest().authenticated()
// 指定登录页面
.and().formLogin().loginPage("/user/login")
// 指定登录失败跳转地址, 使用自定义错误信息
.failureHandler(customAuthenticationFailHander)
// 登录成功后的默认路径
.defaultSuccessUrl("/").permitAll()
// 退出登录后的默认路径
.and().logout().logoutSuccessUrl("/user/login").permitAll();
}
来源:https://juejin.cn/post/7164934051236118559


猜你喜欢
- strcpy函数详解如下1.函数介绍1.1.函数接口char * __cdecl strcpy(char * dst, const char
- 登录添加验证码是一个非常常见的需求,网上也有非常成熟的解决方案,其实,要是自己自定义登录实现这个并不难,但是如果需要在 Spring Sec
- 本文实例讲述了JAVA过滤标签实现将html内容转换为文本的方法。分享给大家供大家参考,具体如下:/*** 把html内容转为文本* @pa
- 五一期间原计划是写两篇文章,看一本技术类书籍,结果这五天由于自律性过于差,禁不住各种诱惑,我连电脑都没打开过,计划完美宣告失败。所以在这能看
- 1、运算符两边的变量为boolean变量时 先列出代码:public clas
- 成员类型访问权限低于字段本身现在假设你有一个小兵类,他的访问权限是仅限当前程序集。internal class 小兵{public int
- Maven 翻译为"专家"、"内行",是 Apache 下的一个纯 Java 开发的开源项目。基于项
- 1. 起源KV项目下载底层重构升级决定采用独立进程进行Media下载处理,以能做到模块复用之目的,因此涉及到了独立进程间的数据传递问题。目前
- 多点触摸技术在实际开发过程中,用的最多的就是放大缩小功能。比如有一些图片浏览器,就可以用多个手指在屏幕上操作,对图片进行放大或者缩小。再比如
- 本文实例为大家分享了unity实现场景切换进度条显示的具体代码,供大家参考,具体内容如下一、UI。建立slider适当更改即可;二、新增lo
- 前言RxJava 在 GitHub 主页上的自我介绍是 "a library for composing asynchronous
- 有了上一节中得到的正则表达式,那么就可以用来构造 NFA 了。NFA 可以很容易的从正则表达式转换而来,也有助于理解正则表达式表示的模式。一
- 前言以多个客户端和一个服务端的socket通信为例,服务端启动时创建一个固定大小的线程池。服务端每接收到一个连接请求后(通信任务),交给线程
- 本文实例为大家分享了Unity创建平铺网格地图的具体代码,供大家参考,具体内容如下创建预制件先拖进场景,再从层级拖回资源选中源图像文件,设置
- 什么是自动装箱和拆箱自动装箱就是Java自动将原始类型值转换成对应的对象,比如将int的变量转换成Integer对象,这个过程叫做装箱,反之
- 在android提供了一种类型:Parcel。被用作封装数据的容器,封装后的数据可以通过Intent或IPC传递。 除了基本类型以外,只有实
- 本文为大家分享了如何使用eclipse创建java项目,供大家参考,具体内容如下首先,打开Eclipse,在工具栏依次点击【File】>
- 本文实例为大家分享了C#绘制饼状图和柱状图的具体代码,供大家参考,具体内容如下#代码如下:using System;using System
- JavaBean根据指定条件设置属性值默认值使用场景当bean数据中已经装配好其他数据,在逻辑以及数据转换完成的最后一步进行数据默认值设置;
- 本文实例讲述了C#检查键盘大小写锁定状态的方法。分享给大家供大家参考。具体分析如下:1、命名空间:using System.Runtime.