我正在尝试以Spring MVC +安全性+ 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);
}
}
您可以尝试在SecurityConfig.java中更改这样的顺序吗?
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.oauth2Login()
.userInfoEndpoint()
.userService(customOAuth2UserService)
.and()
.authorizeRequests()
.antMatchers("/*").permitAll()
.and()
.logout()
.logoutUrl("/logout")
.logoutSuccessUrl("/")
.and()
.successHandler(new SuccessHandler());
}
因为下面的代码允许所有URL,因此永远无法访问oauth2Login()代码。
.authorizeRequests()
.antMatchers("/*").permitAll()