【问题标题】:Spring Security - Authentication not firing upSpring Security - 身份验证未启动
【发布时间】:2014-07-29 08:07:52
【问题描述】:

我创建了一个基于示例应用程序的 Spring Boot - Security 项目,但是 Rest Controller 工作正常,这意味着我可以访问其余资源但安全性似乎根本没有启动,所以当我添加断点时如下所述,它不会在那里中断。不知道为什么。

@Configuration
@EnableWebSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        // @formatter:off
        auth.inMemoryAuthentication() // breakpoint here
            .withUser("roy")
                .password("spring")
                .roles("ADMIN");
        // @formatter:on
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

}

这是使用 Codio 托管和编辑的完整项目:http://bit.ly/1uFI0t5

【问题讨论】:

  • 尝试添加@EnableAutoConfiguration 注解。

标签: java spring spring-security spring-boot


【解决方案1】:

你必须告诉框架哪些是它必须保护的 url。

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

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

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

        http.authorizeRequests()
            .antMatchers("/**").access("hasRole('ROLE_USER')")
            .and().formLogin();

    }
}

【讨论】:

猜你喜欢
  • 2018-08-31
  • 2019-02-07
  • 2022-01-15
  • 2014-02-26
  • 1970-01-01
  • 2013-01-15
  • 2019-08-25
  • 2013-10-30
  • 2014-04-07
相关资源
最近更新 更多