问:
How to structure python programs? Tried making it more structured, now runs 13 times slower
Im very new to programming, I wrote a simple program for a school project and wanted to make the code "prettier" by not just having the program be one giant function but instead be made up of multiple smaller functions with a singe purpose. I seemed to have messed up royally since the program now runs 13 times slower. How should I structured the program to make it run faster and just in general make programs easier to write, read and edit?

Here are the two programs:

First program (for reference values runs in ≈0:20):

```<code>import numpy as np
import matplotlib.pyplot as plt

def graf(a,b,H,p):
    GM =  39.5216489684
    x_0 = a + np.sqrt(a**2 - b**2)
    v_0 = np.sqrt(GM*(2/x_0 - 1/a))
    konstant_period = np.sqrt(a**3)*H
    h = 1/H

    '''starting position given by an elliptic orbit '''
    stor_x_lista = [x_0]
    stor_y_lista = [0]

    hastighet_x = [0]
    hastighet_y = [v_0]

    liten_x_lista = []
    liten_y_lista = []

    ''' a loop that approximates the points of the orbit'''

    t = 0
    tid_lista = []
    n = 0
    while n < konstant_period:
        hastighet_x.append(hastighet_x[n] - h*GM* stor_x_lista[n]/(np.sqrt(stor_x_lista[n]**2 + stor_y_lista[n]**2))**3)
        stor_x_lista.append(stor_x_lista[n] + h*hastighet_x[n])

        hastighet_y.append(hastighet_y[n] - h*GM*stor_y_lista[n]/(np.sqrt(stor_x_lista[n]**2 + stor_y_lista[n]**2))**3)
        stor_y_lista.append(stor_y_lista[n] + h*hastighet_y[n])

    '''smaller list of points to run faster'''
    if n % p == 0:
            liten_x_lista.append(stor_x_lista[n])
            liten_y_lista.append(stor_y_lista[n])
            tid_lista.append(t)

    n += 1
    t += h
    ''' function that finds the angle'''
    vinkel = []
    siffra = 0
    while siffra < len(liten_x_lista):
        if liten_y_lista[siffra ] >= 0:
            vinkel.append( np.arccos( liten_x_lista[siffra]/np.sqrt( liten_x_lista[siffra]**2 + liten_y_lista[siffra]**2)))
            siffra += 1

        elif liten_y_lista[siffra] < 0 :
            vinkel.append( np.pi + np.arccos( -liten_x_lista[siffra]/np.sqrt( liten_x_lista[siffra]**2 + liten_y_lista[siffra]**2) ))
            siffra += 1

    '''get rid of line to find periodic function'''
    mod_lista = []

    modn = 0
    while modn < len(vinkel):
        mod_lista.append(vinkel[modn] - (2*np.pi*tid_lista[modn])/np.sqrt(a**3))
        modn += 1

    '''make all inputs have period 1'''
    squeeze_tid = []
    squeezen = 0
    while squeezen < len(tid_lista):
        squeeze_tid.append(tid_lista[squeezen]/np.sqrt(a**3))
        squeezen += 1

    del mod_lista[-1:]
    del tid_lista[-1:]
    del squeeze_tid[-1:]

    plt.plot(squeeze_tid,mod_lista)
    plt.title('p(t) där a = ' + str(a) + ' och b = ' + str(b))
    plt.show
</code>```

Second more split up program (for reference values runs in ≈4:20):

```<code>import numpy as np
import matplotlib.pyplot as plt

'''function that generates the points of the orbit'''
def punkt(a,b,H,p):
    GM =  39.5216489684
    x_0 = a + np.sqrt(a**2 - b**2)
    v_0 = np.sqrt(GM*(2/x_0 - 1/a))
    konstant_period = np.sqrt(a**3)*H
    h = 1/H

    '''starting position given by an elliptic orbit '''
    stor_x_lista = [x_0]
    stor_y_lista = [0]

    hastighet_x = [0]
    hastighet_y = [v_0]

    liten_x_lista = []
    liten_y_lista = []

    ''' a loop that approximates the points of the orbit'''
    t = 0
    tid_lista = []
    n = 0
    while n < konstant_period:
        hastighet_x.append(hastighet_x[n] - h*GM* stor_x_lista[n]/(np.sqrt(stor_x_lista[n]**2 + stor_y_lista[n]**2))**3)
        stor_x_lista.append(stor_x_lista[n] + h*hastighet_x[n])

        hastighet_y.append(hastighet_y[n] - h*GM*stor_y_lista[n]/(np.sqrt(stor_x_lista[n]**2 + stor_y_lista[n]**2))**3)
        stor_y_lista.append(stor_y_lista[n] + h*hastighet_y[n])

        '''smaller list of points to run faster'''
        if n % p == 0:
            liten_x_lista.append(stor_x_lista[n])
            liten_y_lista.append(stor_y_lista[n])
            tid_lista.append(t)

        n += 1
        t += h

    return (liten_x_lista,liten_y_lista,tid_lista)

''' function that finds the angle'''

def vinkel(a,b,H,p):
    '''import lists'''
    liten_x_lista = punkt(a,b,H,p)[0]
    liten_y_lista = punkt(a,b,H,p)[1]
    tid_lista = punkt(a,b,H,p)[2]

    '''find the angle'''
    vinkel_lista = []
    siffra = 0
    while siffra < len(liten_x_lista):
        if liten_y_lista[siffra ] >= 0:
            vinkel_lista.append( np.arccos( liten_x_lista[siffra]/np.sqrt( liten_x_lista[siffra]**2 + liten_y_lista[siffra]**2)))
            siffra += 1

        elif liten_y_lista[siffra] < 0 :
            vinkel_lista.append( np.pi + np.arccos( -liten_x_lista[siffra]/np.sqrt( liten_x_lista[siffra]**2 + liten_y_lista[siffra]**2) ))
            siffra += 1
    return (vinkel_lista, tid_lista)

def periodisk(a,b,H,p):
    '''import lists'''
    tid_lista = vinkel(a,b,H,p)[1]
    vinkel_lista = vinkel(a,b,H,p)[0]

    '''get rid of linear line to find p(t)'''
    mod_lista = []

    modn = 0
    while modn < len(vinkel_lista):
        mod_lista.append((vinkel_lista[modn] - (2*np.pi*tid_lista[modn])/np.sqrt(a**3)))
        modn += 1

    '''make all inputs have period 1'''
    squeeze_tid = []
    squeezen = 0
    while squeezen < len(tid_lista):
        squeeze_tid.append(tid_lista[squeezen]/np.sqrt(a**3))
        squeezen += 1

    del mod_lista[-1:]
    del tid_lista[-1:]
    del squeeze_tid[-1:]

    return (squeeze_tid,mod_lista)

'''fixa 3d-punkt av p(a,b) a är konstant b varierar??? '''

def hitta_amp(a):
    x_b = []
    y_b = []
    n_b = 0.1
    while n_b <= a:
        x_b.append(n_b)
        y_b.append(punkt(a,n_b,10**5,10**3))

    return 0

def graf(a,b,H,p):
    plt.plot(periodisk(a,b,H,p)[0],periodisk(a,b,H,p)[1])
    plt.show
</code>```

I would assume the thing that is going wrong is that the program is running the same, slow code multiple times instead of just running it once and then accessing the data. Is the problem that everything is done locally and nothing is stored globally or is it something else?
Just as a heads up, the only thing I know about programming is basic syntax, I have no clue how to actually write and run programs. I ran all the code in spyder if that affects anything.

答:
You are calling the functions once per returned list, you should only call them once.

When a method returns multiple variables, (e.g. punkt):

def punkt(a,b,H,p):

    # Here is all your code

    return (liten_x_lista,liten_y_lista,tid_lista)
