如何配置资源服务器以接受访问令牌(Oauth2)Spring Boot / Ionic 3

时间:2017-11-28 14:58:34

标签: cordova azure spring-boot ionic-framework oauth-2.0

我有一个问题,关于如何将我的Spring Boot服务器配置为资源服务器,同时从Ionic 3中的移动应用程序构建中接收访问令牌。

我正在使用Azure Active Directory供用户登录,因为我希望用户登录我的应用程序,所以我使用的是Implicit OAuth2流程。在我的Ionic应用程序中,我使用此(https://github.com/AzureAD/azure-activedirectory-library-for-cordova)apache cordova插件显示登录表单(来自Microsoft AAD),并使用正确的凭据我收到访问令牌和到期日期。

enter image description here

我使用Spring Boot应用程序作为REST API。移动应用中的用户呼叫端点,如:

https://localhost:8080/period
https://localhost:8080/declaration/1
etc...

当然,我想保护这些资源,因为任何人都可以立即打电话给他们。现在我在Authorization标题中发送访问令牌,如下所示:

let host = "https://localhost:8080/";    
let headers = new Headers();
headers.append('Authorization',access_token);
let options = new RequestOptions({headers: headers});
consle.log(this.http.get(this.host + "period",options));

我一直在寻找教程,文档,堆栈主题等...整天但我找不到我正在寻找将访问令牌发送到资源服务器的情况。此外,我很难理解这些教程中发生了什么。到目前为止,这是我设法理解的内容以及我在spring boot应用程序中的内容:

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = false, securedEnabled = true)
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/period/**").hasRole("USER")
                .antMatchers("/files/**").hasRole("USER")
                .antMatchers("/declaration/**").hasRole("USER");
    }


}

我的问题是:

如何设法处理访问代码并将资源返回给用户?

enter image description here

修改

所以我尝试使用Filter来拦截传入的HTTP流量,并从头部获取令牌,如下所示:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.provider.authentication.BearerTokenExtractor;
import org.springframework.security.oauth2.provider.authentication.TokenExtractor;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class OAuth2AuthenticationProcessingFilter implements Filter,InitializingBean {

    private final static Log logger = LogFactory.getLog(OAuth2AuthenticationProcessingFilter.class);

    private TokenExtractor tokenExtractor = new BearerTokenExtractor();

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("Initialize OAuth2 filter");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("Do filter");

        final boolean debug = logger.isDebugEnabled();
        final HttpServletRequest req = (HttpServletRequest) request;
        final HttpServletResponse res = (HttpServletResponse) response;

        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        System.out.println(req.getHeader("Authorization"));
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    }

    @Override
    public void destroy() {
        System.out.println("Destroying OAuth2 filter");
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}

但只有当我按照以下方式设置配置时,才会调用过滤器(doFilter):

.antMatchers("/period/**").permitAll()

如果我像这样使用过滤器(doFilter)将不会被调用(init仍然调用):

.antMatchers("/period/**").permitAll()

所以我认为过滤器不是这里的方法吗?

编辑:在我发现这个Github Repo提供我需要的所有内容后,我得到了这个工作:

https://github.com/yuhuachang/spring-boot-oauth2-azuread

1 个答案:

答案 0 :(得分:0)

Niek:我一直在仔细研究Azure,所以给我一天左右的时间,我会给你发一些验证令牌的说明。

我不会编写实际的Java Spring Boot代码,因为那不是我熟悉的技术堆栈 - 但我会清楚说明你需要编写代码。