2017-05-26 88 views
0

對不起,我不知道如何用文字表達。春季認證頁面更改

我的問題是,我使用Spring Initializr與項目發佈:

  • 的Spring Web
  • 春季安全
  • 彈簧操動
  • 春天JPA

    <parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.3.RELEASE</version> 
    <relativePath/> <!-- lookup parent from repository --> 
    </parent> 
    
    <properties> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
    <java.version>1.8</java.version> 
    </properties> 
    
    <dependencies> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    
    <dependency> 
        <groupId>mysql</groupId> 
        <artifactId>mysql-connector-java</artifactId> 
        <scope>runtime</scope> 
    </dependency> 
    <dependency> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-test</artifactId> 
        <scope>test</scope> 
    </dependency> 
    </dependencies> 
    
    <build> 
    <plugins> 
        <plugin> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-maven-plugin</artifactId> 
        </plugin> 
    </plugins> 
    </build> 
    

當我映射一個URL,並嘗試訪問它,我得到這個漂亮的登錄表單:

login form

但是,當我與

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

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

覆蓋安全我得到這個醜陋的登錄表單

ugly login form

我想是因爲當重寫配置時,有些東西正在改變Spring使用的登錄表單或我我使用Spring安全性錯誤。

有人可以告訴我爲什麼會發生這種情況嗎?謝謝!

+0

我剛剛刪除了@EnableWebSecurity,現在應用程序仍然使用執行程序登錄表單。抱歉。 –

回答

0

第一個很好的登錄表單屬於您的瀏覽器。如果您嘗試使用不同的瀏覽器,則會看到不同的結果。會發生什麼情況是,當您的瀏覽器請求您的應用程序的URL時,您的應用程序會使用未經授權的代碼進行響應,然後您的瀏覽器會要求您提供憑據以重新嘗試請求。

醜陋的登錄表單是Spring的默認設置。不管你想要什麼,你都可以設計它。你可以看到thisthis的例子,讓你開始。你需要添加一些CSS。

方法configure(HttpSecurity http)你當你擴展的類WebSecurityConfigurerAdapter看起來這並沒有覆蓋的默認實現:

protected void configure(HttpSecurity http) throws Exception { 
    logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity)."); 

    http 
     .authorizeRequests() 
      .anyRequest().authenticated() 
      .and() 
     .formLogin().and() 
     .httpBasic(); 
} 

.formLogin()就是你得到的醜陋的登錄表單。