```

<p>You must be careful to only call the function once:</p>

<pre class="lang-py prettyprint-override"><code>   result = punkt(a,b,H,p)
   liten_x_lista = result[0]
   liten_y_lista = result[1]
   tid_lista = result[2]

   # As opposed to:
   liten_x_lista = punkt(a,b,H,p)[0] # 1st call, ignoring results 2 and 3
   liten_y_lista = punkt(a,b,H,p)[1] # 2nd call, ignoring results 1 and 3
   tid_lista = punkt(a,b,H,p)[2] # 3rd call, ignoring results 1 and 2
</code>```

Note: I would personally not return a list, but use python's unpacking:

def punkt(a,b,H,p):

    # Here is all your code

    return liten_x_lista, liten_y_lista, tid_lista
```

<p>And you'd access it:</p>

<pre class="lang-py prettyprint-override"><code>   liten_x_lista, liten_y_lista, tid_lista = punkt(a,b,H,p)
</code>```

Body:
Im very new to programming, I wrote a simple program for a school project and wanted to make the code "prettier" by not just having the program be one giant function but instead be made up of multiple smaller functions with a singe purpose. I seemed to have messed up royally since the program now runs 13 times slower. How should I structured the program to make it run faster and just in general make programs easier to write, read and edit?

Here are the two programs:

First program (for reference values runs in ≈0:20):

import numpy as np
import matplotlib.pyplot as plt

def graf(a,b,H,p):
    GM =  39.5216489684
    x_0 = a + np.sqrt(a**2 - b**2)
    v_0 = np.sqrt(GM*(2/x_0 - 1/a))
    konstant_period = np.sqrt(a**3)*H
    h = 1/H

    '''starting position given by an elliptic orbit '''
    stor_x_lista = [x_0]
    stor_y_lista = [0]

    hastighet_x = [0]
    hastighet_y = [v_0]

    liten_x_lista = []
    liten_y_lista = []

    ''' a loop that approximates the points of the orbit'''

    t = 0
    tid_lista = []
    n = 0
    while n < konstant_period:
        hastighet_x.append(hastighet_x[n] - h*GM* stor_x_lista[n]/(np.sqrt(stor_x_lista[n]**2 + stor_y_lista[n]**2))**3)
        stor_x_lista.append(stor_x_lista[n] + h*hastighet_x[n])

        hastighet_y.append(hastighet_y[n] - h*GM*stor_y_lista[n]/(np.sqrt(stor_x_lista[n]**2 + stor_y_lista[n]**2))**3)
        stor_y_lista.append(stor_y_lista[n] + h*hastighet_y[n])

    '''smaller list of points to run faster'''
    if n % p == 0:
            liten_x_lista.append(stor_x_lista[n])
            liten_y_lista.append(stor_y_lista[n])
            tid_lista.append(t)

    n += 1
    t += h
    ''' function that finds the angle'''
    vinkel = []
    siffra = 0
    while siffra < len(liten_x_lista):
        if liten_y_lista[siffra ] >= 0:
            vinkel.append( np.arccos( liten_x_lista[siffra]/np.sqrt( liten_x_lista[siffra]**2 + liten_y_lista[siffra]**2)))
            siffra += 1

        elif liten_y_lista[siffra] < 0 :
            vinkel.append( np.pi + np.arccos( -liten_x_lista[siffra]/np.sqrt( liten_x_lista[siffra]**2 + liten_y_lista[siffra]**2) ))
            siffra += 1

    '''get rid of line to find periodic function'''
    mod_lista = []

    modn = 0
    while modn < len(vinkel):
        mod_lista.append(vinkel[modn] - (2*np.pi*tid_lista[modn])/np.sqrt(a**3))
        modn += 1

    '''make all inputs have period 1'''
    squeeze_tid = []
    squeezen = 0
    while squeezen < len(tid_lista):
        squeeze_tid.append(tid_lista[squeezen]/np.sqrt(a**3))
        squeezen += 1

    del mod_lista[-1:]
    del tid_lista[-1:]
    del squeeze_tid[-1:]

    plt.plot(squeeze_tid,mod_lista)
    plt.title('p(t) där a = ' + str(a) + ' och b = ' + str(b))
    plt.show

Second more split up program (for reference values runs in ≈4:20):

import numpy as np
import matplotlib.pyplot as plt

'''function that generates the points of the orbit'''
def punkt(a,b,H,p):
    GM =  39.5216489684
    x_0 = a + np.sqrt(a**2 - b**2)
    v_0 = np.sqrt(GM*(2/x_0 - 1/a))
    konstant_period = np.sqrt(a**3)*H
    h = 1/H

    '''starting position given by an elliptic orbit '''
    stor_x_lista = [x_0]
    stor_y_lista = [0]

    hastighet_x = [0]
    hastighet_y = [v_0]

    liten_x_lista = []
    liten_y_lista = []

    ''' a loop that approximates the points of the orbit'''
    t = 0
    tid_lista = []
    n = 0
    while n < konstant_period:
        hastighet_x.append(hastighet_x[n] - h*GM* stor_x_lista[n]/(np.sqrt(stor_x_lista[n]**2 + stor_y_lista[n]**2))**3)
        stor_x_lista.append(stor_x_lista[n] + h*hastighet_x[n])

        hastighet_y.append(hastighet_y[n] - h*GM*stor_y_lista[n]/(np.sqrt(stor_x_lista[n]**2 + stor_y_lista[n]**2))**3)
        stor_y_lista.append(stor_y_lista[n] + h*hastighet_y[n])

        '''smaller list of points to run faster'''
        if n % p == 0:
            liten_x_lista.append(stor_x_lista[n])
            liten_y_lista.append(stor_y_lista[n])
            tid_lista.append(t)

        n += 1
        t += h

    return (liten_x_lista,liten_y_lista,tid_lista)

''' function that finds the angle'''

def vinkel(a,b,H,p):
    '''import lists'''
    liten_x_lista = punkt(a,b,H,p)[0]
    liten_y_lista = punkt(a,b,H,p)[1]
    tid_lista = punkt(a,b,H,p)[2]

    '''find the angle'''
    vinkel_lista = []
    siffra = 0
    while siffra < len(liten_x_lista):
        if liten_y_lista[siffra ] >= 0:
            vinkel_lista.append( np.arccos( liten_x_lista[siffra]/np.sqrt( liten_x_lista[siffra]**2 + liten_y_lista[siffra]**2)))
            siffra += 1

        elif liten_y_lista[siffra] < 0 :
            vinkel_lista.append( np.pi + np.arccos( -liten_x_lista[siffra]/np.sqrt( liten_x_lista[siffra]**2 + liten_y_lista[siffra]**2) ))
            siffra += 1
    return (vinkel_lista, tid_lista)

def periodisk(a,b,H,p):
    '''import lists'''
    tid_lista = vinkel(a,b,H,p)[1]
    vinkel_lista = vinkel(a,b,H,p)[0]

    '''get rid of linear line to find p(t)'''
    mod_lista = []

    modn = 0
    while modn < len(vinkel_lista):
        mod_lista.append((vinkel_lista[modn] - (2*np.pi*tid_lista[modn])/np.sqrt(a**3)))
        modn += 1

    '''make all inputs have period 1'''
    squeeze_tid = []
    squeezen = 0
    while squeezen < len(tid_lista):
        squeeze_tid.append(tid_lista[squeezen]/np.sqrt(a**3))
        squeezen += 1

    del mod_lista[-1:]
    del tid_lista[-1:]
    del squeeze_tid[-1:]

    return (squeeze_tid,mod_lista)

'''fixa 3d-punkt av p(a,b) a är konstant b varierar??? '''

def hitta_amp(a):
    x_b = []
    y_b = []
    n_b = 0.1
    while n_b <= a:
        x_b.append(n_b)
        y_b.append(punkt(a,n_b,10**5,10**3))

    return 0

def graf(a,b,H,p):
    plt.plot(periodisk(a,b,H,p)[0],periodisk(a,b,H,p)[1])
    plt.show

I would assume the thing that is going wrong is that the program is running the same, slow code multiple times instead of just running it once and then accessing the data. Is the problem that everything is done locally and nothing is stored globally or is it something else?
Just as a heads up, the only thing I know about programming is basic syntax, I have no clue how to actually write and run programs. I ran all the code in spyder if that affects anything.

