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项目实现动态表名的三种方法

    mybatis项目实现动态表名的三种方法

    有时在开发过程中java代码中的表名和数据库的表名并不是一致的,此时我们就需要动态的设置表名,本文主要介绍了mybatis项目实现动态表名的三种方法,具有一定的参考价值,感兴趣的可以了解一下
    2024-01-01
  • Java编程实现深度优先遍历与连通分量代码示例

    Java编程实现深度优先遍历与连通分量代码示例

    这篇文章主要介绍了Java编程实现深度优先遍历与连通分量代码示例,
    2017-11-11
  • Springboot与Maven多环境配置的解决方案

    Springboot与Maven多环境配置的解决方案

    多环境配置的解决方案有很多,我看到不少项目的多环境配置都是使用Maven来实现的,本文就实现Springboot与Maven多环境配置,感兴趣的可以了解下
    2021-06-06
  • SpringBoot自定义加载yml实现方式,附源码解读

    SpringBoot自定义加载yml实现方式,附源码解读

    这篇文章主要介绍了SpringBoot自定义加载yml实现方式附源码解读,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 如何在 Spring Boot 中配置和使用 CSRF 保护

    如何在 Spring Boot 中配置和使用 CSRF 保护

    CSRF是一种网络攻击,它利用已认证用户的身份来执行未经用户同意的操作,Spring Boot 提供了内置的 CSRF 保护机制,可以帮助您防止这种类型的攻击,这篇文章主要介绍了Spring Boot 中的 CSRF 保护配置的使用方法,需要的朋友可以参考下
    2023-09-09
  • 解决springboot configuration processor对maven子模块不起作用的问题

    解决springboot configuration processor对maven子模块不起作用的问题

    这篇文章主要介绍了解决springboot configuration processor对maven子模块不起作用的问题,本文通过图文实例代码给大家讲解的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • Java多线程定时器Timer原理及实现

    Java多线程定时器Timer原理及实现

    这篇文章主要介绍了Java多线程定时器Timer原理及实现,涉及Timer的schedule的使用,定时器Timer的schedule等相关内容以及代码示例,具有一定参考价值,需要的朋友可以了解下。
    2017-11-11
  • Java 中责任链模式实现的三种方式

    Java 中责任链模式实现的三种方式

    本文重点给大家介绍java中如何编写责任链模式。主要从下面3个框架中的代码中介绍。非常不错,需要的朋友参考下吧
    2017-09-09
  • 详解Java编程中对象的序列化

    详解Java编程中对象的序列化

    这篇文章主要介绍了Java编程中对象的序列化,包括一些反序列化的例子,需要的朋友可以参考下
    2015-11-11
  • IDEA 热部署设置(JRebel插件激活)

    IDEA 热部署设置(JRebel插件激活)

    这篇文章主要介绍了IDEA 热部署设置(JRebel插件激活),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-08-08

最新评论