Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导:accessDeniedHandler不工作_Java_Spring_Spring Security_Spring Boot - Fatal编程技术网

Java Spring引导:accessDeniedHandler不工作

Java Spring引导:accessDeniedHandler不工作,java,spring,spring-security,spring-boot,Java,Spring,Spring Security,Spring Boot,我有以下Spring安全配置: @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().antMatchers("/a

我有以下Spring安全配置:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/api/private/**", "/app/**").authenticated();
        http.csrf().disable();
        http.logout().logoutSuccessUrl("/");
        http.exceptionHandling().accessDeniedPage("/403"); //.accessDeniedHandler(accessDeniedHandler);
    }
}
我期望以下逻辑:未经身份验证的用户将被重定向到
/403
。而不是Spring显示默认的Tomcat403页面。我还尝试了custom
accessDeniedHandler
,但没有成功


如何在访问失败时实现自定义逻辑?

AccessDeniedHandler仅适用于经过身份验证的用户。未经身份验证的用户的默认行为是重定向到登录页面(或任何适用于正在使用的身份验证机制的内容)

如果要更改,则需要配置
AuthenticationEntryPoint
,未经身份验证的用户尝试访问受保护的资源时会调用该点。你应该能够使用

http.exceptionHandling().authenticationEntryPoint(...)

而不是你所拥有的。有关更多详细信息,请查看。

正常的Spring安全行为是将未经身份验证的用户重定向到您的登录页面,如下所示。验证未经授权(不具有管理员角色)的用户将被定向到“拒绝访问”页面:

http.authorizeRequests().antMatchers("/admin/**")
    .access("hasRole('ADMIN')")
    .and().formLogin().loginPage("/login")
    .and().exceptionHandling().accessDeniedPage("/403");
如果您已经实现了自己的身份验证机制,并且不指望Spring安全配置将未经身份验证的用户交付到您的登录页面,那么您可以按如下方式玩Spring安全配置-为您的自定义403页面而不是真正的登录页面服务:

http.authorizeRequests().antMatchers("/admin/**")
    .access("hasRole('ADMIN')")
    .and().formLogin().loginPage("/403")
    .and().exceptionHandling().accessDeniedPage("/403");

遇到这个问题,它帮助我解决了我的问题,下面是我的代码:

public class CustomHttp403ForbiddenEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
            AuthenticationException authException) throws IOException, ServletException {
        response.getWriter().print("You need to login first in order to perform this action.");
    }

}


希望这能有所帮助。

使用以下方法:-

@Configuration
public class ViewRegistryConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/403").setViewName("notAuth");
    }

}

然后在模板文件夹中创建notAuth.html页面

这样就行了。但是,如果它是一个api,最好使用
response.senderro(HttpServletResponse.SC_禁止,“拒绝访问”)救生答案。谢谢
@Override
protected void configure(HttpSecurity http) throws Exception {

    http.exceptionHandling().accessDeniedHandler(new CustomAccessDeniedHandler()).and()
        .exceptionHandling().authenticationEntryPoint(new CustomHttp403ForbiddenEntryPoint());
}
@Configuration
public class ViewRegistryConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/403").setViewName("notAuth");
    }

}