【问题标题】:Spring boot 1.5 disable oauth2 securitySpring Boot 1.5 禁用 oauth2 安全性
【发布时间】:2019-12-28 14:55:21
【问题描述】:

如何在我的 Spring Boot 应用程序中禁用 oauth2 安全过滤,或跳过安全检查, 我只想直接在 Spring boot @RestController 中点击 GETPOST 端点,而不经过安全过滤。

我正在使用以下配置

security:
  oauth2:
    client:
      access-token-validity-seconds: 3600
  tokenExtractor:
    type: header

pom.xml 依赖项

<dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
</dependency>
<dependency>
     <groupId>org.springframework.security.oauth</groupId>
     <artifactId>spring-security-oauth2</artifactId>
</dependency>

春季版

<spring.version>4.3.7.RELEASE</spring.version>
<spring.boot.version>1.5.2.RELEASE</spring.boot.version>

【问题讨论】:

  • 如果你想忽略它,为什么要添加spring-security?
  • 如果你不想在春季增加安全性,没有强制要求
  • 我想跳过安全检查以测试其他内部 GET、POST url
  • 这个可能重复 - stackoverflow.com/questions/23894010/… 问题。

标签: java maven spring-boot oauth-2.0


【解决方案1】:

如果您不想删除整个 Spring Security,您可以在 Spring Configuration bean 中为所有 url 添加忽略配置:

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers(
            "/**");
}

【讨论】:

  • 谢谢,我在类中找到了拒绝规则,@Configuration public class ResourceAccessConfiguration extends ResourceServerConfigurerAdapter {@Override public void configure(HttpSecurity http)抛出异常 {List matchers = Arrays.stream(new String[]{"/**/payment/**"}).map(AntPathRequestMatcher::new).collect(Collectors.toList()); http.authorizeRequests().requestMatchers(new OrRequestMatcher(matchers)).denyAll();}}
【解决方案2】:

三种方式

A. 我能够绕过 Spring Boot 安全过滤,同时将 @EnableResourceServer 保留在 @SpringBootApplication Application 类中

1.permitall 用于 ResourceServerConfigurerAdapter 覆盖中的 anonymous

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

@Configuration
public class ResourceAccessConfiguration extends ResourceServerConfigurerAdapter {
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").permitAll().anyRequest().anonymous();<< this will allow any resource endpoint access when the HTTP request Authorization header not available
        //http.authorizeRequests().antMatchers("/**").permitAll();<< also can
    }
}

spring boot 应用初始化器

@SpringBootApplication
@EnableResourceServer << keep this
public class Application extends SpringBootServletInitializer {

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

2.移除授权头(从HTTP请求中移除OAuth 2.0 Access Token)

B. 也可以通过删除@EnableResourceServer 并在 application.yml 中设置参数来禁用端点的安全过滤,如下所示。 删除 @EnableResourceServer 后,spring 安全配置将回退到默认值 org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter

1.application.yml,security.ignored 属性

security:
  ignored: /**

2.spring boot 应用初始化器

@SpringBootApplication
//@EnableResourceServer << remove this
public class Application extends SpringBootServletInitializer {

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

3.去掉同上的授权头

C. 还可以通过删除 @EnableResourceServer 并添加配置类扩展 WebSecurityConfigurerAdapter 来禁用端点的安全过滤

1.

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests().antMatchers("/**").permitAll().anyRequest().authenticated().and().csrf().disable();
    }
}

2.//@EnableResourceServer 注释同上

3.去掉同上的授权头

【讨论】:

    猜你喜欢
    • 2017-08-06
    • 1970-01-01
    • 2014-07-16
    • 2020-06-11
    • 2016-05-03
    • 2016-01-02
    • 2018-04-26
    相关资源
    最近更新 更多