首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WSO2 Identity Server的Spring云安全

WSO2 Identity Server的Spring云安全
EN

Stack Overflow用户
提问于 2015-05-09 02:09:57
回答 1查看 2.1K关注 0票数 3

我正在尝试实现一个spring-boot-web应用程序,该应用程序使用spring-cloud-security和外部本地身份验证服务器(WSO2 Identity Server)进行保护。我们使用的是OAuth2 OpenID连接(JWT令牌)。

对于/oauth2/authorize请求,我的应用程序将重定向到WSO2服务器,但在尝试将authorization_code转换为access_token时却失败了。spring-boot报告401身份验证错误。

我相信这是因为oauth2/token端点需要基本身份验证。

有人能指导我如何让/oauth2/token请求使用带有spring-cloud-security的基本身份验证头吗?

这是我的application.yml

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
        }
    }


}
EN

回答 1

Stack Overflow用户

发布于 2016-07-25 17:08:25

尽管已经提到需要将mitmproxy证书添加到Java certificate Store中作为一种解决方案,但如果它对任何人都有帮助,那么需要执行的步骤以及设置mitmproxy以便您可以检查流量的步骤已经通过一个模板项目记录在这里:https://github.com/nicodewet/template-spring-boot-oauth2-wso2-is

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30130236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档