SpringBoot Security权限控制自定义failureHandler实例

 更新时间:2022年11月13日 11:06:33   作者:EdurtIO  
这篇文章主要为大家介绍了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();
}

以上就是SpringBoot Security权限控制自定义failureHandler实例的详细内容,更多关于SpringBoot Security failureHandler的资料请关注脚本之家其它相关文章!

相关文章

  • MyBatis-Plus找不到Mapper.xml文件的几种解决方法

    MyBatis-Plus找不到Mapper.xml文件的几种解决方法

    mybatis-plus今天遇到一个问题,就是mybatis 没有读取到mapper.xml 文件,所以下面这篇文章主要给大家介绍了关于MyBatis-Plus找不到Mapper.xml文件的几种解决方法,需要的朋友可以参考下
    2022-06-06
  • Spring Boot 2.4版本前后的分组配置变化及对多环境配置结构的影响(推荐)

    Spring Boot 2.4版本前后的分组配置变化及对多环境配置结构的影响(推荐)

    这篇文章主要介绍了Spring Boot 2.4版本前后的分组配置变化及对多环境配置结构的影响,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • JAVA项目如何打包部署到Linux服务器上

    JAVA项目如何打包部署到Linux服务器上

    本文详细介绍了在服务器上部署环境包括JDK、MySQL、Tomcat的设置,以及使用Idea-Maven-SpringBoot进行jar包打包部署的流程,内容涵盖了MySQL配置注意事项、pom.xml配置、打包命令等关键步骤,同时,也提供了如何将jar包上传到Linux服务器并运行的具体方法
    2024-10-10
  • Mybatis动态sql超详细讲解

    Mybatis动态sql超详细讲解

    动态SQL是MyBatis的强大特性之一,顾名思义就是会动的SQL,即是能够灵活的根据某种条件拼接出完整的SQL语句,下面这篇文章主要给大家介绍了关于Mybatis动态sql的相关资料,需要的朋友可以参考下
    2023-04-04
  • Spring在SingleTon模式下的线程安全详解

    Spring在SingleTon模式下的线程安全详解

    这篇文章主要介绍了Spring在SingleTon模式下的线程安全详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Springboot2 session设置超时时间无效的解决

    Springboot2 session设置超时时间无效的解决

    这篇文章主要介绍了Springboot2 session设置超时时间无效的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Java使用POI-TL和JFreeChart动态生成Word报告

    Java使用POI-TL和JFreeChart动态生成Word报告

    本文介绍了使用POI-TL和JFreeChart生成包含动态数据和图表的Word报告的方法,并分享了实际开发中的踩坑经验,通过代码示例讲解的非常详细,具有一定的参考价值,需要的朋友可以参考下
    2025-02-02
  • Java servlet 使用 PrintWriter 时的编码与乱码的示例代码

    Java servlet 使用 PrintWriter 时的编码与乱码的示例代码

    本篇文章主要介绍了Java servlet 使用 PrintWriter 时的编码与乱码的示例代码,探讨了 PrintWriter 的缺省编码与普通字符流的缺省编码的差异,具有一定的参考价值,有兴趣的可以了解一下
    2017-11-11
  • 关于MyBatis Plus中使用or和and问题

    关于MyBatis Plus中使用or和and问题

    这篇文章主要介绍了关于MyBatis Plus中使用or和and问题,需要的朋友可以参考下
    2020-12-12
  • 用Java实现简单ATM机功能

    用Java实现简单ATM机功能

    这篇文章主要为大家详细介绍了用Java实现简单ATM机功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01

最新评论