【问题标题】:Spring Boot restful resource security - Authentication not firingSpring Boot RESTful 资源安全 - 身份验证未触发
【发布时间】:2014-12-11 03:37:09
【问题描述】:

我在使用 Spring Boot 1.1.7 保护静态资源时遇到问题。我创建了一个带有自定义 AuthenticationProvider 的 SecurityConfiguration。

SecurityConfiguration 和 AuthenticationProvider bean 正在加载,但我的自定义“身份验证”方法从未被触发。我已经查看了大部分教程,但无法使用自定义身份验证限制对任何 url 的访问。

@EnableWebSecurity
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    final static Logger log = LoggerFactory.getLogger(SecurityConfiguration.class);

    private @Autowired CustomAuthenticationProvider provider;

    /**
     * Configure global.
     *
     * @param auth the auth
     * @throws Exception the exception
     */
//  @Autowired
//  public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
//              auth.authenticationProvider(this.provider);
//  }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        log.warn("Configure AuthMgrBuilder");
        auth.authenticationProvider(this.provider);
    }

    /* (non-Javadoc)
     * @see org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        log.warn("Configure http");
        http.authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .csrf().disable();
    }

}

【问题讨论】:

    标签: java spring spring-security spring-boot


    【解决方案1】:

    我修改了 configure(HttpSecurity http) 方法如下,现在它使用基本的 http 安全性通过身份验证提供程序进行身份验证。不过不确定这是最好的方法。

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            log.warn("Configure http");
            http.csrf().disable()
                .authorizeRequests()
    //          .antMatchers("/**")
                .anyRequest()
                .authenticated()
                .and().httpBasic();
        }
    

    【讨论】:

    • 这对我来说似乎非常明智。如果您想以不同的方式收集凭据,则需要说出那是什么(Spring 无法猜测)。
    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 2016-10-03
    • 2015-07-01
    • 2018-09-11
    • 2019-01-01
    • 2020-11-21
    • 1970-01-01
    • 2012-09-19
    相关资源
    最近更新 更多