【发布时间】:2020-07-17 09:47:10
【问题描述】:
我正在尝试以 Spring MVC + security + OAuth2 的身份登录。
当前认证状态接收成功,可以获取用户信息。
我想获取用户信息,所以我写了userInfoEndpoint,但根本没有调用userInfoEndPoint。
另一方面,successHandler 被成功调用,并且身份验证具有用户信息。
所以我有两个问题
首先,为什么根本不调用userInfoEndpoint?
在Spring Boot案例中,调用成功
二、如何从successHandler的认证中获取用户信息?
successHandler的认证只有这个方法。
Object getCredentials();
Object getDetails();
Object getPrincipal();
boolean isAuthenticated();
void setAuthenticated(boolean var1) throws IllegalArgumentException;
——源代码——
SecurityConfig.java
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/*").permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/")
.and()
.oauth2Login()
.userInfoEndpoint()
.userService(customOAuth2UserService)
.and()
.successHandler(new SuccessHandler());
}
SuccessHandler.java
@Log4j
public class SuccessHandler implements AuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
Authentication authentication) throws IOException, ServletException {
log.info(authentication);
}
}
【问题讨论】:
标签: java spring security model-view-controller oauth-2.0