【问题标题】:Grails Spring security for different form of authentication用于不同形式身份验证的 Grails Spring 安全性
【发布时间】:2013-05-02 08:41:46
【问题描述】:
我目前正在使用 grails 和 Spring 安全性来实现身份验证。没有做太多的定制,使用开箱即用
对于登陆 auth.gsp 的用户来说一切正常,他将在其中输入用户名/密码。
现在我有一个正常流程的要求,将从外部站点到我们的站点进行打孔。打孔将提供用户名和密码作为参数。
在打卡场景中,用户不应看到登录屏幕,而应使用 url 参数中提供的用户名/密码进行身份验证。
这可能吗 ?
我怎样才能在同一个代码中实现这两个目标
【问题讨论】:
标签:
spring
security
grails
【解决方案1】:
是的,您可以进行编程登录。例如:
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
希望这会有所帮助。