在Spring Boot Web应用程序中,我有以下安全配置:
@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
}
现在,当我尝试访问例如以下网址:/api/v1.0/user
时,我会将我重定向到/api/login
页面。
如何配置此项以返回403 Forbidden
而不是重定向到登录页面?
答案 0 :(得分:4)
这是一个社区答案:
<强>问题:强>
禁止的网址返回登录页面内容(而不是403状态代码)。
我有这段代码:
...
http.authorizeRequests().antMatchers("/uri/**").hasAuthority("SOME_ROLE");
改变了Tong的建议:
...
http.exceptionHandling().authenticationEntryPoint(new Http403ForbiddenEntryPoint());
http.authorizeRequests().antMatchers("/uri/**").hasAuthority("SOME_ROLE");