복붙노트

[SPRING] XML 구성없이 사용자 지정 AuthenticationEntryPoint를 정의하는 방법

SPRING

XML 구성없이 사용자 지정 AuthenticationEntryPoint를 정의하는 방법

나는 며칠 동안 애를 썼다. 저는 스프링 부트 (Spring Boot)에서 새롭고 XML 구성을 사용하지 않는 아이디어를 좋아합니다.

JSON을 사용하여 RESTful 애플리케이션을 만들었습니다. 이 튜토리얼에 따라 인증을 적절하게 구성 할 것이다.

한 가지만 제외하면 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 매뉴얼에있는 AuthenticationEntryPoint 설명은 다음과 같이 설명한다.

Java Configuration을 사용하여 수행하는 방법에 대해서는 언급하지 않습니다.

그렇다면 formLogin을 사용할 때 로그인 양식으로 리디렉션되지 않도록 XML없이 restAuthenticationEntryPoint를 "등록"할 수 있습니까?

아래에서 나는 내가 시도한 것을 언급 할 것이다.

다들 감사 해요.

내 시도에서 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 (클래스 .formLogin ()이 사용하는) 문서는 다음과 같이 말합니다 :

그러나 그것을 무시할 수있는 방법을 찾지 못했습니다. 어떤 아이디어?

추신 로그인 양식을 오류 만 리턴하는 사용자 정의 양식으로 겹쳐 쓰는 것이 좋습니다.

해결법

  1. ==============================

    1.당신이 제공 한 ref 문서의 인용문은 http.exceptionHandling ()을 가리키고있다. 공유 엔트리 포인트를 설정할 수 있습니다.

    당신이 제공 한 ref 문서의 인용문은 http.exceptionHandling ()을 가리키고있다. 공유 엔트리 포인트를 설정할 수 있습니다.

    http.exceptionHandling().authenticationEntryPoint(myEntryPoint);
    
  2. from https://stackoverflow.com/questions/24684806/how-to-define-a-custom-authenticationentrypoint-without-xml-configuration by cc-by-sa and MIT license