【问题标题】:How to get past the Authentication Required Spring-boot Security如何通过验证所需的 Spring-boot 安全性
【发布时间】:2015-10-31 16:51:02
【问题描述】:

我已经输入了“root”密码,并且它不断弹出。我该如何抑制或摆脱它。我正在使用 spring boot 和 spring security。

application.properties

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springbootpractice
spring.datasource.username=root


spring.jpa.database = MYSQL
spring.jpa.show-sql = true

# Hibernate
hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql: true
hibernate.hbm2ddl.auto: update
entitymanager.packagesToScan: /

如果这很重要,我正在使用 intellij 14。

----更新1-----

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/", "/index").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .loginPage("/index")
                .permitAll()
                .and()
                .logout()
                .permitAll();
    } 

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .authorizeRequests()
                    .antMatchers("/", "/index").permitAll()
                    .anyRequest().authenticated()
                    .and()
                    .formLogin()
                    .loginPage("/index")
                    .permitAll()
                    .and()
                    .logout()
                    .permitAll();
        }

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

【问题讨论】:

标签: intellij-idea spring-security spring-boot


【解决方案1】:

您可以绕过这个 Spring Boot 安全机制。请参阅下面的示例:

@SpringBootApplication
@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class})
public class SampleSpringBootApplication {

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

【讨论】:

    【解决方案2】:

    ACV 的回答可能是通过将security.basic.enabled=false 添加到通常位于src/main/resources 文件夹下的 application.properties 文件中来完全关闭身份验证的最简单方法。

    或者你只需​​输入密码:)

    1。使用默认密码

    当你运行你的 spring 应用程序时,通常会打印一大堆日志,而人们通常不会阅读这些日志。密码实际上是在启动时生成并打印到屏幕上的。并且用户名只是user。如果您正在使用浏览器进行测试,它可能只需要您输入一次并缓存它,所以一劳永逸,您应该安全登录,而无需每次都进行身份验证。 (但是,每次重启应用都会生成一个新密码)

    2。自定义您的密码

    如果您想自定义用户名和密码,请将以下属性添加到您的 application.properties:

    security.user.name=myuser
    security.user.password=mypassword
    

    这是使用您自己的用户名和密码时的样子

    参考:

    1. Spring Boot Features - Security
    2. Monitoring and Management over HTTP

    【讨论】:

      【解决方案3】:

      当 Spring Security 在类路径中时,Spring Boot 默认使用基本身份验证保护所有页面。这就是为什么要求您提供用户名和密码。

      您需要配置安全性。为此,人们通常会扩展 WebSecurityConfigurerAdapter,如下所示:

      @Configuration
      public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
      
          @Override
          protected void configure(HttpSecurity http) throws Exception {
              http
                  .authorizeRequests()
                      .antMatchers("/", "/home").permitAll()
                      .anyRequest().authenticated()
                      .and()
                  ...
      

      更多详情请参考this Spring Security guide

      【讨论】:

      • 请参阅更新 1。我已阅读 spring.io,但无法解决问题,这就是我在此处发布的原因。
      • 希望您的 WebSecurityConfig 类被正确注释。只是为了确认一下,因为上面的示例中没有看到它。
      • 是的,抱歉...已更新
      • 我把root作为用户名,密码什么也没有,我把用户名和用户名和密码作为密码,它不起作用。我无法通过它!
      • 我也看不到任何问题。奇怪的。您是否逐步尝试过 Spring 安全指南?
      【解决方案4】:

      问题来了

      (1) .loginPage("/index") 说我的登录页面在索引处,但我只想使用 spring 的默认登录页面。

      (2) 必须将安全包移动到演示包(主包)中。感谢@Sanjay 的建议。我尝试使用@ComponantScan,但它无法正常工作。

      【讨论】:

        【解决方案5】:

        这个类必须在所有其他包的父包中: WebSecurityConfig。 也在application.properties设置:

        security.basic.enabled=false
        

        【讨论】:

        • 我试图运行 spring-boot 附带的示例示例,application.properties 文件位于文件夹src/main/resources 下,添加security.basic.enabled=false 是我唯一需要做的事情。
        猜你喜欢
        • 2020-02-09
        • 2014-07-06
        • 1970-01-01
        • 2019-02-07
        • 1970-01-01
        • 2018-12-10
        • 2019-03-13
        • 2019-04-23
        • 2021-01-12
        相关资源
        最近更新 更多