zoukankan      html  css  js  c++  java
  • spring security +spring boot 自定义 403 页面

    用的spring security  做的权限控制, 当  访问没有权限, 跳转 会跳到默认403 页面。不符合当前项目需求。

    一下是解决方式:

    package com.ycmedia;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
    import org.springframework.boot.context.embedded.ErrorPage;
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.HttpStatus;
    import org.springframework.security.core.userdetails.UserDetailsService;
    import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    import com.ycmedia.service.UserService;
    
    @SpringBootApplication
    @EnableAutoConfiguration
    public class Application extends WebMvcConfigurerAdapter {
    
        @Bean
        public UserDetailsService userDetailsService() {
            return new UserService();
        }
        /**
         * 自定义异常页
         */
        @Bean
        public EmbeddedServletContainerCustomizer containerCustomizer() {
    
           return (container -> {
                ErrorPage error401Page = new ErrorPage(HttpStatus.FORBIDDEN, "/403.html");
                ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
                ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");
                container.addErrorPages(error401Page, error404Page, error500Page);
           });
        }
    
        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
        }
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    看到网上什么实现AccessDeniedHandler  ,好像不好用。

  • 相关阅读:
    FastAdmin 提示框 toastr 改变文字
    FastAdmin 将会员模块升级为基础模块的升级指导
    随笔
    随笔
    c++11 lambda(了解)
    c++11 类型推断
    boost::archive::text_oarchive
    std::ostringstream
    随笔
    随笔1
  • 原文地址:https://www.cnblogs.com/zgghb/p/5823132.html
Copyright © 2011-2022 走看看