【问题标题】:oauth2 and basic authoauth2 和基本身份验证
【发布时间】:2021-07-29 03:11:57
【问题描述】:

我使用带有 spring security 的 spring boot 2.4.6。

实际上我们使用 oauth 2

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    private JwtAuthenticationConverter jwtAuthenticationConverter() {
    JwtGrantedAuthoritiesConverter jwtGrantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
    jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("roles");
    jwtGrantedAuthoritiesConverter.setAuthorityPrefix("ROLE_");
    JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
    jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter);
    return jwtAuthenticationConverter;
}

@Override
protected void configure(HttpSecurity http) throws Exception {

    http.cors().configurationSource(request -> new CorsConfiguration().applyPermitDefaultValues())
            .and() // (1)
            .authorizeRequests()
            .antMatchers("/actuator/**").permitAll()
            .antMatchers("/v2/api-docs").permitAll()
            .anyRequest().authenticated() // (2)
            .and()
            .oauth2ResourceServer().jwt() // (3)
            .jwtAuthenticationConverter(jwtAuthenticationConverter());
    }
}

对于某些端点,我需要支持登录名/密码。 有没有办法做到这一点?

【问题讨论】:

    标签: spring-boot spring-security


    【解决方案1】:

    您可以设置多个 HttpSecurity 实例,每个实例匹配一个或多个路径并实现不同的身份验证方案。您可以将它们添加为从WebSecurityConfigurerAdapter 扩展的单独内部类。有关更多信息和示例,请参阅docs

    如果订单很重要,请确保包含@Order。在您提供的示例中,配置是通用的并且匹配所有请求,因此它应该是@Order(2)。附加配置将是 @Order(1) 并以 http.antMatcher("/some/path/**").formLogin()... 或类似开头。

    注意:herehere 的类似(旧)问题。

    【讨论】:

      猜你喜欢
      • 2021-01-31
      • 2017-09-18
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-28
      • 2016-08-23
      相关资源
      最近更新 更多