2015-05-08 107 views
3

我正在嘗試使用具有外部本地身份驗證服務器(WSO2身份驗證服務器)的spring-cloud-security進行安全保護的spring-boot-web應用程序。我們正在使用OAuth2 OpenID Connect(JWT令牌)。使用WSO2身份驗證服務器的Spring Cloud安全

我的應用程序將重定向到WSO2服務器,只適用於/ oauth2/authorize請求,但在嘗試將authorization_code轉換爲access_token時失敗。 spring-boot報告401認證錯誤。

我相信這是因爲oauth2/token端點需要基本認證。

有人可以指導我如何使/ oauth2 /令牌請求使用基本認證標題與春季雲安全?

這裏是我的application.yml

spring: 
    oauth2: 
    sso: 
     home: 
     secure: false 
     path: /,/**/*.html 
    client: 
     accessTokenUri: https://URI:9443/oauth2/token 
     userAuthorizationUri: https://URI:9443/oauth2/authorize 
     clientId: id 
     clientSecret: secret 
     scope: openid 
     clientAuthenticationScheme: header 
    resource: 
     userInfoUri: https://URI:9443/oauth2/userinfo?schema=openid 

這裏是我的簡單application.java

package demo; 

import java.io.IOException; 

import javax.servlet.Filter; 
import javax.servlet.FilterChain; 
import javax.servlet.ServletException; 
import javax.servlet.http.Cookie; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 
import org.springframework.cloud.security.oauth2.sso.EnableOAuth2Sso; 
import org.springframework.cloud.security.oauth2.sso.OAuth2SsoConfigurerAdapter; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.web.csrf.CsrfFilter; 
import org.springframework.security.web.csrf.CsrfToken; 
import org.springframework.security.web.csrf.CsrfTokenRepository; 
import org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository; 
import org.springframework.web.filter.OncePerRequestFilter; 
import org.springframework.web.util.WebUtils; 

@SpringBootApplication 
@EnableZuulProxy 
@EnableOAuth2Sso 
public class SpringOauth2WithWso2Application { 

    public static void main(String[] args) { 
     SpringApplication.run(SpringOauth2WithWso2Application.class, args); 
    } 

    @Configuration 
    protected static class SecurityConfiguration extends OAuth2SsoConfigurerAdapter { 

     @Override 
      public void match(RequestMatchers matchers) { 
      matchers.anyRequest(); 
      } 

      @Override 
      public void configure(HttpSecurity http) throws Exception { 
      http.authorizeRequests().antMatchers("/index.html", "/home.html", "/") 
       .permitAll().anyRequest().authenticated().and().csrf() 
       .csrfTokenRepository(csrfTokenRepository()).and() 
       .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class); 
      } 



     private Filter csrfHeaderFilter() { 
      return new OncePerRequestFilter() { 
       @Override 
       protected void doFilterInternal(HttpServletRequest request, 
         HttpServletResponse response, FilterChain filterChain) 
         throws ServletException, IOException { 
        CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class 
          .getName()); 
        if (csrf != null) { 
         Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN"); 
         String token = csrf.getToken(); 
         if (cookie == null || token != null 
           && !token.equals(cookie.getValue())) { 
          cookie = new Cookie("XSRF-TOKEN", token); 
          cookie.setPath("/"); 
          response.addCookie(cookie); 
         } 
        } 
        filterChain.doFilter(request, response); 
       } 
      }; 
     } 

     private CsrfTokenRepository csrfTokenRepository() { 
      HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository(); 
      repository.setHeaderName("X-XSRF-TOKEN"); 
      return repository; 
     } 
    } 


} 
+0

事實證明,錯誤是由於使用自簽名證書的wso2身份服務器導致的。我們通過將-Djavax.net.debug = ssl添加到spring-boot啓動命令來診斷它。我們通過將WSO2服務器的公共證書添加到spring-boot應用程序的java truststore來解決此問題。問題解決了。 – Bryce

+1

這將是值得發佈的完整答案否則這篇文章最終會浪費人們的時間,如果他們希望使用它來幫助他們將WSO2 IS集成到Spring Cloud Security(特別是Spring Security OAuth2)。例如,如果您希望將Spring Security OAuth2與WSO2 IS一起使用,則此問題/線程當然具有相關性:https://github.com/spring-cloud/spring-cloud-security/issues/63(請注意需要更改在嘗試訪問用戶身份時,承載者承載者是鏈中的最後一步)。也可以將標題(使用WSO2 IS的Spring Cloud Security)更改爲不太通用的東西。 –

回答