【问题标题】:Spring oauth2 and digest authentication will work togetherSpring oauth2 和摘要认证将一起工作
【发布时间】:2018-05-04 11:50:56
【问题描述】:

我已将 spring oauth2 添加到我的宁静服务中。大多数服务都由我自己的门户使用,因此获取令牌然后调用 api 就可以了。但是,我已经公开了一些必须在没有此令牌概念的情况下调用的 Web 服务。这些消费者有用户名和密码。

最好的例子是 Swagger 实现。应该通过摘要而不是 oauth 令牌来验证打开 swagger 页面的位置。

我做了以下代码更改,但它不起作用。 我相信,在这种情况下,我不需要从资源服务器调用 oauth 服务器。所以只需在资源服务器中制作以下代码。但是看到问题,例如在身份验证页面接受我的凭据后,它再次重新路由/重定向到同一身份验证页面。

请帮忙。

@Configuration
@EnableResourceServer
public class ResourceServerImpl extends ResourceServerConfigurerAdapter {


    @Override
    public void configure(HttpSecurity http) throws Exception {    
        http.anonymous().disable().requestMatchers().antMatchers("/**").and().authorizeRequests()
        .antMatchers(HttpMethod.POST, "/url1/path1/path2").hasAnyAuthority( "FUNCTION_FN1")
        .antMatchers(HttpMethod.GET, "/url2/path1/path2").hasAnyAuthority( "FUNCTION_FN2")
        .antMatchers("/swagger-ui.html").hasRole("USER")
        .anyRequest().authenticated()
        .and().formLogin().and().httpBasic()
        .and().exceptionHandling().accessDeniedHandler(new OAuth2AccessDeniedHandler());
    }
}


@Configuration
@EnableWebSecurity
public class OAuth2SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
        auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");
    }
}

更新 我认为这不可能提出一些通过 DIGEST 进行身份验证的请求,而有些是通过 OAUTH 进行身份验证的。所以目前我制作了 swagger url 也使用 oauth 令牌进行身份验证,而不是摘要支持。

【问题讨论】:

  • 你没有提供任何关于配置的代码,所以你为什么要问我必须做哪些代码更改?
  • @AtaurRahmanMunna 当然,我会提供,但这可行吗...?
  • 但是我暴露了更多的网络服务——所以对于其他消费者和 oauth2 消费者来说,端点是不同的,对吧?
  • 是的,阿陶尔。 URL 不同,但服务是单一的,但我希望在两种情况下都遵循相同的访问规则(授权)。
  • 您需要为 httpBasic() 提供单独的配置。参考 - stackoverflow.com/questions/27774742/…

标签: spring spring-security spring-oauth2


【解决方案1】:

是的,在同一个应用程序中配置多个授权机制绝对是可能的。应为每个机制提供WebSecurityConfigurerAdapter 的不同实例。 @EnableResourceServer@EnableAuthorizationServer 提供相应的 oauth 适配器。 ApiSecurityConfiguration 为基本认证提供适配器。

这是basicoauth2 的最小工作示例:

@SpringBootApplication
public class TestApp {

    @RestController
    public static class Endpoints {

        // doesn't require any authentication
        @RequestMapping("/test")
        public String test() {
            return "no auth";
        }

        // requires basic authentication
        @RequestMapping("/api/test")
        public String apiTest() {
            return "basic auth";
        }

        // requires oauth authentication
        @RequestMapping("/oauth/test")
        public String oauthTest() {
            return "oauth";
        }
    }

    @Configuration
    @EnableWebSecurity
    public static class ApiSecurityConfiguration extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.requestMatchers().antMatchers("/api/**")
                    .and()
                    .authorizeRequests().antMatchers("/api/**").hasRole("USER")
                    .and()
                    .httpBasic();
        }

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication()
                    .withUser("user")
                    .password("password")
                    .roles("USER");
        }
    }

    @Configuration
    @EnableAuthorizationServer
    public static class AuthServerConfiguration extends AuthorizationServerConfigurerAdapter {

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.inMemory()
                    .withClient("test")
                    .secret("test")
                    .authorizedGrantTypes("client_credentials")
                    .authorities("USER")
                    .scopes("read", "write", "test")
                    .resourceIds("oauth2-resource")
                    .accessTokenValiditySeconds(120);
        }
    }

    @Configuration
    @EnableResourceServer
    public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http.requestMatchers().antMatchers("/oauth/**")
                    .and()
                    .authorizeRequests().antMatchers("/oauth/**").authenticated();
        }
    }

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

pom.xml

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.springframework.security.oauth</groupId>
      <artifactId>spring-security-oauth2</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
  </dependencies>

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 1970-01-01
    • 2014-03-11
    • 2019-04-28
    • 2011-05-03
    • 1970-01-01
    • 2013-06-28
    • 2016-03-10
    • 2016-05-10
    相关资源
    最近更新 更多