Body:
You are calling the functions once per returned list, you should only call them once.

When a method returns multiple variables, (e.g. punkt):

def punkt(a,b,H,p):

    # Here is all your code

    return (liten_x_lista,liten_y_lista,tid_lista)

You must be careful to only call the function once:

   result = punkt(a,b,H,p)
   liten_x_lista = result[0]
   liten_y_lista = result[1]
   tid_lista = result[2]

   # As opposed to:
   liten_x_lista = punkt(a,b,H,p)[0] # 1st call, ignoring results 2 and 3
   liten_y_lista = punkt(a,b,H,p)[1] # 2nd call, ignoring results 1 and 3
   tid_lista = punkt(a,b,H,p)[2] # 3rd call, ignoring results 1 and 2

Note: I would personally not return a list, but use python's unpacking:

def punkt(a,b,H,p):

    # Here is all your code

    return liten_x_lista, liten_y_lista, tid_lista

And you'd access it:

   liten_x_lista, liten_y_lista, tid_lista = punkt(a,b,H,p)


来源: stackexchange

问:
java.lang.IllegalStateException: UserDetailsService is required. with custom provider
I implemented a custom user details service interface so i can pass organizationId with username

```<code>public interface CustomUserDetailsService {

    UserDetails loadUserByUsernameAndOrganizationId(UUID organizationId, String username) throws UsernameNotFoundException;
}
</code>```

and implemented it in a service class:

```<code>@Service("userDetailsService")
public class CustomUserDetailsServiceImpl implements CustomUserDetailsService {

    @Override
    public UserDetails loadUserByUsernameAndOrganizationId(UUID organizationId, String username) throws UsernameNotFoundException {

        //Method implementation

        return UserDetails;
    }
}
</code>```

Then implemented a custom authentication token to allow sending organizationId:

```<code>public class CustomAuthenticationToken extends UsernamePasswordAuthenticationToken {
    private static final long serialVersionUID = 1L;

    private UUID organizationId;

    public CustomAuthenticationToken(Object principal, Object credentials, UUID organizationId) {
        super(principal, credentials);
        this.organizationId = organizationId;
        super.setAuthenticated(false);
    }

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

    public UUID getOrganizationId() {
        return organizationId;
    }
}
</code>```

And a custom authentication filter so i can extract organizationId from request and pass it to CustomAuthenticationToken class:

```<code>public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

    public static final String SPRING_SECURITY_FORM_ORGANIZATION_KEY = "organizationId";

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

        if (!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }

        CustomAuthenticationToken authRequest = getAuthRequest(request);
        setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }

    private CustomAuthenticationToken getAuthRequest(HttpServletRequest request) {
        String username = obtainUsername(request);
        String password = obtainPassword(request);
        UUID organizationId = obtainOrganizationId(request);

        if (username == null) {
            username = "";
        }
        if (password == null) {
            password = "";
        }

        return new CustomAuthenticationToken(username, password, organizationId);
    }

    private UUID obtainOrganizationId(HttpServletRequest request) {
        UUID organizationId = null;
        String organizationIdStr = request.getParameter(SPRING_SECURITY_FORM_ORGANIZATION_KEY);
        if (organizationIdStr != null && !organizationIdStr.trim().isEmpty())
            organizationId = UUID.fromString(organizationIdStr);
        return organizationId;
    }

}
</code>```

And in my custom provider i do next implementations:

```<code>@Component
public class CustomUserDetailsAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private CustomUserDetailsService userDetailsService;
    @Autowired
    private ResourceServerTokenServices tokenServices;

    /**
     * The plaintext password used to perform PasswordEncoder#matches(CharSequence, String)} on when the user is not found to avoid SEC-2056.
     */
    private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword";

    /**
     * The password used to perform {@link PasswordEncoder#matches(CharSequence, String)} on when the user is not found to avoid SEC-2056. This is necessary, because some {@link PasswordEncoder}
     * implementations will short circuit if the password is not in a valid format.
     */
    private String userNotFoundEncodedPassword;

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

        if (authentication.getCredentials() == null) {
            logger.debug("Authentication failed: no credentials provided");
            throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        }

        String presentedPassword = authentication.getCredentials().toString();

        if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
            logger.debug("Authentication failed: password does not match stored value");
            throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        }
    }

    @Override
    protected void doAfterPropertiesSet() throws Exception {
        Assert.notNull(this.userDetailsService, "A UserDetailsService must be set");
        this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD);
    }

    @Override // Note that we must cast the authentication token to our CustomAuthenticationToken.
    protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication;
        UserDetails loadedUser;

        try {
            loadedUser = this.userDetailsService.loadUserByUsernameAndOrganizationId(auth.getOrganizationId(), auth.getPrincipal().toString());
        } catch (UsernameNotFoundException notFound) {
            if (authentication.getCredentials() != null) {
                String presentedPassword = authentication.getCredentials().toString();
                passwordEncoder.matches(presentedPassword, userNotFoundEncodedPassword);
            }
            throw notFound;
        } catch (Exception repositoryProblem) {
            throw new InternalAuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem);
        }

        if (loadedUser == null) {
            throw new InternalAuthenticationServiceException("UserDetailsService returned null, " + "which is an interface contract violation");
        }
        return loadedUser;
    }

}
</code>```

And finally i used my provider inside my WebSecurityConfiguration which extends WebSecurityConfigurerAdapter:

```<code>@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsAuthenticationProvider authProvider;
    @Autowired
    private ServEngineBasicAuthenticationEntryPoint authenticationEntryPoint;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
        auth.authenticationProvider(authProvider);
    }
}
</code>```

Till now everything goes fine and great and no problems at all when i run my springboot application, but i got an error when i tried to call refresh token service /oauth/token?grant_type=refresh_token&refresh_token=MY_REFRESH_TOKEN_DENERATED_BEFORE
and the error is:

```<code>o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: IllegalStateException, UserDetailsService is required.
java.lang.IllegalStateException: UserDetailsService is required.
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:464)
    at org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper.loadUserDetails(UserDetailsByNameServiceWrapper.java:68)
    at org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider.authenticate(PreAuthenticatedAuthenticationProvider.java:103)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175)
    at org.springframework.security.oauth2.provider.token.DefaultTokenServices.refreshAccessToken(DefaultTokenServices.java:150)
    at org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter.getAccessToken(RefreshTokenGranter.java:47)
    at org.springframework.security.oauth2.provider.token.AbstractTokenGranter.grant(AbstractTokenGranter.java:67)
    at org.springframework.security.oauth2.provider.CompositeTokenGranter.grant(CompositeTokenGranter.java:38)
    at org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer$4.grant(AuthorizationServerEndpointsConfigurer.java:583)
    at org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(TokenEndpoint.java:132)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.servlet.resource.ResourceUrlEncodingFilter.doFilter(ResourceUrlEncodingFilter.java:64)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:215)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter.doFilter(OAuth2ClientContextFilter.java:60)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:97)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:747)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)
</code>```

So now how i can pass my custom user details service with its custom load user method by organizationId and username so oauth2 refresh token service uses it instead of default spring userDetailsService which not in use any more by my application because it has only one method to load user by username

BTW i floowed this article to implement spring security extra login fields:
https://www.baeldung.com/spring-security-extra-login-fields

答:
The problem solved by next implementation:

Implementing a CustomPreAuthProvider

```<code>@Component("preAuthProvider")
public class CustomPreAuthProvider extends PreAuthenticatedAuthenticationProvider {

    @Autowired
    private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> userService;

    public CustomPreAuthProvider() {
        super();
    }

    @PostConstruct
    public void init() {
        super.setPreAuthenticatedUserDetailsService(userService);
    }
}
</code>```

Then implemented a CustomPreAuthUserDetailsService which implemented AuthenticationUserDetailsService and inside override loadUserDetails method i called my custom method load user by organizationId and username.

