【问题标题】:How to handle and custom error 401 from Spring boot ldap?如何处理和自定义来自 Spring Boot ldap 的错误 401?
【发布时间】:2018-10-24 04:44:48
【问题描述】:

我正在使用 Spring secutiry LDAP 对其用户进行身份验证的应用程序,身份验证工作正常,我的问题是,如果有人尝试使用未经授权的凭据登录,应用程序会立即发送错误 401,而我不想这样,我想自定义一些友好的消息来显示这个用户,但是即使在调试中我也找不到应用程序在哪里执行 de 身份验证,甚至似乎我的后端没有被调用,我该如何自定义这个异常?

这是我的安全配置类的配置:

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

}

提前感谢您的帮助!

【问题讨论】:

    标签: java spring-security spring-ldap


    【解决方案1】:

    你考虑过WhiteLabel Page吗?

    例如:

    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 前端,这些链接也可能适用:

    Tutorial: Spring Security and 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

    【讨论】:

    • 嗨@paulsm4!感谢您的帮助,我认为您的建议不适用于此应用程序,原因是该应用程序已经有一个 Angular 前端,它调用后端并期望返回一个带有结果的对象或异常的详细信息message 作为其属性之一,我可能错了,但在这种情况下我看不到如何使用白标页面。
    猜你喜欢
    • 2015-05-14
    • 2021-07-10
    • 2019-12-22
    • 2020-10-20
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多