[英]Spring security grails plugin
我在grails应用程序中使用的是Spring security grails插件v2.0.0 RC5 ,但我注意到了一点,即在登录该应用程序时,用户名不区分大小写,例如,如果您编写了user
或USER
都可以成功登录。 我需要做的是使用户名区分大小写。
我在springSecurityService
找到了isLoggedIn
操作来处理登录,但是对提供的用户名或密码进行任何检查都看不到任何内容。
这是isLoggedIn
代码:
boolean isLoggedIn() {
def authentication = SCH.context.authentication
authentication && !authenticationTrustResolver.isAnonymous(authentication)
}
我在寻找正确的地方吗?
有一个配置属性。 https://grails-plugins.github.io/grails-spring-security-core/3.2.x/index.html#domainClassProperties
userLookup.usernameIgnoreCase
在我的Grails应用中,我使用的是Spring Security插件,并定义了一个自定义的userDetailsService Spring bean,以便控制如何检索用户和角色数据,例如
class MyUserDetailsService implements GrailsUserDetailsService {
/**
* Some Spring Security classes (e.g. RoleHierarchyVoter) expect at least one role, so
* we give a user with no granted roles this one which gets past that restriction but
* doesn't grant anything.
*/
static final List NO_ROLES = [new GrantedAuthorityImpl(SpringSecurityUtils.NO_ROLE)]
UserDetails loadUserByUsername(String username, boolean loadRoles) {
return loadUserByUsername(username)
}
UserDetails loadUserByUsername(String username) {
User.withTransaction { status ->
User user = User.findByUsername(username)
if (!user && user.username.equal(username)) {
throw new UsernameNotFoundException('User not found', username)
}
def authorities = user.authorities.collect {new GrantedAuthorityImpl(it.authority)}
return new CustomUserDetails(
user.username,
user.password,
user.enabled,
!user.accountExpired,
!user.passwordExpired,
!user.accountLocked,
authorities ?: NO_ROLES,
user.id,
user.name)
}
}
}
现在你会看到
**if (!user && user.username.equal(username)) {**
这将解决您的问题。 但这是另一个问题,您的实现还取决于您的数据库,因为像MySQL一样,它不区分大小写,因此,如果搜索名称为Joy的用户,它将返回所有不带名称joy的用户,这无关紧要大写字母或小写字母。 因此,在保留新用户之前,您需要签入数据库值。 另外,另一个问题是,如果您要检查用户域
username blank: false, unique: true, email: true
用户名是唯一的,这意味着如果数据库中存在Joy,则不能再次插入用户名joy,因此您需要更改它并编写自己的自定义逻辑来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.