如何在没有XML配置的情况下定义自定义AuthenticationEntryPoint

我已经挣扎了好几天了.我是 Spring Boot的新手,并且喜欢不使用XML配置的想法.

我创建了一个RESTfull应用程序(使用JSON).我正在关注this tutorial以正确配置身份验证.

我认为我设法使用Java配置重现几乎所有的配置,除了一件事 – AuthenticationEntryPoint

本教程使用http标记中的属性,并按以下方式定义formLogin:

<http entry-point-ref="restAuthenticationEntryPoint">

  <intercept-url pattern="/api/admin/**" access="ROLE_ADMIN"/>

  <form-login
     authentication-success-handler-ref="mySuccessHandler"
     authentication-failure-handler-ref="myFailureHandler"
  />

  <logout />

</http>

Spring Security manual中的AuthenticationEntryPoint说明说:

AuthenticationEntryPoint can be set using the entry-point-ref attribute on the < http > element.

没有提到有关如何使用Java配置执行此操作的任何内容.

那么如何在没有XML的情况下“注册”我自己的restAuthenticationEntryPoint,以防止在使用formLogin时重定向到登录表单?

下面我将提到我尝试过的内容.

谢谢你们.

在我的尝试中,发现你可以使用basicAuth定义它,如下所示:

@Configuration
@Order(1)                                                        
public static class RestWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {


        if (restAuthenticationEntryPoint == null) {
            restAuthenticationEntryPoint = new RestAuthenticationEntryPoint();
        }

        http
            .authorizeRequests()
                .antMatchers("/**").hasAnyRole(Sec.ADMIN,Sec.SUPER_USER)
...
        .and()
            .httpBasic()
                .authenticationEntryPoint(restAuthenticationEntryPoint)

但我正在使用这样的表单登录(没有httpBasic部分):

.and()
            .formLogin()
                .successHandler(mySavedRequestAwareAuthenticationSuccessHandler)
                .failureHandler(simpleUrlAuthenticationFailureHandler)

问题是,当它没有收到凭据时,它会重定向到登录表单.由于这是一个REST服务,它不应该.

FormLoginConfigurer的documentation(类.formLogin()使用)说:

Shared Objects Created

The following shared objects are populated

06003

但是找不到覆盖它的方法.
有任何想法吗?

附:
不要认为将登录表单覆盖到只返回错误的自定义表单是个好主意.

您提供的参考文档的引用指向http.exceptionHandling().您可以在那里设置共享入口点.
http.exceptionHandling().authenticationEntryPoint(myEntryPoint);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