2016-05-04 9 views
4

春のセキュリティで動作していない:私のカスタムページでカスタムHTTP 403ページには、私は、デフォルトのアクセス拒否ページ置き換えたい

HTTP 403

をし、私のアプローチは、このでした:

@Configuration 
@EnableWebSecurity 
public class SecurityContextConfigurer extends WebSecurityConfigurerAdapter { 

    @Autowired 
private UserDetailsService userDetailsService; 

@Override 
public void configure(WebSecurity web) throws Exception { 
    web.ignoring().antMatchers("/resources/**"); 
} 

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http.sessionManagement().maximumSessions(1) 
      .sessionRegistry(sessionRegistry()).expiredUrl("/"); 
    http.authorizeRequests().antMatchers("/").permitAll() 
      .antMatchers("/register").permitAll() 
      .antMatchers("/security/checkpoint/for/admin/**").hasRole("ADMIN") 
      .antMatchers("/rest/users/**").hasRole("ADMIN").anyRequest() 
      .authenticated().and().formLogin().loginPage("/") 
      .defaultSuccessUrl("/welcome").permitAll().and().logout() 
      .logoutUrl("/logout"); 
} 

@Bean 
public SessionRegistry sessionRegistry() { 
    return new SessionRegistryImpl(); 
} 

@Bean 
public AuthenticationProvider daoAuthenticationProvider() { 
    DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider(); 
    daoAuthenticationProvider.setUserDetailsService(userDetailsService); 

    return daoAuthenticationProvider; 

} 

@Bean 
public ProviderManager providerManager() { 

    List<AuthenticationProvider> arg0 = new CopyOnWriteArrayList<AuthenticationProvider>(); 
    arg0.add(daoAuthenticationProvider()); 

    return new ProviderManager(arg0); 

} 

@Bean(name = "myAuthenticationManagerBean") 
@Override 
public AuthenticationManager authenticationManagerBean() throws Exception { 
    return super.authenticationManagerBean(); 
} 

@Override 
protected AuthenticationManager authenticationManager() throws Exception { 
    return providerManager(); 
} 

    @Bean 
    public ExceptionTranslationFilter exceptionTranslationFilter() { 
     ExceptionTranslationFilter exceptionTranslationFilter = 
       new ExceptionTranslationFilter(new CustomAuthenticationEntryPoint()); 
     exceptionTranslationFilter.setAccessDeniedHandler(accessDeniedHandler()); 

     return exceptionTranslationFilter; 
    } 
    @Bean 
    public AccessDeniedHandlerImpl accessDeniedHandler() { 
     AccessDeniedHandlerImpl accessDeniedHandlerImpl = new 
       AccessDeniedHandlerImpl(); 
     accessDeniedHandlerImpl.setErrorPage("/page_403.jsp"); 
     System.out.println("ACCESS DENIED IS CALLED......"); 
     return accessDeniedHandlerImpl; 
    } 

    private class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint{ 

     @Override 
     public void commence(HttpServletRequest request, HttpServletResponse response, 
       AuthenticationException authenticationException) throws IOException, 
       ServletException { 

      response.sendError(HttpServletResponse.SC_FORBIDDEN, 
        "Access denied."); 
     } 

    } 

} 

しかしで上記の設定はまだ完了していません。

HTTP 403

この目的のために注入する必要のあるBeanがさらにありますか?

+1

これは、豆のカップル以外何も設定していないことをさらに明確に示しています。豆を加えるだけでは助けにならないし、複雑にすることも簡単です(回答とリファレンスガイドを参照)。 –

答えて

3

免責事項:これが唯一の解決策ではなく、作業1。 Custom 403 Page in Spring Security

この場合、私のアプローチは、あなたのSecurityContextが

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    http.sessionManagement().maximumSessions(1) 
      .sessionRegistry(sessionRegistry()).expiredUrl("/"); 
    http.authorizeRequests().antMatchers("/").permitAll() 
      .antMatchers("/register").permitAll() 
      .antMatchers("/security/checkpoint/for/admin/**").hasRole("ADMIN") 
      .antMatchers("/rest/users/**").hasRole("ADMIN").anyRequest() 
      .authenticated().and().formLogin().loginPage("/") 
      .defaultSuccessUrl("/welcome").permitAll().and().logout() 
      .logoutUrl("/logout").and() 
      .exceptionHandling().accessDeniedPage("/page_403");//this is what you have to do here to get job done. 
} 

参考に、このメソッドを追加している可能な限りシンプルになります。

1

@M。 Deinum氏は、Spring SecurityにこれらのBeanを組み込む方法を伝えなければならないと指摘しました。とにかく、あなたが達成しようとしている何のためにはるかに簡単な方法がある:

@Configuration 
@EnableWebSecurity 
public class SecurityContextConfigurer extends WebSecurityConfigurerAdapter { 
    // Rest omitted 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       // The usual stuff 
       .exceptionHandling() 
        .accessDeniedPage("/page_403.jsp") 
        .authenticationEntryPoint((request, response, authException) -> { 
         response.sendError(HttpServletResponse.SC_FORBIDDEN); 
        }); 
    } 
} 
関連する問題