2010-07-01 4 views
5

Spring Security 구성에 사용자 정의 인증 공급자가 정의되어 있습니다. 이 클래스는 AuthenticationProvider를 구현하며 내 페이지에 정의 된 양식을 사용하여 성공적으로 로그인 할 수 있습니다. 문제는이 클래스를 로그인 페이지뿐만 아니라 등록 페이지에서도 호출하기를 원합니다.Spring - 컨트롤러에서 custom-authentication-provider를 호출하십시오.

등록 페이지는 다른 명령 클래스를 사용하며 로그인 양식보다 많은 정보를 수집합니다. 현재 사용자가 등록 할 때 적절한 컨트롤러를 호출하고 레코드를 데이터베이스에 추가하면 로그인 할 수 있지만 자동으로 로그인되지는 않습니다. 그들이 등록 페이지에서 사용자 이름/암호를 알려 주었기 때문에 이것을 사용자 지정 AuthenticationProvider 클래스에 전달하여 로그인 할 수도 있습니까?

등록 컨트롤러에서 org.springframework.security.Authentication 클래스를 만들고 내 고객 AuthenticationProvider 클래스에서 authenticate 메소드를 호출하려고 시도했지만 오류가 발생하지 않지만 사용자가 로그인하지 않았습니다. 이를 달성하기 위해 Spring Security 필터 체인에서 더 높은 메소드를 호출해야합니까? 컨트롤러를 j_spring_security_check URL로 리디렉션해야합니까? 그렇다면 어떻게 사용자 이름/비밀번호를 전달합니까?

답변

1

AuthenticationProvider.authenticate()의 결과를 SecurityContext (SecurityContextHolder에서 가져옴)에 넣어야합니다.

AuthenticationSuccessEvent - 애플리케이션이이 이벤트를 사용하는 경우 (일부 스프링 보안 기능에서도 사용할 수 있음) 게시해야합니다 (autowiring을 통해 기본값 AuthenticationEventPublisher을 얻을 수 있음). 인증 공급자를 ProviderManager으로 감싸는 것이 유용 할 수 있습니다. 지정된 게시자를 사용하여 자동으로 이벤트를 게시합니다.

2

문제는 사용자가 성공적으로 인증 했음에도 사용자의 SecurityContext에이 인증 결과를 저장하지 않았기 때문입니다. 웹 응용 프로그램에서 SecurityContextPersistenceFilter을 사용하는 this is a ThreadLocal objectstore the user's credentials in the HTTPSession

가능한 경우 사용자 지정 인증 공급자를 통해 직접 인증하지 않아야합니다. xml 구성에 맞춤 인증 공급자가 연결되어있는 AuthenticationManager이 있어야합니다. 예를 들어,

<bean id="customAuthenticationProvider" 
    class="com.foo.CustomAuthenticationProvider"> 
    <property name="accountService" ref="accountService"/> 
</bean> 
<security:authentication-manager alias="authenticationManager"> 
    <security:authentication-provider ref="customAuthenticationProvider"/> 
</security:authentication-manager> 

당신은 당신의 등록 서비스에 authenticationManager를 연결하고 추가,

등록 서비스는 다음과 같이 처리합니다.

final UsernamePasswordAuthenticationToken authRequest = new 
    UsernamePasswordAuthenticationToken(username, password); 

final Authentication authentication = 
    authenticationManager.authenticate(authRequest); 
SecurityContextHolder.getContext().setAuthentication(authentication); 

우리는 또한 선택적 기억 - 나 TokenBasedRememberMeServicesonLoginSuccess() 방법을 사용하여, 쿠키 이 시점에서 인증 결과를 저장합니다.

관련 문제