```<code>@Service
public class CustomPreAuthUserDetailsService implements AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> {
    private Logger logger = LoggerFactory.getLogger(CustomPreAuthUserDetailsService.class);

    @Autowired
    private CustomUserDetailsService userDetailsService;

    @Override
    public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException {
        logger.info("CustomPreAuthUserDetailsService.loadUserDetails(PreAuthenticatedAuthenticationToken token) called.");
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken) token.getPrincipal();
        JwtUser jwtUser = (com.skios.ms.authentication.JwtUser) usernamePasswordAuthenticationToken.getPrincipal();

        return userDetailsService.loadUserByUsernameAndOrganizationId(jwtUser.getOrganizationId(), jwtUser.getUsername());
    }
}
</code>```

and finally adding this to AuthenticationManagerBuilder in WebSecurityConfigurerAdapter configurer class:

```<code>@Autowired
@Qualifier("preAuthProvider")
private AuthenticationProvider preAuthProvider;

@Autowired
private CustomUserDetailsAuthenticationProvider authProvider;

@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider).authenticationProvider(preAuthProvider);
}
</code>```

And my problem finally solved and now i can regenerate a new access token using refresh token and load user by custom load method which accepts organizationId and username :)

ContentLicense: CC BY-SA 4.0

Body:
I implemented a custom user details service interface so i can pass organizationId with username

public interface CustomUserDetailsService {

    UserDetails loadUserByUsernameAndOrganizationId(UUID organizationId, String username) throws UsernameNotFoundException;
}

and implemented it in a service class:

@Service("userDetailsService")
public class CustomUserDetailsServiceImpl implements CustomUserDetailsService {

    @Override
    public UserDetails loadUserByUsernameAndOrganizationId(UUID organizationId, String username) throws UsernameNotFoundException {

        //Method implementation

        return UserDetails;
    }
}

Then implemented a custom authentication token to allow sending organizationId:

public class CustomAuthenticationToken extends UsernamePasswordAuthenticationToken {
    private static final long serialVersionUID = 1L;

    private UUID organizationId;

    public CustomAuthenticationToken(Object principal, Object credentials, UUID organizationId) {
        super(principal, credentials);
        this.organizationId = organizationId;
        super.setAuthenticated(false);
    }

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

    public UUID getOrganizationId() {
        return organizationId;
    }
}

And a custom authentication filter so i can extract organizationId from request and pass it to CustomAuthenticationToken class:

public class CustomAuthenticationFilter extends UsernamePasswordAuthenticationFilter {

    public static final String SPRING_SECURITY_FORM_ORGANIZATION_KEY = "organizationId";

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

        if (!request.getMethod().equals("POST")) {
            throw new AuthenticationServiceException("Authentication method not supported: " + request.getMethod());
        }

        CustomAuthenticationToken authRequest = getAuthRequest(request);
        setDetails(request, authRequest);
        return this.getAuthenticationManager().authenticate(authRequest);
    }

    private CustomAuthenticationToken getAuthRequest(HttpServletRequest request) {
        String username = obtainUsername(request);
        String password = obtainPassword(request);
        UUID organizationId = obtainOrganizationId(request);

        if (username == null) {
            username = "";
        }
        if (password == null) {
            password = "";
        }

        return new CustomAuthenticationToken(username, password, organizationId);
    }

    private UUID obtainOrganizationId(HttpServletRequest request) {
        UUID organizationId = null;
        String organizationIdStr = request.getParameter(SPRING_SECURITY_FORM_ORGANIZATION_KEY);
        if (organizationIdStr != null && !organizationIdStr.trim().isEmpty())
            organizationId = UUID.fromString(organizationIdStr);
        return organizationId;
    }

}

And in my custom provider i do next implementations:

@Component
public class CustomUserDetailsAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private CustomUserDetailsService userDetailsService;
    @Autowired
    private ResourceServerTokenServices tokenServices;

    /**
     * The plaintext password used to perform PasswordEncoder#matches(CharSequence, String)} on when the user is not found to avoid SEC-2056.
     */
    private static final String USER_NOT_FOUND_PASSWORD = "userNotFoundPassword";

    /**
     * The password used to perform {@link PasswordEncoder#matches(CharSequence, String)} on when the user is not found to avoid SEC-2056. This is necessary, because some {@link PasswordEncoder}
     * implementations will short circuit if the password is not in a valid format.
     */
    private String userNotFoundEncodedPassword;

    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {

        if (authentication.getCredentials() == null) {
            logger.debug("Authentication failed: no credentials provided");
            throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        }

        String presentedPassword = authentication.getCredentials().toString();

        if (!passwordEncoder.matches(presentedPassword, userDetails.getPassword())) {
            logger.debug("Authentication failed: password does not match stored value");
            throw new BadCredentialsException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", "Bad credentials"));
        }
    }

    @Override
    protected void doAfterPropertiesSet() throws Exception {
        Assert.notNull(this.userDetailsService, "A UserDetailsService must be set");
        this.userNotFoundEncodedPassword = this.passwordEncoder.encode(USER_NOT_FOUND_PASSWORD);
    }

    @Override // Note that we must cast the authentication token to our CustomAuthenticationToken.
    protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
        CustomAuthenticationToken auth = (CustomAuthenticationToken) authentication;
        UserDetails loadedUser;

        try {
            loadedUser = this.userDetailsService.loadUserByUsernameAndOrganizationId(auth.getOrganizationId(), auth.getPrincipal().toString());
        } catch (UsernameNotFoundException notFound) {
            if (authentication.getCredentials() != null) {
                String presentedPassword = authentication.getCredentials().toString();
                passwordEncoder.matches(presentedPassword, userNotFoundEncodedPassword);
            }
            throw notFound;
        } catch (Exception repositoryProblem) {
            throw new InternalAuthenticationServiceException(repositoryProblem.getMessage(), repositoryProblem);
        }

        if (loadedUser == null) {
            throw new InternalAuthenticationServiceException("UserDetailsService returned null, " + "which is an interface contract violation");
        }
        return loadedUser;
    }

}

And finally i used my provider inside my WebSecurityConfiguration which extends WebSecurityConfigurerAdapter:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsAuthenticationProvider authProvider;
    @Autowired
    private ServEngineBasicAuthenticationEntryPoint authenticationEntryPoint;

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
        auth.authenticationProvider(authProvider);
    }
}

Till now everything goes fine and great and no problems at all when i run my springboot application, but i got an error when i tried to call refresh token service /oauth/token?grant_type=refresh_token&refresh_token=MY_REFRESH_TOKEN_DENERATED_BEFORE
and the error is:

o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: IllegalStateException, UserDetailsService is required.
java.lang.IllegalStateException: UserDetailsService is required.
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter$UserDetailsServiceDelegator.loadUserByUsername(WebSecurityConfigurerAdapter.java:464)
    at org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper.loadUserDetails(UserDetailsByNameServiceWrapper.java:68)
    at org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider.authenticate(PreAuthenticatedAuthenticationProvider.java:103)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:175)
    at org.springframework.security.oauth2.provider.token.DefaultTokenServices.refreshAccessToken(DefaultTokenServices.java:150)
    at org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter.getAccessToken(RefreshTokenGranter.java:47)
    at org.springframework.security.oauth2.provider.token.AbstractTokenGranter.grant(AbstractTokenGranter.java:67)
    at org.springframework.security.oauth2.provider.CompositeTokenGranter.grant(CompositeTokenGranter.java:38)
    at org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer$4.grant(AuthorizationServerEndpointsConfigurer.java:583)
    at org.springframework.security.oauth2.provider.endpoint.TokenEndpoint.postAccessToken(TokenEndpoint.java:132)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.servlet.resource.ResourceUrlEncodingFilter.doFilter(ResourceUrlEncodingFilter.java:64)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:320)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:170)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilterInternal(BasicAuthenticationFilter.java:215)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:116)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:74)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:334)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:215)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.security.oauth2.client.filter.OAuth2ClientContextFilter.doFilter(OAuth2ClientContextFilter.java:60)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:94)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114)
    at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.springframework.web.filter.CorsFilter.doFilterInternal(CorsFilter.java:97)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
    at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:747)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)

