Spring Cloud Security with WSO2 Identity Server

I am trying to implement a spring-boot-web application secured with spring-cloud-security with an external local authentication server (WSO2 Identity Server). We are using OAuth2 OpenID Connect (JWT tokens).

My app will redirect WSO2 server, just fine for / oauth 2 / authorize request, but doesn't work when trying to convert authorization_code to access_token. spring-boot reports authentication error 401.

I believe this is because the oauth2 / token endpoint requires basic authentication.

Can anyone point me to how to make the / oauth 2 / token request to use basic authentication headers with spring-cloud-security?

Here's my 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

      

Here is my simple 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;
        }
    }


}

      

+3


source to share


1 answer


Although the need to add the WSO2 IS certificate to the Java certificate store is mentioned as a solution, in case it helps anyone with the steps required to do this as well as configuring mitmproxy so you can check if the traffic described here was with template project: https://github.com/nicodewet/template-spring-boot-oauth2-wso2-is



0


source







All Articles