Можно ли установить дополнительные параметры пружинной загрузки с заводной? - PullRequest
0 голосов
/ 09 января 2019

Я новичок в groovy и весенней загрузке. Я начинаю работать при входе в систему с весенней загрузкой. Мне нужно передать два дополнительных параметра в класс CustomAuthToken . Я могу передать только один. Когда я назначаю другую переменную некоторому значению, auth fail.

Это мой код.

CustomAuthFilter.groovy

Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {

    if (!request.post) {
        throw new AuthenticationServiceException("not supported: $request.method")
    }
    String username = (obtainUsername(request) ?: '').trim()
    String password = (obtainPassword(request) ?: '').trim()
    String extrafield1 = request.getParameter("extrafield1")
    String extrafield2 = request.getParameter("extrafield2")


        def authentication = new CustomAuthToken(username, password, extrafield1, null, false, false, false)

    HttpSession session = request.getSession(false)
    if (session || getAllowSessionCreation()) {
        request.session['SPRING_SECURITY_LAST_USERNAME_KEY'] = TextEscapeUtils.escapeEntities(username)
    }

    return getAuthenticationManager().authenticate(authentication)
}

CustomAuthToken.groovy

CustomAuthToken(Object principal, Object credentials, String extrafield1, String PVM, Boolean isAccept, Boolean isLogEnabled, Boolean is3PLEnabled) {
    super(principal, credentials)
    extra1 = extrafield1
}

Это работает, и я могу получить доступ к полю extra1. Но когда я пытаюсь передать другой параметр, он не работает.

CustomAuthToken(Object principal, Object credentials, String extrafield1, String extrafield2, String PVM, Boolean isAccept, Boolean isLogEnabled, Boolean is3PLEnabled) {
    super(principal, credentials)
    extra1 = extrafield1
    extra2 = extrafield2
}

Когда я пытаюсь это extra2 проходит. Но аутентификация не удалась. Может кто-нибудь иметь представление об этом?

1 Ответ

0 голосов
/ 10 января 2019

Я думаю, что

CustomAuthToken extends UsernamePasswordAuthenticationToken

Если это так, вам нужно изменить вызов конструктора super с

super(principal, credentials)

до

super(principal, credentials, Collections.emptyList())

Видите ли, конструктор, который вы вызываете, устанавливает authenticated=false

public UsernamePasswordAuthenticationToken(Object principal, Object credentials) {
    super(null);
    this.principal = principal;
    this.credentials = credentials;
    setAuthenticated(false);
}

Итак, вы хотите вызвать правильный конструктор

public UsernamePasswordAuthenticationToken(Object principal, Object credentials,
        Collection<? extends GrantedAuthority> authorities) {
    super(authorities);
    this.principal = principal;
    this.credentials = credentials;
    super.setAuthenticated(true); // must use super, as we override
}
...