【问题标题】:Spring MVC + security + OAuth2 userInfoEndpoint() not workSpring MVC + security + OAuth2 userInfoEndpoint() 不起作用
【发布时间】: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


    【解决方案1】:

    您可以尝试在 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()
    

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 2014-07-27
      • 2018-06-02
      • 2014-08-31
      • 2018-02-05
      • 2018-09-20
      • 1970-01-01
      • 2018-04-22
      • 2019-09-25
      相关资源
      最近更新 更多