通过java代码配置弹簧安全性的自定义403错误页面 [英] custom 403 error page with spring security configured via java code

查看:164
本文介绍了通过java代码配置弹簧安全性的自定义403错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道如何在spring security中配置自定义的403页面?在Web上看,我得到的所有结果都是XML配置,我正在使用Java配置。这是我的SecurityConfig:

  @Configuration 
@ComponentScan(value =com)
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Bean
@Override
public AuthenticationManager authenticationManagerBean()throws Exception {
返回新的CustomAuthenticationManager();
}

protected void configure(HttpSecurity http)throws Exception {
http
.csrf()
.disable()
.authorizeRequests ()
.antMatchers(/ resources / **,/ publico / **)。permitAll()
.anyRequest()。authenticated()
.and()
.formLogin()
.loginPage(/ acesso / login)。permitAll()
.loginProcessingUrl(/ login)。permitAll()
.usernameParameter(login )
.passwordParameter(senha)
.successHandler(new CustomAuthenticationSuccessHandler())
.failureHandler(new CustomAuthenticationFailureHandler())
.and()
。 logout()
.logoutUrl(/ logout)
.logoutSuccessUrl(/ acesso / login)。permitAll();
}

}

我有一个AccessDeniedHandler的自定义实现也是:

 公共类CustomAccessDeniedHandler实现AccessDeniedHandler {

@Override
public void handle( HttpServletRequest请求,HttpServletResponse响应,AccessDeniedException arg2)抛出IOException,ServletException {
response.sendRedirect(request.getContextPath()+/ erro / no_permit);
}

}


解决方案

如果我是对的,为了个性化页面403,您可以使用此服务器实现的模型。



Spring Security:自定义403拒绝访问页面



示例:



AppConfig.java

  @Override 
protected void configure(HttpSecurity http)抛出异常{
http
.authorizeRequests()
.antMatchers(/ resources / **,/ signup)。permitAll()
.anyRequest()。authenticated()
。和()
.formLogin()
.loginPage(/ login)
.permitAll()
.and()
.exceptionHandling()。accessDeniedPage( / 403)
。和()
.logout()。logoutUrl(/ logout)。logoutSuccessUrl(/)
.and()
.rememberMe ()
。和()
.csrf()。disable();
}

HomeController.java

  @RequestMapping(/ 403)
public String accessDenied(){
returnerrors / 403;
}

而.html将是一个包含403消息的自定义页面。 / p>

Anyone knows how to configure a customized 403 page in spring security? Looking in the web, all the results I get it's with XML configuration, and I am using Java configuration. That's my SecurityConfig:

@Configuration
@ComponentScan(value="com")
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return new CustomAuthenticationManager();
    }

    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
                .disable()
            .authorizeRequests()
                .antMatchers("/resources/**", "/publico/**").permitAll()
                .anyRequest().authenticated()
                .and()
            .formLogin()
                .loginPage("/acesso/login").permitAll()
                .loginProcessingUrl("/login").permitAll()
                .usernameParameter("login")
                .passwordParameter("senha")
                .successHandler(new CustomAuthenticationSuccessHandler())
                .failureHandler(new CustomAuthenticationFailureHandler())
                .and()
            .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/acesso/login").permitAll();
    }

}

I have a custom implementation for AccessDeniedHandler too:

public class CustomAccessDeniedHandler implements AccessDeniedHandler {

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

}

解决方案

If I'm right, to personalize the page 403, you could use the model implemented by this server.

Spring Security : Customize 403 Access Denied Page

Example:

AppConfig.java

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
            .antMatchers("/resources/**", "/signup").permitAll()
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
            .exceptionHandling().accessDeniedPage("/403")
            .and()
            .logout().logoutUrl("/logout").logoutSuccessUrl("/")
            .and()
            .rememberMe()
            .and()
            .csrf().disable();
}

HomeController.java

@RequestMapping("/403")
public String accessDenied() {
    return "errors/403";
}

And the .html, would be a custom page with some message 403.

这篇关于通过java代码配置弹簧安全性的自定义403错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