2014-04-14 9 views
0

Grails 백엔드가있는 단일 페이지 응용 프로그램을 디자인하고 있습니다. 백엔드의 서비스 중 일부는 인증을 요구할 것이므로이를 관리하기 위해 클라이언트 측 쿠키와 함께 스프링 보안 플러그인을 사용하려고 시도하고 있습니다.Grails Spring Security Security

나는 모든 것을 시도했지만 사용자 이름/암호 매개 변수 세트를 사용하는 로그인 서비스를 만드는 방법에 대한 정보가 많지 않으며 세션을 인증 된 것으로 설정합니다. 여기까지 내가 지금까지 가지고있는 것이있다.

class LoginService { 

    def userDetailsService 
    def daoAuthenticationProvider 

    def login(String username, String password) 
    { 
     UserDetails userDetails = userDetailsService.loadUserByUsername(username); 
     Authentication authentication = 
     new UsernamePasswordAuthenticationToken(username, password); 
     SecurityContextHolder.getContext().setAuthentication(authentication); 
     daoAuthenticationProvider.additionalAuthenticationChecks( 
     userDetails, authentication) 
     authentication.isAuthenticated() 
    } 
    } 

잘못된 생각은 UserDetailsService가 해당 사용자 이름과 관련된 db의 개체를로드한다는 것입니다. 인증 개체는 해당 UserDetail과 관련하여 사용하려는 방법입니다. 그런 다음 daoAuthenticationProvider는 세부 정보 및 인증 개체가 서로 충돌하는지 확인 (유효한 암호 확인)합니다. 두 테스트는 "잘못된 자격 증명"

def fixtureLoader 
def grailsApplication 
def loginService 
def loginPerson 

def setup() { 
    loginPerson = new Person(); 
    loginPerson.username = "username" 
    loginPerson.password = "password" 
    loginPerson.email = "email" 
    loginPerson.save(flush: true, failOnError: true) 
} 

def cleanup() { 
} 

void "test correct login"() { 
    when: 
    def result = loginService.login(loginPerson.username,loginPerson.password) 

    then: 
    assert result == true 
} 

void "test incorrect login"() { 
    when: 
    def result = loginService.login(loginPerson.username,"computer") 

    then: 
    assert result == false 
} 

실패함으로써

은 여기가 내가 지금까지 인증 이벤트의 순서에 관한 일을하는데있어 정말로 확실하지 않다 내 서비스 테스트입니다.

도움을 주시면 대단히 감사하겠습니다. 수동으로 사용자가 로그인 할 경우

+0

이러한 접근 방식은 효과가 있거나 그렇지 않아야합니다. 하지만 통합 테스트를 통해 테스트 할 수는 없습니다. 스프링 보안은 서블릿 필터 체인으로 구현되며 유닛 또는 통합 테스트에서는 활성화되지 않습니다. 요청 및 응답은 단순한 모의 (mock)입니다. 기능 테스트를 해보십시오. –

답변

1
    가의 AuthenticationManager를 사용하는 로그인 서비스 방법을 변경
  • :
하여 통합 테스트 "테스트 올바른 로그인"에서

class LoginService { 
    def authenticationManager 

    def login(String username, String password) { 
     Authentication preAuthentication = new UsernamePasswordAuthenticationToken(username, password) 
     def authentication = authenticationManager.authenticate(preAuthentication) 
     SecurityContextHolder.getContext().setAuthentication(authentication) 
     authentication.isAuthenticated() 
    } 
} 
  • 는, 문자열을 전달 "암호 "대신 passwordPerson.password (비밀번호가 암호화 된 경우)

  • "잘못된 로그인 테스트 "에서 'assert result == false'를 'throw 된 dCredentialsException) '(실제로 여기에 예외가 실제로 발생 함)

관련 문제