2016-04-22 53 views
2

春季启动Web应用程序我有以下安全配置:春天开机 - 返回403禁止的,而不是重定向到登录页面

@Override 
public void configure(HttpSecurity http) throws Exception { 
    // @formatter:off 
    http 
     .headers().frameOptions().disable() 
     .and() 
      .antMatcher("/**").authorizeRequests() 
      .antMatchers("/actuator/health").permitAll() 
      .antMatchers("/actuator/**").hasAuthority(Authority.Type.ROLE_ADMIN.getName()) 
      .antMatchers("/login/**").permitAll() 
      .anyRequest().authenticated() 
     .and() 
      .formLogin() 
       .loginPage("/login") 
       .loginProcessingUrl("/login") 
       .failureUrl("/login?error").permitAll() 
     .and() 
      .logout() 
      .logoutRequestMatcher(new AntPathRequestMatcher("/logout")) 
      .logoutSuccessUrl("/login?logout") 
     .and() 
      .csrf().csrfTokenRepository(csrfTokenRepository()) 
     .and() 
      .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class) 
      .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class); 
    // @formatter:on 
} 

现在,当我尝试下面的URL访问,例如:/api/v1.0/user它会将我重定向到/api/login页面。

如何配置此为了返回403 Forbidden而不是重定向到登录页面?

+1

猜你需要写你的AuthenticationEntryPoint – Tong

+1

@Tong的感谢!我已将'.exceptionHandling()。authenticationEntryPoint(new Http403ForbiddenEntryPoint())'添加到我的配置中,现在它按预期工作 – alexanoid

回答

2

这是一个社区答:

问题:

故宫网址正在返回的登录页面内容(而不是403个状态码)。

我有这个代码:

... 
http.authorizeRequests().antMatchers("/uri/**").hasAuthority("SOME_ROLE"); 

改变与彤的建议:

... 
http.exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint()); 
http.authorizeRequests().antMatchers("/uri/**").hasAuthority("SOME_ROLE");