2016-08-16 5 views
1

사용자 확인을 처리하기 위해 grails에서 Apache Shiro 플러그인을 사용하고 있습니다.아파치 시로의 패스워드 해시 Grails

Google 관리자가 특정 사용자를 위해 로그인 할 수있는 시스템의 관리자 부분에 기능을 추가하고 싶습니다.

관리자를위한 새로운 복잡한 인터페이스를 만들지 않으려합니다.

사용자로 로그인하는 방법, 암호는 모르지만 암호 만 해시?

답변

0

나는 결국 그것을 해결,하지만 난 내 솔루션의 보안에 대해 확실하지 않다, 그래서 당신이 그것에 대해 언급하고 싶습니다.

admin 컨트롤러에 signIn이라는 새 작업을 만들었습니다. 이 컨트롤러는 관리자 만 액세스 할 수 있으며 다른 사용자는 액세스 할 수 없습니다. 동작은 다음과 같습니다.

def signIn(String username) { 

     def authToken = new UsernamePasswordToken(username, params.password as String) 

     // Specific setting for administrator signIn 
     // 
     params.rememberMe = false 
     params.targetUri = '/' 
     authToken.host = 'admin' 

     // If a controller redirected to this page, redirect back 
     // to it. Otherwise redirect to the root URI. 
     def targetUri = params.targetUri ?: "/index/index" 

     // Handle requests saved by Shiro filters. 
     SavedRequest savedRequest = WebUtils.getSavedRequest(request) 
     if (savedRequest) { 
      targetUri = savedRequest.requestURI - request.contextPath 
      if (savedRequest.queryString) targetUri = targetUri + '?' + savedRequest.queryString 
     } 

     try{ 
      // Perform the actual login. An AuthenticationException 
      // will be thrown if the username is unrecognised or the 
      // password is incorrect. 
      SecurityUtils.subject 
      SecurityUtils.subject.login(authToken) 

      log.info "Redirecting to ${targetUri}." 
      redirect(uri: targetUri) 
     } 
     catch (AuthenticationException ex){ 
      // Authentication failed, so display the appropriate message 
      // on the login page. 
      log.info "Authentication failure for user ${params.username}." 
      flash.message = message(code: "login.failed") 

      // Keep the username and "remember me" setting so that the 
      // user doesn't have to enter them again. 
      def m = [ username: params.username ] 

      // Remember the target URI too. 
      if (params.targetUri) { 
       m["targetUri"] = params.targetUri 
      } 

      // Now redirect back to the login page. 
      redirect(action: "login", params: m) 
     } 
    } 

그리고 DbShiroRealm도 약간 수정했습니다.

def authenticate(authToken) { 


    log.info "Attempting to authenticate ${authToken.username} in DB realm..." 
    def username = authToken.username 

    // Null username is invalid 
    if (username == null) { 
     throw new AccountException("Null usernames are not allowed by this realm.") 
    } 

    // Get the user with the given username. If the user is not 
    // found, then they don't have an account and we throw an 
    // exception. 
    def user = User.findByUsername(username) 
    if (!user) { 
     throw new UnknownAccountException("No account found for user [${username}]") 
    } 

    log.info "Found user ${user.username} in DB" 

    // Now check the user's password against the hashed value stored 
    // in the database. 
    def account = new SimpleAccount(username, user.password, "ShiroDbRealm") 

    if(!(authToken.host == 'admin')) { // skips password authentication 
     if (!credentialMatcher.doCredentialsMatch(authToken, account)) { 
      log.info "Invalid password (DB realm)" 
      throw new IncorrectCredentialsException("Invalid password for user ${username}") 
     } 
    } 

    return account 
} 

나는 로그인되어 정보를 전달하기 위해 UsernamePasswordToken에 호스트 필드를 사용했다.

UsernamePasswordToken을 무시하고 암호 인증을 건너 뛰는에 대한 추가 부울 필드를 추가하는 것이 더 적절할 것이다.