可能是最详细的Spring Cloud OAuth2实现单点登录
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
// 其他配置 ...
.oauth2Login()
.successHandler(new SimpleUrlAuthenticationSuccessHandler("/dashboard"))
// 其他配置 ...
;
}
@Component
public static class AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Authentication authentication) {
// 自定义登录成功后的行为
}
}
}
这个代码示例展示了如何在Spring Security中使用OAuth 2.0进行登录,并提供了一个自定义的登录成功处理器AuthenticationSuccessHandler
。在configure
方法中,我们使用了.oauth2Login()
来启用OAuth 2.0登录,并通过.successHandler()
指定了登录成功后要跳转的URL以及自定义的登录成功处理器。
评论已关闭