Spring Security OAuth2 SSO with Custom provider + logout



我正在尝试使用Spring-boot和Dave Syer示例使用Spring Security Oauth2实现sso

我想使用我的自定义服务器提供程序,它工作得很好。

对于客户端,我希望用户被认证(因此重定向到OAuth2 url),当他们试图访问客户端网站(例如localhost:8080/),并重定向回index.html文件一旦认证。我还想实现登出时,用户在一个链接在index.html文件。

我已经提出了以下客户端sso client:

<>之前包org.ikane;进口java.io.IOException;进口java.security.Principal;进口java.util.Arrays;进口javax.servlet.Filter;进口javax.servlet.FilterChain;进口javax.servlet.ServletException;进口javax.servlet.http.Cookie;进口javax.servlet.http.HttpServletRequest;进口javax.servlet.http.HttpServletResponse;进口org.apache.commons.lang3.StringUtils;进口org.slf4j.Logger;进口org.slf4j.LoggerFactory;进口org.springframework.boot.CommandLineRunner;进口org.springframework.boot.SpringApplication;进口org.springframework.boot.autoconfigure.SpringBootApplication;进口org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;进口org.springframework.context.ConfigurableApplicationContext;进口org.springframework.core.env.ConfigurableEnvironment;进口org.springframework.security.config.annotation.web.builders.HttpSecurity;进口org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;进口org.springframework.security.core.Authentication;进口org.springframework.security.core.context.SecurityContext;进口org.springframework.security.core.context.SecurityContextHolder;进口org.springframework.security.web.csrf.CsrfFilter;进口org.springframework.security.web.csrf.CsrfToken;进口org.springframework.security.web.csrf.CsrfTokenRepository;进口org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository;进口org.springframework.stereotype.Component;进口org.springframework.stereotype.Controller;进口org.springframework.web.bind.annotation.RequestMapping;进口org.springframework.web.bind.annotation.ResponseBody;进口org.springframework.web.filter.OncePerRequestFilter;进口org.springframework.web.util.WebUtils;@SpringBootApplication@ controllerdemosooauth2clientapplication实现CommandLineRunner {private static final Logger = LoggerFactory.getLogger(demosooauth2clientapplication .class);@Override公共无效运行(字符串…arg0)抛出异常{SecurityContext = securitycontexholder . getcontext ();尝试{认证认证= securityContext.getAuthentication();.toString logger.info (authentication.getDetails () ());SecurityContextHolder.clearContext ();} catch(异常e) {记录器。错误("错误",e);}}public static void main(String[] args) {ConfigurableApplicationContext applicationContext = SpringApplication.run(demosooauth2clientapplication .class, args);ConfigurableEnvironment environment = applicationcontext . getenenvironment ();logger.info(" n thttp://localhost: {} {} n tProfiles: {} n",StringUtils.defaultIfEmpty (env.getProperty("server.port")、"8080"),StringUtils.defaultIfEmpty (env.getProperty("server.contextPath"),"/"),Arrays.toString (env.getActiveProfiles ()));}@RequestMapping (value = "/")public String home() {返回"指数";}@RequestMapping (value = "/用户")@ResponseBodypublic主体用户(主体用户){返回用户;}/* *类OAuthConfiguration设置OAuth2单点登录*配置和与之相关的web安全。*/@ component@ controller@EnableOAuth2Sso受保护的静态类OAuthClientConfiguration扩展WebSecurityConfigurerAdapter {CSRF_COOKIE_NAME = "XSRF-TOKEN";CSRF_ANGULAR_HEADER_NAME = "X-XSRF-TOKEN";@OverrideHttpSecurity (http安全)抛出异常{http.antMatcher("/* *").authorizeRequests ().antMatchers("/index . html","/").permitAll () .anyRequest ().csrf .authenticated (), () () .csrfTokenRepository (csrfTokenRepository ()), () .addFilterAfter (csrfHeaderFilter (), CsrfFilter.class);}private Filter csrfHeaderFilter() {返回新的OncePerRequestFilter() {@Override(HttpServletRequest请求,httpservleresponse响应,FilterChain FilterChain)抛出ServletException, IOException {csrf = (CsrfToken) request.getAttribute(CsrfToken.class. getname ());If (csrf != null) {Cookie = webbutils。getCookie(请求,CSRF_COOKIE_NAME);字符串token = csrf.getToken();如果(cookie == null || tokentoken.equals(cookie.getValue())) {cookie = new cookie (CSRF_COOKIE_NAME, token);cookie.setPath("/");response.addCookie(饼干);}}filterChain。doFilter(请求、响应);}};}/* ** Angular将CSRF令牌发送到一个名为"X-XSRF-TOKEN"的自定义头中。*而不是Spring安全期望的默认"X-CSRF-TOKEN"。*因此,我们现在告诉Spring安全期望令牌在* "X-XSRF-TOKEN"报头。

**这个自定义被添加到csrf()

过滤器。** @return*/private CsrfTokenRepository CsrfTokenRepository () {HttpSessionCsrfTokenRepository存储库= new HttpSessionCsrfTokenRepository();repository.setHeaderName (CSRF_ANGULAR_HEADER_NAME);返回存储库;}}}你可以找到一个GitHub源代码。对于如何实现这个用例有什么提示吗?

Thanks in advance

  • 要使您的客户端应用程序重定向到授权服务器只需添加注释@EnableOAuth2Sso在你的WebSecurityConfigurerAdapter和在属性文件中放置适当的OAuth2配置(client-id、secret、访问令牌uri…)。(我假设您的客户端应用程序也使用Spring Boot)

  • 要结束用户的会话,您必须重定向到授权服务器中的端点,并以编程方式注销,如本文所示。

我已经在github上创建了一个存储库,其中包含一个示例应用程序,该应用程序具有您正在寻找的那些功能。

请查看一下,如果对你有帮助,请告诉我。

最新更新