401 null oauth2 Google на весенней загрузке - PullRequest
0 голосов
/ 22 ноября 2018

Я хотел добавить google oayut2 в мое клиентское приложение crud.Взаимодействие с сервером происходит с помощью resttemplate.Я зарегистрировал проект в консоли Google, добавил в приложение @ EnableOAuth2Sso, свойство и даже фильтр (хотя это кажется возможным без него)

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

private OAuth2ClientContext oauth2ClientContext;
private AuthorizationCodeResourceDetails authorizationCodeResourceDetails;
private ResourceServerProperties resourceServerProperties;

@Autowired
public void setOauth2ClientContext(OAuth2ClientContext oauth2ClientContext) {
    this.oauth2ClientContext = oauth2ClientContext;
}

@Autowired
public void setResourceServerProperties(ResourceServerProperties resourceServerProperties) {
    this.resourceServerProperties = resourceServerProperties;
}

@Autowired
private CustomAuthenticationSuccessHandler customAuthenticationSuccessHandler;

@Bean
public RestTemplate rest() {
    return new RestTemplate();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    CharacterEncodingFilter filter = new CharacterEncodingFilter();
    filter.setEncoding("UTF-8");
    filter.setForceEncoding(true);
    http
            .authorizeRequests()
            .antMatchers("/user/**").hasAnyAuthority("USER")
            .antMatchers("/admin/**").hasAnyAuthority("ADMIN")
            .and()
            .formLogin()
            .loginPage("/")
            .failureUrl("/?error")
            .successHandler(customAuthenticationSuccessHandler) 
            .usernameParameter("username")
            .passwordParameter("password")
            .and()
            // Setting the filter for the URL "/google/login".
            .addFilterBefore(filter(), BasicAuthenticationFilter.class)
            .csrf().disable();
}

@Bean
public static NoOpPasswordEncoder passwordEncoder() {
    return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}

/*This method for creating filter for OAuth authentication.*/
private OAuth2ClientAuthenticationProcessingFilter filter() {
    //Creating the filter for "/google/login" url
    OAuth2ClientAuthenticationProcessingFilter oAuth2Filter = new OAuth2ClientAuthenticationProcessingFilter(
            "/login/google");

    //Creating the rest template for getting connected with OAuth service.
    //The configuration parameters will inject while creating the bean.
    OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(authorizationCodeResourceDetails,
            oauth2ClientContext);
    oAuth2Filter.setRestTemplate(oAuth2RestTemplate);

    // Setting the token service. It will help for getting the token and
    // user details from the OAuth Service.
    oAuth2Filter.setTokenServices(new UserInfoTokenServices(resourceServerProperties.getUserInfoUri(),
            resourceServerProperties.getClientId()));

    return oAuth2Filter;
   }
}

свойство google:

spring.security.oauth2.client.registration.google.client-id=170427894383- 
io56an8cpvrp9ucj301vo06qb9m67fgn.apps.googleusercontent.com
spring.security.oauth2.client.registration.google.client-secret=lk8OH3po6- 
MXTjQ0zE_Aqv1x
spring.security.oauth2.client.registration.google.scope=profile,email,openid
spring.security.oauth2.client.registration.google.client-authentication- 
method=form
spring.security.oauth2.client.provider.google.authorization- 
uri=https://accounts.google.com/o/oauth2/auth
spring.security.oauth2.client.provider.google.token- 
uri=https://www.googleapis.com/oauth2/v4/token
spring.security.oauth2.client.provider.google.user-info- 
uri=https://www.googleapis.com/oauth2/v3/userinfo
spring.security.oauth2.client.provider.google.user-name-attribute=sub

в html:

  <div>
        With Google: <a href="/admin">click here</a>
  </div>

При попытке войти в систему, 401 null немедленно сбрасывается.Ничего не помогает

...