2016-06-10 14 views
1

Ich habe ein Problem im Umgang mit 403 verbotenes Problem im Springboot. Wie ich es von meiner Klasse behandelt habe erweitert WebSecurityConfiguratorAdapter um anzupassen. Es gibt mir als Ausgang verboten. Es sollte auf 403 URL umleiten, aber es funktioniert nicht. Ich bin Anfänger und weiß nicht, wo es falsch ist.Wie behandelt man 403 verbotenen Fehler in Spring Boot Annotation?

public class WebAppInitializer implements WebApplicationInitializer { 
    @Override 
    public void onStartup(ServletContext servletContext) 
      throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 

     ctx.register(SecurityConfiguration.class); 
     ctx.setServletContext(servletContext); 
     //ctx.register(SecurityConfiguration.class); 
     DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx); 
     dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); 


     Dynamic dynamic = servletContext.addServlet("dispatcher", dispatcherServlet); 
     dynamic.addMapping("/data/*"); 

     dynamic.setLoadOnStartup(1); 
    } 
} 

und My AppConfig Klasse

package com.portal.spring.config; 

import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.web.servlet.config.annotation.EnableWebMvc; 
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 

@Configuration 
@ComponentScan("com.portal") 
@EnableWebMvc 
public class AppConfig extends WebMvcConfigurerAdapter { 
} 

und Sicherheitskonfigurations

package com.portal.spring.config; 
import java.util.logging.Logger; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 

@Configuration 
@EnableWebSecurity 
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 
    private static final Logger log= Logger.getLogger(SecurityConfiguration.class.getName()); 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.exceptionHandling().accessDeniedHandler(new AccessDenyHandler()); 
     } 
} 

und accessdenyhandler

package com.portal.spring.config; 
import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import org.springframework.security.access.AccessDeniedException; 
import org.springframework.security.web.access.AccessDeniedHandler; 

public class AccessDenyHandler implements AccessDeniedHandler { 

    @Override 
    public void handle(HttpServletRequest request, HttpServletResponse response, 
      AccessDeniedException arg2) throws IOException, ServletException { 
     response.sendRedirect("//403"); 
    } 
} 
+0

Bitte beschreiben Sie mehr. Warum denkst du, dass es nicht funktioniert –

+0

@SangramJadhav ... Ich weiß nicht, dass es keine Ausnahme gibt ... Ist meine Art und Weise richtig zu behandeln 403 verbotenes Problem –

Antwort

0

Hier ist mein AccessDenied Handler. Ich habe explizit Spring SpringDeniedHandler-Implementierung delegiert, aber ich habe einige CSRF-bezogene Sachen, die ich behandeln muss. Dieses Stück ist nicht im untenstehenden Code enthalten, da es app-spezifisch ist. Anderer Code wie SecurityConfig ist ähnlich dem, was ich verwendet habe

public class MyAccessDeniedHandler implements AccessDeniedHandler { 

    private AccessDeniedHandlerImpl accessDeniedHandlerImpl = new AccessDeniedHandlerImpl(); 

    public void handle(HttpServletRequest request, HttpServletResponse response, 
      AccessDeniedException accessDeniedException) throws IOException, ServletException { 

     //Some CSRF related code 

     // Then call accessDeniedHandlerImpl.handle to handle request 
     accessDeniedHandlerImpl.handle(request, response, accessDeniedException); 
    } 

    /** 
    * The error page to use. Must begin with a "/" and is interpreted relative to the current context root. 
    * 
    * @param errorPage the dispatcher path to display 
    * 
    * @throws IllegalArgumentException if the argument doesn't comply with the above limitations 
    * @see AccessDeniedHandlerImpl#setErrorPage(String) 
    */ 
    public void setErrorPage(String errorPage) { 
     // You can set custom error page here 
     accessDeniedHandlerImpl.setErrorPage(errorPage); 
    } 
} 
+0

Danke für Ihre Unterstützung.Aber Sie haben das Beispiel ausgeführt ... Sogar schon es gibt mir als HTML-Ausgabe verboten..ich möchte es nur anpassen –