So now how i can pass my custom user details service with its custom load user method by organizationId and username so oauth2 refresh token service uses it instead of default spring userDetailsService which not in use any more by my application because it has only one method to load user by username

BTW i floowed this article to implement spring security extra login fields:
https://www.baeldung.com/spring-security-extra-login-fields

Body:
The problem solved by next implementation:

Implementing a CustomPreAuthProvider

@Component("preAuthProvider")
public class CustomPreAuthProvider extends PreAuthenticatedAuthenticationProvider {

    @Autowired
    private AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> userService;

    public CustomPreAuthProvider() {
        super();
    }

    @PostConstruct
    public void init() {
        super.setPreAuthenticatedUserDetailsService(userService);
    }
}

Then implemented a CustomPreAuthUserDetailsService which implemented AuthenticationUserDetailsService and inside override loadUserDetails method i called my custom method load user by organizationId and username.

@Service
public class CustomPreAuthUserDetailsService implements AuthenticationUserDetailsService<PreAuthenticatedAuthenticationToken> {
    private Logger logger = LoggerFactory.getLogger(CustomPreAuthUserDetailsService.class);

    @Autowired
    private CustomUserDetailsService userDetailsService;

    @Override
    public UserDetails loadUserDetails(PreAuthenticatedAuthenticationToken token) throws UsernameNotFoundException {
        logger.info("CustomPreAuthUserDetailsService.loadUserDetails(PreAuthenticatedAuthenticationToken token) called.");
        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = (UsernamePasswordAuthenticationToken) token.getPrincipal();
        JwtUser jwtUser = (com.skios.ms.authentication.JwtUser) usernamePasswordAuthenticationToken.getPrincipal();

        return userDetailsService.loadUserByUsernameAndOrganizationId(jwtUser.getOrganizationId(), jwtUser.getUsername());
    }
}

and finally adding this to AuthenticationManagerBuilder in WebSecurityConfigurerAdapter configurer class:

@Autowired
@Qualifier("preAuthProvider")
private AuthenticationProvider preAuthProvider;

@Autowired
private CustomUserDetailsAuthenticationProvider authProvider;

@Override
@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider).authenticationProvider(preAuthProvider);
}

And my problem finally solved and now i can regenerate a new access token using refresh token and load user by custom load method which accepts organizationId and username :)


问:
What is the meaning of this code in service now?
I am usin My requests widget in Server Now.
There is this line of code.

```<code>    var notClosedResolved = 'stateNOT IN6,7';
    var closedResolved = 'stateIN6,7';
</code>```

What that means?

Plase help
Thanks in advance

答:
It's an encoded query. State 6/7 by default are the numeric values of the states stored within ServiceNow.

So 'Resolved' and 'Closed' are the "Display" values (what the end user sees) and 6/7 are the numeric value behind those display values, to allow for easier accessibility when developing.

Effectively, the notClosedResolved/closedResolved indicate that they'll be used when doing some form of query.

For example:

```<code>//The two variables storing the encoded queries.

var notClosedResolved = 'stateNOT IN6,7';;
var closedResolved = 'stateIN6, 7';

//The query being added to the GlideRecord, then queried.
var gr = new GlideRecord('incident');
gr.addEncodedQuery(notClosedResolved);
gr.query();

//If any tickets matching the query (Not closed or resolved) exist, then do whatever is in the while and update the record
while(gr.next()){
    gr.short_description = 'Example - All non-closed/resolved incidents';
    gr.update();
}

//The query being added to the GlideRecord, then queried.
var gr = new GlideRecord('incident');
gr.addEncodedQuery(closedResolved);
gr.query();

//If any tickets matching the query (Closed or resolved) exist, then do whatever is in the while and update the record
while(gr.next()){
    gr.short_description = 'Example - All closed/resolved incidents';
    gr.update();
}
</code>```

Body:
I am usin My requests widget in Server Now.
There is this line of code.

    var notClosedResolved = 'stateNOT IN6,7';
    var closedResolved = 'stateIN6,7';

What that means?

Plase help
Thanks in advance

Body:
It's an encoded query. State 6/7 by default are the numeric values of the states stored within ServiceNow.

So 'Resolved' and 'Closed' are the "Display" values (what the end user sees) and 6/7 are the numeric value behind those display values, to allow for easier accessibility when developing.

Effectively, the notClosedResolved/closedResolved indicate that they'll be used when doing some form of query.

For example:

//The two variables storing the encoded queries.

var notClosedResolved = 'stateNOT IN6,7';;
var closedResolved = 'stateIN6, 7';

//The query being added to the GlideRecord, then queried.
var gr = new GlideRecord('incident');
gr.addEncodedQuery(notClosedResolved);
gr.query();

//If any tickets matching the query (Not closed or resolved) exist, then do whatever is in the while and update the record
while(gr.next()){
    gr.short_description = 'Example - All non-closed/resolved incidents';
    gr.update();
}

//The query being added to the GlideRecord, then queried.
var gr = new GlideRecord('incident');
gr.addEncodedQuery(closedResolved);
gr.query();

//If any tickets matching the query (Closed or resolved) exist, then do whatever is in the while and update the record
while(gr.next()){
    gr.short_description = 'Example - All closed/resolved incidents';
    gr.update();
}


问:
Using RAII to re-allocate a resource after a given number of iterations
I need to process a large number of items. To do this, a resource is required which in this case is an external software program.

Due to memory leaks of the external software it is not possible to process all items with a single instance of the resource. Creating a new instance of the resource is not an option for performance reasons. Therefore I need to process the items in portions of a given size N. This means I create an instance of the resource, process the next N items, then deallocate the resource. This process is repeated until all items have been processed.

Processing of an item is actually a complicated process which can cause different types of failures (exceptions). Therefore I like to use the RAII idiom to manage the resource and make sure each instance is cleanly deallocated.

It is no big deal to implement this with two nested loops as in this example:

```<code>#include <iostream>
#include <vector>

class ResourceAllocator {
public:
    ResourceAllocator() { std::cout << "Allocating resource\n"; }
    ~ResourceAllocator() { std::cout << "Deallocating resource\n"; }
    void Process(int n) { std::cout << "Processing item " << n << "\n"; }
};

int main()
{
    const int totalSize = 20;
    const int portionSize = 6;

    std::vector<int> items(totalSize);
    int count = 0;
    bool complete = false;
    while (!complete) {
        ResourceAllocator a;
        for (int i = 0; i < portionSize; i++) {
            a.Process(items[i]);
            count++;
            if (count >= totalSize) {
                complete = true;
                break;
            }
        }
    }
}
</code>```

However I keep wondering if there is a solution that requires only a single loop.

答:
An alternative would be to use a container for the value and reset it every N iterations.</p>

```<code>#include <iostream>
#include <vector>
#include <optional>

class ResourceAllocator {
public:
    ResourceAllocator() { std::cout << "Allocating resource\n"; }
    ~ResourceAllocator() { std::cout << "Deallocating resource\n"; }
    void Process(int n) { std::cout << "Processing item " << n << "\n"; }
};

int main()
{
    const int totalSize = 20;
    const int portionSize = 6;

    std::vector<int> items(totalSize);
    {
        std::optional<ResourceAllocator> a;
        for (int i = 0; i < items.size(); ++i)
        {
            if (i % portionSize == 0) a.emplace();

            a->Process(items[i]);
        }
    }
}
</code>```

Body:
I need to process a large number of items. To do this, a resource is required which in this case is an external software program.

Due to memory leaks of the external software it is not possible to process all items with a single instance of the resource. Creating a new instance of the resource is not an option for performance reasons. Therefore I need to process the items in portions of a given size N. This means I create an instance of the resource, process the next N items, then deallocate the resource. This process is repeated until all items have been processed.

