【问题标题】:Spring Security Configuration in Spring BootSpring Boot 中的 Spring 安全配置
【发布时间】:2014-02-05 22:04:19
【问题描述】:

我正在将 Spring 3 项目转换为 Spring 4 + Spring Boot。我不知道这样做是否正确。我将 Spring Security XML 配置转换为 基于 Java 的配置,如下所示:

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/", "/home").permitAll()
            .anyRequest().authenticated();
    http.formLogin()
            .defaultSuccessUrl("/afterLogin")
            .loginPage("/profiles/lognin/form")
            .failureUrl("/accessDenied")
            .and()
            .authorizeRequests()
            .regexMatchers("....")
            .hasRole("ROLE_USER")
            .antMatchers("....")
            .hasRole("ROLE_USER")
            //....
            ;
}

@Override
protected void configure(AuthenticationManagerBuilder authManagerBuilder)
        throws Exception {
           authManagerBuilder.authenticationProvider(this.getDaoAuthenticationProvider());
}
   // ....
} 

当我点击主页 URL 时,我得到 Spring Security 默认登录弹出面板。在我看来,上面的配置没有生效,但是Spring Boot中默认的Spring Security配置没有生效。如果是这样,如何覆盖默认的?

【问题讨论】:

    标签: spring-security spring-boot spring-java-config


    【解决方案1】:

    我找到了答案。我需要使用以下行创建一个名为 application.properties 的文件:

    security.basic.enabled=false
    

    并将此文件放在src/main/resource 下。就是这样。

    【讨论】:

      【解决方案2】:

      这样配置你的弹簧。

      protected void configure(HttpSecurity http) throws Exception {
      
          http
                      .csrf()
                  .and()
                      .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
                      .exceptionHandling()
                  .and()
                      .rememberMe()
                  .and()
                      .formLogin()
                      .loginProcessingUrl("/user")   // rest apiyi yaz.
                      //.usernameParameter("username")
                      //.passwordParameter("password")
                      .permitAll()
                  .and()
                      .logout()
                      //.logoutUrl("/api/logout")
                      //.deleteCookies("JSESSIONID", "CSRF-TOKEN")
                      .permitAll()
                  .and()
                      .headers()
                      .frameOptions()
                      .disable()
                      .authorizeRequests()
                      .antMatchers("/login").permitAll()
                      .antMatchers("/#/dashboard/home").permitAll()
                  ;
      
      
      
      }
      

      【讨论】:

        猜你喜欢
        • 2015-02-22
        • 2021-02-21
        • 1970-01-01
        • 1970-01-01
        • 2014-10-27
        • 2019-08-25
        • 2016-02-12
        • 2018-12-05
        相关资源
        最近更新 更多