How to handle HTTP 403 with Spring Security 3.0.x

前端 未结 5 1338
温柔的废话
温柔的废话 2021-02-04 09:44

I\'m facing a little issue with Spring Security 3.0.x (3.0.2 in particular at the moment). The whole application I\'m working on is working perfectly except when someone who doe

5条回答
  •  逝去的感伤
    2021-02-04 10:12

    I still don't get why you had to implement your own access handler... I have currently faced same task:

      - works like charm.
    

    Don't forget to specify handler in your Controller:

     @RequestMapping(value = "/accessDenied")
          public String accessDenied() {
    
                return "accessDenied"; // logical view name
           }
    

    Update for Spring Boot(2014 Oct):

    @Configuration
    @EnableWebSecurity
    public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.exceptionHandling().accessDeniedHandler(customHandler) OR .accessDeniedPage("/somePage.html").and
                .formLogin()
                    .failureHandler(ajaxAuthenticationFailureHandler)} 
    

    Nowadays we don't really return views for such task since angular js kicks in so you can use your failure/success handler and return tailored JSON responses. For us it was sufficient to use failure handler but you get to choose where you want your control to kick in. We generally don't use view resolvers as there are UI tiles frameworks(such as angular partials) able to construct pieces into single page for you. Html pieces are stored on the server and served simply as static resources.

    Lets play with Embedded Tomcat to achieve similar behavior to web.xml !

    @Configuration
    @EnableAutoConfiguration
    public class ApplicationWebXml extends SpringBootServletInitializer {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.profiles(addDefaultProfile())
                .showBanner(false)
                .sources(Application.class);
    }
    
    //required for container customizer to work, the numerous tutorials didn't work for me, so I simply tried overriding the default one
    @Bean
    public EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        return tomcat;
    }
    
    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer(
    
    ) {
        return new EmbeddedServletContainerCustomizer() {
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                TomcatEmbeddedServletContainerFactory containerFactory = (TomcatEmbeddedServletContainerFactory) container;
                 containerFactory.setSessionTimeout(1); // just for your interest, remove as necessary
    
                containerFactory.addErrorPages(new ErrorPage(HttpStatus.FORBIDDEN,"/views/accessDenied.html"),
                        new ErrorPage(HttpStatus.NOT_FOUND,"/views/notFound.html"));
                containerFactory.addConnectorCustomizers(new TomcatConnectorCustomizer() {
                    @Override
                    public void customize(Connector connector) {
                        connector.setPort(8082);// just for your interest, remove as necessary
                    }
                });
            }
        };
    }
    

    }

提交回复
热议问题