ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java – 我们可以根据路径变量使用httpbasic和OAuth2作为API吗?

2019-09-10 17:04:24  阅读:240  来源: 互联网

标签:jhipster java spring-boot spring-security oauth-2-0


在我的应用程序中,我想仅为某些特定的API调用提供OAuth2安全性.我的问题是我可以根据路径变量提供HttpBasic或Oauth2身份验证吗?

以下是我将考虑的两种情况.

1)让我们说用户(其名称在路径变量中提供)xyz,如果xyz没有OAuth的功能,我想使用httpBasic进行身份验证

2)如果另一个用户abc具有OAuth功能,我想使用Oauth / OpenId connect对其进行身份验证.

我有一个表为用户分配功能,下面是表的一瞥.

名称,功能

xyz,HttpBasic

abc,Oauth

解决方法:

好吧,我自己做了一些研究,能够找到解决方案.这就是我做的,

– 使用WebSecurityConfigurerAdapter处理一个httpbasic配置,现在在任何拦截器开始它之前我创建了一个请求匹配器,它将检查授权头是基本还是承载.

      //By default this filter order is 100 and OAuth has filter order 3
      @Order(2)
    public class MicroserviceSecurityConfigurationHttpBasic extends  WebSecurityConfigurerAdapter { 
          @Override
          protected void configure(HttpSecurity http) throws Exception {
             http.csrf().disable().exceptionHandling()
            .authenticationEntryPoint(customAccessDeniedHandler())
            .and().headers().frameOptions().disable()
            .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
            .and()
            .requestMatcher(new BasicRequestMatcher())
            .authorizeRequests()
            .antMatchers("/api/**").authenticated()
            .and().httpBasic();

          }
          private class BasicRequestMatcher implements RequestMatcher {
            @Override
            public boolean matches(HttpServletRequest httpRequest) {
             String auth = httpRequest.getHeader("Authorization");
             String requestUri = httpRequest.getRequestURI();
             //Fetching Identifier to provide OAuth Security to only specific urls
             String identifier= requestUri.substring(requestUri.lastIndexOf("/") + 1, requestUri.length());

            //Lets say for identifier ABC only, I want to secure it using OAuth2.0
           if (auth != null && auth.startsWith("Basic") && identifier.equalsIgnoreCase("ABC")) {
             auth=null;
              }
           //For ABC identifier this method will return null so then the authentication will be redirected to OAuth2.0 config.
           return (auth != null && auth.startsWith("Basic"));
            }
        }
  }

– 之后我用ResourceServerConfigurerAdapter创建了OAuth2.0配置,下面是它的一瞥.

    //Default filter order=3 so this will be executed after WebSecurityConfigurerAdapter 
    public class MicroserviceSecurityConfiguration extends ResourceServerConfigurerAdapter {
       ...
      //Here I am intercepting the same url but the config will look for bearer token only
      @Override
public void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().exceptionHandling()
    .and().headers().frameOptions().disable()
    .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
    .and().authorizeRequests()
    .antMatchers("/api/**").authenticated();
    }
   }

参考文献:https://github.com/spring-projects/spring-security-oauth/issues/1024

Spring security with Oauth2 or Http-Basic authentication for the same resource

标签:jhipster,java,spring-boot,spring-security,oauth-2-0
来源: https://codeday.me/bug/20190910/1799953.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有