gpt4 book ai didi

grails - 从服务中获取Spring Security身份验证

转载 作者:行者123 更新时间:2023-12-02 13:56:39 24 4
gpt4 key购买 nike

由于我一直在Grails应用程序中使用专用服务,以使身份验证与vaadin UI一起使用,因此我在验证登录名时遇到了问题:

1)在 bootstrap 中创建一个新用户并将其记录到db(postgre)

User.withTransaction {
User test = new User(
username: "test",
password: springSecurityService.encodePassword("password"),
enabled: true,
accountExpired: false,
accountLocked: false,
passwordExpired: false
).save()

Role dashManager = new Role(authority: "ROLE_USER").save()

new UserRole(user: test, role: dashManager).save()

2)vaadin ui通常调用grails服务
boolean login(String username, String password) {
try {
println username + "----" + password
security.signIn(username, password)
return true
} catch (SecurityServiceException e) {
Notification.show("Login/Password incorrect", Notification.TYPE_ERROR_MESSAGE);
return false
}
}

3)我的securityService总是返回无效
import grails.transaction.Transactional
import org.springframework.security.core.context.SecurityContextHolder as SCH
import org.springframework.security.authentication.BadCredentialsException
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken

@Transactional
class SecurityService {

def springSecurityService
def authenticationManager

void signIn(String username, String password) {
try {
def authentication = new UsernamePasswordAuthenticationToken(username, password)
SCH.context.authentication = authenticationManager.authenticate(authentication)
} catch (BadCredentialsException e) {
throw new SecurityException("Invalid username/password")
}
}

void signOut() {
SCH.context.authentication = null
}

boolean isSignedIn() {
return springSecurityService.isLoggedIn()
}
}

最佳答案

您可能正在对密码进行双重编码。插件的最新版本会生成一个用户/人域类,该类为您编码密码,因此您无需调用springSecurityService.encodePassword("password"),如果这样做,则将其编码两次。这应该工作:

User test = new User(
username: "test",
password: "password",
enabled: true
).save()

我省略了将 accountExpiredaccountLockedpasswordExpired设置为 false的方法,因为这些是默认值。

关于grails - 从服务中获取Spring Security身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20685982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com