首先,对我的英语不好道歉。我会尽力的。
我正在尝试使用Spring Security OAuth2(2.3.6.RELEASE重要!)和Google API实施身份验证代码。我遵循了本教程https://spring.io/guides/tutorials/spring-boot-oauth2/,但是当该指南在OAuth2ClientAuthenticationProcessingFilter中配置UserInfoTokenServices时,我陷入了困境,因为我的依赖项中没有UserInfoTokenServices。
我已经在google中搜索过,但是找不到一个我可以理解的简单示例,在这些示例中,他们使用的UserInfoTokenServices来自比我使用的版本更早的版本。我不知道我是否遵循正确的路径,或者相反,我的方法是否完全错误。我已经查看了https://projects.spring.io/spring-security-oauth/docs/oauth2.html和Spring https://github.com/spring-projects/spring-security-oauth/tree/master/samples/oauth2的gitHub,但找不到帮助我的示例。 我的代码的相关部分:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
@SpringBootApplication
@RestController
@EnableOAuth2Client
public class Application extends WebSecurityConfigurerAdapter {
@Autowired
OAuth2ClientContext oAuth2ClientContext;
@RequestMapping("/user")
public Principal user(Principal principal) {return principal;}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.antMatcher("/**")
.authorizeRequests()
.antMatchers("/", "/login**", "/webjars/**", "/error**")
.permitAll()
.anyRequest()
.authenticated()
.and().logout().logoutSuccessUrl("/").permitAll()
.and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.and().addFilterBefore(ssoGoogleFilter(), BasicAuthenticationFilter.class);
}
public Filter ssoGoogleFilter() {
OAuth2ClientAuthenticationProcessingFilter googleFilter =
new OAuth2ClientAuthenticationProcessingFilter("/google/login");
OAuth2RestTemplate googleOAuth2RestTemplate = new OAuth2RestTemplate(googleAuthorizationCodeResourceDetails(),
oAuth2ClientContext);
googleFilter.setRestTemplate(googleOAuth2RestTemplate);
googleFilter.setTokenServices(/*????????*/);
return googleFilter;
}
@Bean
@ConfigurationProperties("google.client")
public AuthorizationCodeResourceDetails googleAuthorizationCodeResourceDetails() {
return new AuthorizationCodeResourceDetails();
}
@Bean
public FilterRegistrationBean<OAuth2ClientContextFilter> oAuth2ClientContextFilterRegistration(OAuth2ClientContextFilter filter) {
FilterRegistrationBean<OAuth2ClientContextFilter> registrarion = new FilterRegistrationBean<OAuth2ClientContextFilter>();
registrarion.setFilter(filter);
registrarion.setOrder(-200);
return registrarion;
}
public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
application.yml
google:
client:
clientId: <--My ClientId->
clientSecret: <--My Client Secret-->
accessTokenUri: https://oauth2.googleapis.com/token
userAuthorizationUri: https://accounts.google.com/o/oauth2/v2/auth
clientAuthenticationScheme: form
scope:
- openid
- email
- profile
resource:
userInfoUri: https://openidconnect.googleapis.com/v1/userinfo
preferTokenInfo: true
到目前为止,我已经能够获取令牌,但是OAuth2ClientAuthenticationProcessingFilter中的tokenServices属性为null,这将导致loadAuthentication(..)方法引发异常。
任何帮助或指南将不胜感激。