Processing of an item is actually a complicated process which can cause different types of failures (exceptions). Therefore I like to use the RAII idiom to manage the resource and make sure each instance is cleanly deallocated.

It is no big deal to implement this with two nested loops as in this example:

#include <iostream>
#include <vector>

class ResourceAllocator {
public:
    ResourceAllocator() { std::cout << "Allocating resource\n"; }
    ~ResourceAllocator() { std::cout << "Deallocating resource\n"; }
    void Process(int n) { std::cout << "Processing item " << n << "\n"; }
};

int main()
{
    const int totalSize = 20;
    const int portionSize = 6;

    std::vector<int> items(totalSize);
    int count = 0;
    bool complete = false;
    while (!complete) {
        ResourceAllocator a;
        for (int i = 0; i < portionSize; i++) {
            a.Process(items[i]);
            count++;
            if (count >= totalSize) {
                complete = true;
                break;
            }
        }
    }
}

However I keep wondering if there is a solution that requires only a single loop.

Body:
An alternative would be to use a container for the value and reset it every N iterations.

#include <iostream>
#include <vector>
#include <optional>

class ResourceAllocator {
public:
    ResourceAllocator() { std::cout << "Allocating resource\n"; }
    ~ResourceAllocator() { std::cout << "Deallocating resource\n"; }
    void Process(int n) { std::cout << "Processing item " << n << "\n"; }
};

int main()
{
    const int totalSize = 20;
    const int portionSize = 6;

    std::vector<int> items(totalSize);
    {
        std::optional<ResourceAllocator> a;
        for (int i = 0; i < items.size(); ++i)
        {
            if (i % portionSize == 0) a.emplace();

            a->Process(items[i]);
        }
    }
}


问:
How can I get input from multiple entry widgets created in a loop?
I've hunted through some of the previous answers and got a step closer, but my problem is still that I can not get the value from multiple entry boxes.

```<code>import tkinter as tk
from tkinter import ttk
window = tk.Tk()
my_list = []

def get_info():
    for each_player in my_list:
        tk.Label(window, text=temp_entry.get()).grid()

#number of players is determined by the user.
#In this example, lets say there are 3 players
tk.Label(window, text="Number of Players: ").grid()
num_of_players = ttk.Combobox(window, values=[1, 2, 3])
num_of_players.current(2)
num_of_players.grid(row=0, column=1)
#The code above is only the recreate the user selecting the amount of players from a combobox

#create number of entry boxes for user-determined number of players
for each_player in range(1, int(num_of_players.get()) + 1):
    temp_label = tk.Label(window, text="Player {}: ".format(each_player))
    temp_entry = tk.Entry(window)
    my_list.append(temp_entry)

    temp_label.grid(row=each_player, column=0, pady=10)
    temp_entry.grid(row=each_player, column=1, pady=10)
button = tk.Button(window, text="Save", command=get_info)
button.grid()

window.mainloop()
</code>```

It's at the end of the code where I struggle to find out how I could possibly get information from the entry boxes. How can I use the get() method but only after the user has input text?

答:
Your list contains entry widgets, so in your loop you need to reference the loop variable rather than temp_entry:

```<code>def get_info():
    for each_player in my_list:
        print(each_player.get())
</code>```

Body:
I've hunted through some of the previous answers and got a step closer, but my problem is still that I can not get the value from multiple entry boxes.

import tkinter as tk
from tkinter import ttk
window = tk.Tk()
my_list = []

def get_info():
    for each_player in my_list:
        tk.Label(window, text=temp_entry.get()).grid()

#number of players is determined by the user.
#In this example, lets say there are 3 players
tk.Label(window, text="Number of Players: ").grid()
num_of_players = ttk.Combobox(window, values=[1, 2, 3])
num_of_players.current(2)
num_of_players.grid(row=0, column=1)
#The code above is only the recreate the user selecting the amount of players from a combobox

#create number of entry boxes for user-determined number of players
for each_player in range(1, int(num_of_players.get()) + 1):
    temp_label = tk.Label(window, text="Player {}: ".format(each_player))
    temp_entry = tk.Entry(window)
    my_list.append(temp_entry)

    temp_label.grid(row=each_player, column=0, pady=10)
    temp_entry.grid(row=each_player, column=1, pady=10)
button = tk.Button(window, text="Save", command=get_info)
button.grid()

window.mainloop()

It's at the end of the code where I struggle to find out how I could possibly get information from the entry boxes. How can I use the get() method but only after the user has input text?

Body:
Your list contains entry widgets, so in your loop you need to reference the loop variable rather than temp_entry:

def get_info():
    for each_player in my_list:
        print(each_player.get())


问:
How to create model instances from csv file
There is a task to parse the csv file and create instances in the database based on the received data. On the backend - DRF and at the front - React.
The specific feature is that the file processing is not quite hidden. The logic is as follows:
There is a button to load the file. The file is loaded and validated, but nothing is created in the database at once. A window appears with a list of saved data (like a table) and in this window there is a new button to confirm by clicking on which the database is already requested.

What I just did:
1. Created a class to download the file (Upload button)

```<code>class FileUploadView(APIView):
    parser_classes = ( MultiPartParser, FormParser)
    renderer_classes = [JSONRenderer]

    def put(self, request, format=None):
        if 'file' not in request.data:
            raise ParseError("Empty content")
        f = request.data['file']
        filename = f.name
        if filename.endswith('.csv'):
            file = default_storage.save(filename, f)
            r = csv_file_parser(file)
            status = 204
        else:
            status = 406
            r = "File format error"
        return Response(r, status=status)
</code>```

In the class, the csv_file_parser function is called, the result of which is json containing all the saved data like this:

```<code>{
    "1": {
        "Vendor": "Firstvendortestname",
        "Country": "USA",
         ...
         ...
        "Modules": "Module1",
        " NDA date": "2019-12-24"
    },
    "2": {
        "Vendor": "Secondvendortestname",
        "Country": "Canada",
         ...
         ...
        "Modules": "Module1",
        " NDA date": "2019-12-24"
    }
}
</code>```

This data will be used to preview the fields from which the model instaces will be created in the base by clicking on the Confirm button.

csv_file_parser function

```<code>def csv_file_parser(file):
    result_dict = {}
    with open(file) as csvfile:
        reader = csv.DictReader(csvfile)
        line_count = 1
        for rows in reader:
            for key, value in rows.items():
                if not value:
                    raise ParseError('Missing value in file. Check the {} line'.format(line_count))
            result_dict[line_count] = rows
            line_count += 1

    return result_dict
</code>```

When the Confirm button is pressed, React passes this data as an argument to a class that works with the database using the POST method. With the implementation of this class, I have difficulties. How to correctly process the received data and record it in the database?

```<code>class CsvToDatabase(APIView):

    def post(self, request, format=None):
        data = request.data
        for vendor in data:
            Vendors(
                vendor_name=vendor['Vendor'],
                country=vendor['Country']
            ).save()

        return Response({'received data': request.data})
</code>```

This code gives error

```<code>TypeError at /api/v1/vendors/from_csv_create/
string indices must be integers
</code>```

Printing of request.data output

```<code><QueryDict: {'{\n    "1": {\n        "Vendor": "Firstvendortestname",\n        "Country": "USA",\n        "Primary Contact Name": "Jack Jhonson",\n        "Primary Contact Email": "jack@gmail.com",\n        "Secondary Contact Name": "Jack2 Jhonson",\n        "Secondary Contact Email": "jack2@gmail.com",\n        "Modules": "Module1, Module2",\n        " NDA date": "2019-12-24"\n    },\n    "2": {\n        "Vendor": "Secondvendortestname",\n        "Country": "Canada",\n        "Primary Contact Name": "Sandra Bullock",\n        "Primary Contact Email": "sandra@gmail.com",\n        "Secondary Contact Name": "Sandra Bullock",\n        "Secondary Contact Email": "sandra@gmail.com",\n        "Modules": "Module1, Module2",\n        " NDA date": "2019-12-24"\n    }\n}': ['']}>
</code>```

