簡體   English   中英

Spring Security Grails插件

[英]Spring security grails plugin

我在grails應用程序中使用的是Spring security grails插件v2.0.0 RC5 ,但我注意到了一點,即在登錄該應用程序時,用戶名不區分大小寫,例如,如果您編寫了userUSER都可以成功登錄。 我需要做的是使用戶名區分大小寫。

我在springSecurityService找到了isLoggedIn操作來處理登錄,但是對提供的用戶名或密碼進行任何檢查都看不到任何內容。

這是isLoggedIn代碼:

boolean isLoggedIn() {
    def authentication = SCH.context.authentication
    authentication && !authenticationTrustResolver.isAnonymous(authentication)
}

我在尋找正確的地方嗎?

在我的Grails應用中,我使用的是Spring Security插件,並定義了一個自定義的userDetailsS​​ervice 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM