java - 通过 java 代码配置 spring security 的自定义 403 错误页面

标签 java spring spring-mvc spring-security

谁知道如何在spring security中配置自定义的403页面?在网上查看,我得到的所有结果都是使用 XML 配置,而我使用的是 Java 配置。那是我的安全配置:

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

}

最佳答案

如果我是对的,要个性化 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();
}

家庭 Controller .java

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

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

关于java - 通过 java 代码配置 spring security 的自定义 403 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24194724/

相关文章:

java - 是否有用于将 spring 库添加到 Eclipse 中的项目的项目方面?

java - RestEasy 配置中的多个前缀

java - 应用程序在单击按钮时崩溃,显示尝试调用虚拟方法 'void android.widget.Button.setOnClickListener'

java - 使用 Firebase 在 Android 应用程序中使用 Spring Boot

java - Spring数据休息+Spring安全: @Secured not working

java - 如何将 spring-boot war 部署到 debian jetty8

java - 在 Android Activity 期间停止后台服务

java - PBKDF2-HMAC-SHA256 for JAVA 的可靠实现

java - 如何从hibernate auto转移到生产

java - Spring事务管理@Transactional行为