Maybe I'm using the wrong data format?

And overall, I have a feeling that I'm doing the job the wrong way. I don't use serializers, do I need them here?

答:
You iterating over dict key, but should iterate over items:</p>

```<code>   for key, vendor in data.items():
        Vendors(
            vendor_name=vendor['Vendor'],
            country=vendor['Country']
        ).save()
</code>```

Body:
There is a task to parse the csv file and create instances in the database based on the received data. On the backend - DRF and at the front - React.
The specific feature is that the file processing is not quite hidden. The logic is as follows:
There is a button to load the file. The file is loaded and validated, but nothing is created in the database at once. A window appears with a list of saved data (like a table) and in this window there is a new button to confirm by clicking on which the database is already requested.

What I just did:
1. Created a class to download the file (Upload button)

class FileUploadView(APIView):
    parser_classes = ( MultiPartParser, FormParser)
    renderer_classes = [JSONRenderer]

    def put(self, request, format=None):
        if 'file' not in request.data:
            raise ParseError("Empty content")
        f = request.data['file']
        filename = f.name
        if filename.endswith('.csv'):
            file = default_storage.save(filename, f)
            r = csv_file_parser(file)
            status = 204
        else:
            status = 406
            r = "File format error"
        return Response(r, status=status)

In the class, the csv_file_parser function is called, the result of which is json containing all the saved data like this:

{
    "1": {
        "Vendor": "Firstvendortestname",
        "Country": "USA",
         ...
         ...
        "Modules": "Module1",
        " NDA date": "2019-12-24"
    },
    "2": {
        "Vendor": "Secondvendortestname",
        "Country": "Canada",
         ...
         ...
        "Modules": "Module1",
        " NDA date": "2019-12-24"
    }
}

This data will be used to preview the fields from which the model instaces will be created in the base by clicking on the Confirm button.

csv_file_parser function

def csv_file_parser(file):
    result_dict = {}
    with open(file) as csvfile:
        reader = csv.DictReader(csvfile)
        line_count = 1
        for rows in reader:
            for key, value in rows.items():
                if not value:
                    raise ParseError('Missing value in file. Check the {} line'.format(line_count))
            result_dict[line_count] = rows
            line_count += 1

    return result_dict

When the Confirm button is pressed, React passes this data as an argument to a class that works with the database using the POST method. With the implementation of this class, I have difficulties. How to correctly process the received data and record it in the database?

class CsvToDatabase(APIView):

    def post(self, request, format=None):
        data = request.data
        for vendor in data:
            Vendors(
                vendor_name=vendor['Vendor'],
                country=vendor['Country']
            ).save()

        return Response({'received data': request.data})

This code gives error

TypeError at /api/v1/vendors/from_csv_create/
string indices must be integers

Printing of request.data output

<QueryDict: {'{\n    "1": {\n        "Vendor": "Firstvendortestname",\n        "Country": "USA",\n        "Primary Contact Name": "Jack Jhonson",\n        "Primary Contact Email": "jack@gmail.com",\n        "Secondary Contact Name": "Jack2 Jhonson",\n        "Secondary Contact Email": "jack2@gmail.com",\n        "Modules": "Module1, Module2",\n        " NDA date": "2019-12-24"\n    },\n    "2": {\n        "Vendor": "Secondvendortestname",\n        "Country": "Canada",\n        "Primary Contact Name": "Sandra Bullock",\n        "Primary Contact Email": "sandra@gmail.com",\n        "Secondary Contact Name": "Sandra Bullock",\n        "Secondary Contact Email": "sandra@gmail.com",\n        "Modules": "Module1, Module2",\n        " NDA date": "2019-12-24"\n    }\n}': ['']}>

Maybe I'm using the wrong data format?

And overall, I have a feeling that I'm doing the job the wrong way. I don't use serializers, do I need them here?

Body:
You iterating over dict key, but should iterate over items:

   for key, vendor in data.items():
        Vendors(
            vendor_name=vendor['Vendor'],
            country=vendor['Country']
        ).save()


问:
GroupBy and Change the values of one columns
dataframe

Hi dear coders, I need help from you as I don't know how to deal with it.
As you can see on my dataframe i have a column description and a column title.
I want for a same description , my title to be all the same.
I want to groupby description and give to "title" of this group all the same value.
By default, the first title value for each description group.

for exemple, for the description "L'eau de cologne, symbole incontournable..." I want all the titles to be "Eau de cologne Les exclusifs de Chanel 75ml..." (as its the first value)
The value "Eau de cologne Les exclusifs de Chanel 200ml..." will then be replaced by "Eau de cologne Les exclusifs de Chanel 75ml..."

Sorry I don't know if i explained it well

答:
Here's a solution with some dummy data using pandas.DataFrame.transform:

```<code>import pandas as pd

df = pd.DataFrame({'title': ['t1', 't2', 't3', 't4', 't5'],
                   'description': ['d1', 'd1', 'd1', 'd2', 'd2']})

  description title
0          d1    t1
1          d1    t2
2          d1    t3
3          d2    t4
4          d2    t5
</code>```

We expect to replace t2 and t3 with t1, and t5 with t4.

Code:

```<code>df['first_title'] = df.groupby('description').transform("first")

print(df)
</code>```

Output:

```<code>  description title first_title
0          d1    t1          t1
1          d1    t2          t1
2          d1    t3          t1
3          d2    t4          t4
4          d2    t5          t4
</code>```

Body:
Here's a solution with some dummy data using pandas.DataFrame.transform:

import pandas as pd

df = pd.DataFrame({'title': ['t1', 't2', 't3', 't4', 't5'],
                   'description': ['d1', 'd1', 'd1', 'd2', 'd2']})

  description title
0          d1    t1
1          d1    t2
2          d1    t3
3          d2    t4
4          d2    t5

We expect to replace t2 and t3 with t1, and t5 with t4.

Code:

df['first_title'] = df.groupby('description').transform("first")

print(df)

Output:

  description title first_title
0          d1    t1          t1
1          d1    t2          t1
2          d1    t3          t1
3          d2    t4          t4
4          d2    t5          t4


问:
Copy Reactor Context to MDC in the case of TimeoutMainSubscriber
I have been following the code at https://github.com/archie-swif/webflux-mdc/blob/master/src/main/java/com/example/webfluxmdc/MdcContextLifter.java to put data from Reactor Context to MDC using hooks.

The solution works perfectly in most of the cases except for when Mono is timing out and TimeoutMainSubscriber is in use.

Below is the small test that I used.

```<code>Mono<Integer> integerMono = Mono.just(1)
            .doOnEach(logger.addToContext(i -> ImmutableMap.of("index", i)))
            .flatMap(___ -> Mono.fromSupplier(() -> {
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    return 0;
                }
                return 1;
            }))
            .timeout(Duration.ofMillis(5000), Schedulers.parallel())
            .doOnEach(logger.info("testMDCLogging"))
            .doOnError(logger.error("testMDCErrorLogging"))
            .subscriberContext(logger.initContext());
</code>```

When verifying the propagation of Context through MDC lifters, onNext() onError() and onComplete() are not called in TimeoutMainSubscriber (where the Context is copied over to MDC), only currentContext() is called.

How do I propagate the Context and copy over to MDC to be used in doOnError and doOnEach in the case of Mono timeout? I don't want to move the copy to currentContext() because this method is called several times and it is not an optimal solution in my opinion.

答:
Did you try overriding onError method in MdcContextLifter class?

```<code>    @Override
    public void onError(Throwable t) {
        copyToMdc(coreSubscriber.currentContext()); // similar to onNext()
        coreSubscriber.onError(t);
    }
</code>```

Body:
I have been following the code at https://github.com/archie-swif/webflux-mdc/blob/master/src/main/java/com/example/webfluxmdc/MdcContextLifter.java to put data from Reactor Context to MDC using hooks.

The solution works perfectly in most of the cases except for when Mono is timing out and TimeoutMainSubscriber is in use.

Below is the small test that I used.

