【问题标题】:custom 403 error page with spring security configured via java code通过 java 代码配置 spring 安全性的自定义 403 错误页面
【发布时间】:2014-06-14 16:31:54
【问题描述】:

有人知道如何在spring security中配置自定义的403页面吗?在网上查看,我得到的所有结果都是 XML 配置,我使用的是 Java 配置。那是我的 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();
    }

}

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

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");
    }

}

【问题讨论】:

    标签: java spring spring-mvc spring-security


    【解决方案1】:

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

    Spring Security : Customize 403 Access Denied Page

    例子:

    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";
    }
    

    .html 将是一个带有一些消息 403 的自定义页面。

    【讨论】:

    • 我已经看过这个例子,但是它使用 XML 配置,我在我的项目中没有使用任何 XML。我需要一个使用 java 代码配置的示例。
    • 你添加了这个方法:.and() .exceptionHandling().accessDeniedPage("/403") 你的HTTPSecurity?
    • 好的,这就是我要找的。谢谢。
    • Spring Security 的实际 java 配置看起来很丑
    猜你喜欢
    • 2016-08-06
    • 1970-01-01
    • 2017-01-06
    • 2016-08-07
    • 2012-01-31
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多