2013-05-02 80 views
1

我目前使用grails和Spring安全來實現身份驗證。沒有做太多的定製,使用開箱即用的 ,對於登陸auth.gsp的用戶來說,一切正常,他將輸入用戶名/密碼。 現在我有一個要求與正常流量一起,將有一個從外部網站衝到我們的site.punch out將有用戶名和密碼作爲參數提供。 在衝出場景中,用戶不應該看到登錄屏幕,並應使用url參數中提供的用戶名/密碼進行身份驗證。 這可能嗎? 我該如何在相同的代碼中同時獲得這兩項Grails針對不同形式的身份驗證的Spring安全

回答

4

是的,您可以進行程序化登錄。例如:

import org.springframework.security.authentication.AuthenticationManager; 
import org.springframework.security.core.context.SecurityContextHolder; 
.... 
AuthenticationManager authenticationManager; 
.... 
def login(String username, String password) { 
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(params.username, params.password); 
    User details = new User(params.username); 
    token.setDetails(details); 

    try { 
     //doing actual authentication  
     Authentication auth = authenticationManager.authenticate(token); 
     log.debug("Login succeeded!"); 
     //setting principal in context 
     SecurityContextHolder.getContext().setAuthentication(auth); 
     return true 
    } catch (BadCredentialsException e) { 
     log.debug("Login failed") 
     return false 
    } 
} 

這裏你可以看到一些例如:http://raibledesigns.com/rd/entry/java_web_application_security_part3

希望這有助於。