Mono<Integer> integerMono = Mono.just(1)
            .doOnEach(logger.addToContext(i -> ImmutableMap.of("index", i)))
            .flatMap(___ -> Mono.fromSupplier(() -> {
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    return 0;
                }
                return 1;
            }))
            .timeout(Duration.ofMillis(5000), Schedulers.parallel())
            .doOnEach(logger.info("testMDCLogging"))
            .doOnError(logger.error("testMDCErrorLogging"))
            .subscriberContext(logger.initContext());

When verifying the propagation of Context through MDC lifters, onNext() onError() and onComplete() are not called in TimeoutMainSubscriber (where the Context is copied over to MDC), only currentContext() is called.

How do I propagate the Context and copy over to MDC to be used in doOnError and doOnEach in the case of Mono timeout? I don't want to move the copy to currentContext() because this method is called several times and it is not an optimal solution in my opinion.

Body:
Did you try overriding onError method in MdcContextLifter class?

    @Override
    public void onError(Throwable t) {
        copyToMdc(coreSubscriber.currentContext()); // similar to onNext()
        coreSubscriber.onError(t);
    }


问:
Fetch googlesheet data using access token?
I want to fetch googlesheet data. Through my code I am able to get the access token but now I don't know how can I fetch all the records in a googlesheet using that particular access token in javascript.
I have given the scope in manifest as spreadsheet.readonly.
I have my token in a particular variable and also googlesheeetId in another variable now what headers and what url do I need to paas so that I can get the googlesheet data.
Please help with some reference or links or sample code so that I can get a glimpse of what process do I need to follow to do this.

Here is the code

```<code>var gSheetId;

chrome.identity.getAuthToken({ 'interactive': true }, function (token) {
  if (chrome.runtime.lastError) {
    console.log(chrome.runtime.lastError);
    return;
  }
  console.log(token);

  //getting googlesheetId from user
  gSheetId = prompt("Please provide your googlesheet Id");
  if (!gSheetId) {
    alert("Operation Cannot be Performed! Please refresh");
  }
  else {
    //read googlesheetdata
    var headers = {
      'Authorization':'Bearer '+token
    }
    //here I need to read the googlesheet data using the token
    chrome.debugger.onEvent.addListener(onEvent);
  }
});
</code>```

Thanks in advance!

答:
Solution

Brought to you by @StayAtOrbit , owner of the oiginal question. (this is a community wiki answer)

Useful resources for this issue:

Google sheets API documentation for JavaScript

Sending access token to an API

Making authentificated requests with a token

var gSheetId;

chrome.identity.getAuthToken({ 'interactive': true }, function (token) {
  if (chrome.runtime.lastError) {
    console.log(chrome.runtime.lastError);
    return;
  }
  console.log(token);

  //getting googlesheetId from user
  gSheetId = prompt("Please provide your googlesheet Id");
  if (!gSheetId) {
    alert("Operation Cannot be Performed! Please refresh");
  }
  else { //read googlesheetdata
    var obj = {
      method: 'GET',
      headers: {
       'Accept': 'application/json',
       'Content-Type': 'application/json',
       'Host': 'sheets.googleapis.com',
       'Authorization': 'Bearer '+token
      }
    }
 fetch('https://sheets.googleapis.com/v4/spreadsheets/<sheetID>/values/<range>', obj)
.then(function (res) {
return console.log(res.json());
}).then(function (resJson) {
return console.log(resJson);
})
    //here I need to read the googlesheet data using the token
    chrome.debugger.onEvent.addListener(onEvent);
  }
});```

Body:
I want to fetch googlesheet data. Through my code I am able to get the access token but now I don't know how can I fetch all the records in a googlesheet using that particular access token in javascript.
I have given the scope in manifest as spreadsheet.readonly.
I have my token in a particular variable and also googlesheeetId in another variable now what headers and what url do I need to paas so that I can get the googlesheet data.
Please help with some reference or links or sample code so that I can get a glimpse of what process do I need to follow to do this.

Here is the code

var gSheetId;

chrome.identity.getAuthToken({ 'interactive': true }, function (token) {
  if (chrome.runtime.lastError) {
    console.log(chrome.runtime.lastError);
    return;
  }
  console.log(token);

  //getting googlesheetId from user
  gSheetId = prompt("Please provide your googlesheet Id");
  if (!gSheetId) {
    alert("Operation Cannot be Performed! Please refresh");
  }
  else {
    //read googlesheetdata
    var headers = {
      'Authorization':'Bearer '+token
    }
    //here I need to read the googlesheet data using the token
    chrome.debugger.onEvent.addListener(onEvent);
  }
});

Thanks in advance!


问:
Transposing first column in dataframe in pandas
I am fairly new to Python and I just do pretty basic stuff. So when I managed to sort this out by myself I was really chuffed, although I am not sure if this is the most pythonic way to do it.
I had a csv file that contained this information when read the normal way:

I wanted the items in the first-row to be the column headers.

答:
So I used Transpose:

```<code>df_t=df.T
</code>```

And save it as a new file, without the header to remove the existing headings.

```<code>df_t.to_csv('employment.csv)
</code>```

When opening the new file, this is what I have:

As I said, not the most pythonic, but it seems to work. Any suggestions on how to improve this, will be most appreciated.

Body:
So I used Transpose:

df_t=df.T

And save it as a new file, without the header to remove the existing headings.

df_t.to_csv('employment.csv)

When opening the new file, this is what I have:

As I said, not the most pythonic, but it seems to work. Any suggestions on how to improve this, will be most appreciated.


问:
ERROR TypeError: Cannot read property '__source' of undefined
Since I upgrade to the new spartacus version (1.5) I've seen this bug in the console. It's came from SiteContextComponentService.getInjectedService.

I have not implemented nor used yet this service. How I can fix this bug? Cause I strongly believe is the origin of other little problems in my develop environment.

答: If you use 'CMSSiteContextComponent' component type (flextype?), then it should come with the context cms component data from the backend. Please check if the response contains it.


问:
FileSystemObject.FileExists returns false even if I map server path
The FileExists object keeps returning false even if I use the following:

```<code>If fso.fileExists(Server.MapPath(thumbfile)) Then %><img src="<%=thumbfile%>" /><% Else %><img src="./images/no-pic.gif" /><% End If %></div>
</code>```

I am able to navigate to it locally. It is hosted on IIS6, what permissions are required on the server mapped directory?

To debug I did this:

```<code><% Response.Write (Server.MapPath(thumbfile)) %>
</code>```

This returned (which is valid):

```<code>\\101.101.101.123\docs\729\pic.jpg
</code>```

The files are located on server 101.101.101.123 but IIS is on server 101.101.101.124

FileExists is still returning false.

```<code><% Response.Write (thumbfile) %>
</code>```

returns:

```<code>pdf/729/pic.jpg
</code>```

Where PDF is a VIRTUAL DIRECTORY mapped to the listed path with Read/Write/Execute

```<code>\\101.101.101.123\docs
</code>```

答:
If you're using a UNC path mapped to a drive letter then this won't work. Although your interactive account has the drive letter mapped, the account that the website runs under most likely won't.

Mapped drive letters are account specific.

ContentLicense: CC BY-SA 3.0

Body:
The FileExists object keeps returning false even if I use the following:

If fso.fileExists(Server.MapPath(thumbfile)) Then %><img src="<%=thumbfile%>" /><% Else %><% End If %>

I am able to navigate to it locally. It is hosted on IIS6, what permissions are required on the server mapped directory?

To debug I did this:

<% Response.Write (Server.MapPath(thumbfile)) %>

This returned (which is valid):

\\101.101.101.123\docs\729\pic.jpg

The files are located on server 101.101.101.123 but IIS is on server 101.101.101.124

FileExists is still returning false.

<% Response.Write (thumbfile) %>

returns:

pdf/729/pic.jpg

Where PDF is a VIRTUAL DIRECTORY mapped to the listed path with Read/Write/Execute

\\101.101.101.123\docs