2017-05-26 92 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()就是你得到的丑陋的登录表单。