【发布时间】:2020-02-23 08:16:21
【问题描述】:
我正在练习 Spring-Boot 2.2 + Spring Security 5 + Oauth2。按照很多例子,我在这里碰壁了。
我的spring安全配置是这样的:
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/", "/css/**", "/webjars/**").permitAll()
.anyRequest().authenticated()
.and().oauth2Login().userInfoEndpoint()
.userService(new DefaultOAuth2UserService());
}
我有一个使用这种方法的控制器:
@GetMapping("/create")
public ModelAndView create(Principal principal) {
log.debug(principal);
return new ModelAndView("create.html", "topicForm", new TopicForm());
}
在 Thymeleaf 模板中,我会调用 User,它只返回一个数字。
在debug中,authentication是org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken,Principal是DefaultOidcUser,name属性是“sub”,在google的oauth响应中不是名字而是数字。
DefaultOAuth2UserService 在控制器中的断点命中之前永远不会被调用。
我在哪里走错了路?
--编辑-- 在进一步调试中,我认为问题源于 OAuth2LoginAuthenticationFilter 调用 org.springframework.security.oauth2.client.oidc.userinfo.OidcUserService 可以通过 oidcUserService(oidcUserService()) 进行配置
【问题讨论】:
标签: spring-boot spring-security oauth-2.0