簡體   English   中英

如何從Spring Boot ldap處理和自定義錯誤401?

[英]How to handle and custom error 401 from Spring boot ldap?

我正在使用Spring Secutiry LDAP對其用戶進行身份驗證的應用程序,身份驗證工作正常,我的問題是,如果有人嘗試使用未經授權的憑據登錄,則該應用程序會立即發送錯誤401,而我卻沒有想要這樣,我想自定義一些友好的消息來顯示此用戶,但是即使在調試中,我也找不到應用程序在哪里執行de authentication,甚至似乎沒有調用我的后端,我如何自定義此異常?

這是我的“安全性”配置類上的配置:

@Configuration
@Order(2147483640)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and()
            .cors().and()
            .csrf().disable()
            .authorizeRequests()
            .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
            .antMatchers(HttpMethod.GET, "/**").permitAll()
            .antMatchers(HttpMethod.POST, "/**").permitAll()
            .antMatchers(HttpMethod.PUT, "/**").permitAll()
            .antMatchers(HttpMethod.DELETE, "/**").permitAll()
            .antMatchers("/**").permitAll();
}

@Configuration
protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {

    @Autowired
    private UsrPessoaService usrPessoaService;

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication().userDetailsContextMapper(usrPessoaService)
                .userDnPatterns("secret")
                .groupSearchBase("secret")
                .contextSource()
                .root("secret")
                .url("secret").port(000);

        System.out.println(auth.toString());
    }
}

}

在此先感謝您的幫助!

您是否考慮過WhiteLabel頁面

例如:

https://www.baeldung.com/spring-boot-custom-error-page

@RequestMapping("/error")
public String handleError(HttpServletRequest request) {
    Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);

    if (status != null) {
        Integer statusCode = Integer.valueOf(status.toString());

        if(statusCode == HttpStatus.NOT_FOUND.value()) {
            return "error-404";
        }
        else if(statusCode == HttpStatus.INTERNAL_SERVER_ERROR.value()) {
            return "error-500";
        }
    }
    return "error";
}

還有一個示例“ error.html”模板:

<!DOCTYPE html>
<html>
<body>
<h1>Page not found</h1>
<h2>Sorry, we couldn't find the page you were looking for</h2>
<a href="/">Go Home</a>
</body>
</html>

附錄:

由於OP具有Angular前端,因此這些鏈接也可能適用:

教程:Spring Security和Angular

在本教程中,我們將展示Spring Security,Spring Boot和Angular的一些不錯的功能,共同提供令人愉悅和安全的用戶體驗。

Spring和Angular的初學者應該可以使用它,但是在這兩個方面的專家都將使用很多細節。

也可以看看:

https://github.com/spring-projects/spring-security/blob/master/web/src/main/java/org/springframework/security/web/AuthenticationEntryPoint.java

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM