簡體   English   中英

Grails Spring-安全性混淆

[英]Grails Spring-Security Confustion

我正在設計一個帶有Grails后端的單頁應用程序。 后端的某些服務將需要身份驗證,因此我試圖將Spring Security插件與客戶端cookie一起使用來管理此服務。

我已經嘗試了所有方法,但是關於創建使用用戶名/密碼參數集並將會話設置為經過身份驗證的登錄服務的信息似乎並不多。 到目前為止,這就是我所擁有的。

 class LoginService {

  def userDetailsService
  def daoAuthenticationProvider

  def login(String username, String password )
  {
      UserDetails userDetails = userDetailsService.loadUserByUsername(username);
      Authentication authentication = 
        new UsernamePasswordAuthenticationToken(username, password);
      SecurityContextHolder.getContext().setAuthentication(authentication);
      daoAuthenticationProvider.additionalAuthenticationChecks( 
        userDetails, authentication)
      authentication.isAuthenticated()
  }
  }

我的錯誤想法是UserDetailsS​​ervice從數據庫中加載了有關用戶名的對象。 身份驗證對象是我想使用的有關該UserDetail的方法。 然后,daoAuthenticationProvider檢查以查看詳細信息和身份驗證對象是否相互配合(檢查有效密碼)。

這是我的服務測試,其中兩個測試均因“憑據錯誤”而失敗

def fixtureLoader
def grailsApplication
def loginService
def loginPerson

def setup() {
    loginPerson = new Person();
    loginPerson.username = "username"
    loginPerson.password = "password"
    loginPerson.email = "email"
    loginPerson.save(flush: true, failOnError: true)
}

def cleanup() {
}

void "test correct login"() {
    when:
    def result = loginService.login(loginPerson.username,loginPerson.password)

    then:
    assert result == true
}

void "test incorrect login"() {
    when:
    def result = loginService.login(loginPerson.username,"computer")

    then:
    assert result == false
}

對於身份驗證中的事件順序,我不確定該怎么做。

任何幫助是極大的贊賞。

  • 如果要手動登錄用戶,請將登錄服務方法更改為使用authenticationManager:

class LoginService {
    def authenticationManager

    def login(String username, String password ) {
        Authentication preAuthentication = new UsernamePasswordAuthenticationToken(username, password)
        def authentication = authenticationManager.authenticate(preAuthentication)
        SecurityContextHolder.getContext().setAuthentication(authentication)
        authentication.isAuthenticated()
    }
}
  • 在集成測試“測試正確的登錄名”中,傳遞字符串“ password”而不是loginPerson.password(以防密碼被加密)

  • 在“測試不正確的登錄”中,用“ throw(BadCredentialsException)”替換“ assert result == false”(實際上在這里需要例外)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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