【问题标题】:Configure antMatchers in Spring Security在 Spring Security 中配置 antMatchers
【发布时间】:2018-09-08 20:02:52
【问题描述】:

我在 Wildfly 服务器中有这个 Spring Security 配置:

@Configuration
@EnableWebSecurity
@Import(value= {Application.class, ContextDatasource.class})
@ComponentScan(basePackages= {"org.rest.api.server.*"})
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired 
    private RestAuthEntryPoint authenticationEntryPoint;

    @Autowired
    MyUserDetailsService myUserDetailsService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserDetailsService);
        auth.authenticationProvider(authenticationProvider());
    }
    @Bean
    public DaoAuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
        authenticationProvider.setUserDetailsService(myUserDetailsService);
        authenticationProvider.setPasswordEncoder(passwordEncoder());
        return authenticationProvider;
    }
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
        .csrf().disable()
        .authorizeRequests()
        .antMatchers("/v1/notification")
        .permitAll()
        .anyRequest()
        .authenticated()
        .and()
        .httpBasic()
        .authenticationEntryPoint(authenticationEntryPoint);
    }
    @Bean
    public PasswordEncoder passwordEncoder() {
        return NoOpPasswordEncoder.getInstance();
    }
}

我想配置 Spring 安全性以允许所有请求发送到 http://localhost:8080/war_package/v1/notification 但不幸的是我总是未经授权。你知道配置这个的正确方法是什么吗?

【问题讨论】:

    标签: spring-boot spring-security spring-security-rest


    【解决方案1】:

    您需要启用 ResourceServer 并在其中添加 configure(HttpSecurity http)。

    @Configuration
    @EnableResourceServer
    public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
        @Override
        public void configure(HttpSecurity http) throws Exception {
            http
            .csrf().disable()
            .authorizeRequests()
            .antMatchers("/v1/notification")
            .permitAll()
            .anyRequest()
            .authenticated()
            .and()
            .httpBasic()
            .authenticationEntryPoint(authenticationEntryPoint);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 2018-01-31
      • 2012-01-23
      • 2011-01-08
      • 2020-03-04
      相关资源
      最近更新 更多