스프링 보안의 본질
스프링 보안은 본질적으로 일련의 필터로, 필터 체인 프록시 (FilterchainProxy)라는 별도의 필터로 필터 체인에 삽입됩니다. 그림에서 볼 수 있듯이.
실제로, 다른 URL을 확인하기 위해 FilterChainProxy 아래에 여러 필터 체인이있을 수 있으며, 필터 체인에 소유 한 필터는 정의 된 서비스에 따라 자동으로 증가 또는 감소합니다. 따라서 자신의 논리를 구현하지 않는 한 이러한 필터를 표시하고 정의 할 필요가 없습니다.
주요 범주
입증
인증은 사용자 인증 정보를 나타내는 데 사용되는 인터페이스입니다. 사용자가 인증을 위해 로그인하기 전에 관련 정보는 인증 특정 구현 클래스의 객체로 캡슐화됩니다. 로그인 인증이 성공하면 사용자 권한 및 기타 정보를 포함한보다 포괄적 인 정보가있는 인증 객체가 생성 된 다음 SecurityContexTholder가 보유한 SecurityContext에 저장되어 액세스 권한의 인증과 같은 후속 프로그램을 호출합니다.
AuthenticationManager
검증에 사용되는 기본 인터페이스는 AuthenticationManager입니다.
Public Interface AuthenticationManager {인증 인증 (인증 인증)은 AuthenticationException;}Authenticate () 메소드가 실행 된 후에는 세 가지 상황이있을 수 있습니다.
확인이 성공적이며 사용자 정보에 대한 인증이 반환됩니다.
확인이 실패했으며 AuthenticationException 예외가 발생했습니다.
판단 할 수 없습니다.
공급자 관리자
ProviderManager는 위의 AuthenticationManager의 가장 일반적인 구현입니다. 자체적으로 검증을 처리하지는 않지만 확인을 구성하는 AuthenticationProvider 목록에 확인을 위임 한 다음 각 AuthenticationProvider를 차례로 호출하여 인증을받습니다. 이 과정에서 AuthenticationProvider가 성공적으로 인증되는 한 더 이상 확인은 계속되지 않습니다. 이 인증 결과는 ProviderManager의 인증 결과로 직접 사용됩니다.
인증 프로세스
사용자는 사용자 이름과 비밀번호로 로그인합니다.
Spring Security는 획득 된 사용자 이름과 비밀번호를 일반적으로 사용되는 UsernamepasswordaUthenticationToken과 같은 인증 인터페이스 구현 클래스로 캡슐화합니다.
위에서 생성 된 인증 객체를 인증을 위해 AuthenticationManager 구현 클래스 제공자 관리자로 전달하십시오.
ProviderManager는 인증을 위해 각 AuthenticationProvider를 차례로 호출합니다. 인증이 성공하면 인증 객체가 사용자 권한을 캡슐화하고 기타 정보가 반환됩니다.
AuthenticationManager가 반환 한 인증 객체를 현재 SecurityContext에 할당하십시오.
사용자 정의 검증
위의 지식 예약을 사용하면 검증 방법을 사용자 정의 할 수 있습니다. 위에서부터는 실제로 AuthenticationProviders가 실제로 검증 작업을 수행하는 데 사용된다는 것을 알 수 있습니다. 따라서 확인 방법을 사용자 정의하려면 자신의 AuthenticationProvider 만 구현 한 다음 제공자 관리자에 추가하면됩니다.
AuthenticationProvider를 사용자 정의하십시오
@ComponentPublic 클래스 CustomAuthenticationProvider는 AuthenticationProvider {@override public authentication authentication (authentication authentication)을 authenticationException {String name = authentication.getName (); 문자열 암호 = augentication.getCredentials (). toString (); if (if (whistauthenticateAgainstthirdPartySystem ()) {// 자격 증명 // 사용 및 제 3 자 시스템에 대해 인증 된 새로운 usernamepasswordauthenticationToken (이름, 암호, new ArrayList <> ()); } else {return null; }} @override public boolean supports (class <?> 인증) {return Authentication.equals (usernamepasswordauthenticationToken.class); }}supports () 메소드는 인증 매개 변수를 수락하여 전달 된 인증이 AuthenticationProvider가 처리 할 수있는 유형입니다.
AuthenticationProvider를 등록하십시오
이제 제공자 관리자에서 방금 만든 AuthenticationProvider를 등록하고 모든 작업이 완료됩니다.
@configuration@enablewebsecurity@componentscan ( "org.baeldung.security") public class SecurityConfig는 websecurityconfigureradapter {@autowired private customauthenticationprovider authprovider를 확장합니다. @override protected void configure (AuthenticationManagerBuilder Auth)는 예외 {auth.authenticationProvider (authprovider); } @Override Protected Void Configure (httpsecurity http) 예외 {http.authorizeRequests (). anyRequest (). authenticated () and () .httpbasic (); }}요약
위는 Spring Security Verification Process 분석 및 편집자가 귀하에게 소개 한 사용자 정의 검증 방법입니다. 나는 그것이 당신에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!