广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot Security权限控制自定义failureHandler实例
  • 895
分享到

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

SpringBoot Security failureHandlerSpringBoot Security 2022-11-16 00:11:07 895人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录创建hander文件夹修改WEBSecurityConfig配置创建hander文件夹 在 java 源码目录下创建hander文件夹, 在该文件夹下创建CustomAuthen

创建hander文件夹

在 java 源码目录下创建hander文件夹, 在该文件夹下创建CustomAuthenticationFailHander类文件


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;

@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);
    }
    
    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的资料请关注编程网其它相关文章!

--结束END--

本文标题: SpringBoot Security权限控制自定义failureHandler实例

本文链接: https://www.lsjlt.com/news/172941.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作