> For the complete documentation index, see [llms.txt](https://potoyang.gitbook.io/spring-in-action-v4/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://potoyang.gitbook.io/spring-in-action-v4/untitled-3/untitled-2.md).

# 9.4　认证用户

如果你使用程序清单 9.1 中最简单的 Spring Security 配置的话，那么就能无偿地得到一个登录页。实际上，在重写 configure(HttpSecurity) 之前，我们都能使用一个简单却功能完备的登录页。但是，一旦重写了 configure(HttpSecurity) 方 法，就失去了这个简单的登录页面。

不过，把这个功能找回来也很容易。我们所需要做的就是在 configure(HttpSecurity) 方法中，调用 formLogin()，如下面的程序清单所示。

请注意，和前面一样，这里调用 add() 方法来将不同的配置指令连接在一起。 如果我们访问应用的 “/login” 链接或者导航到需要认证的页面，那么将会在浏览器中展现登录页面。如图 9.2 所示，在审美上它没有什么令人兴奋的，但是它却能实现所需的功能。

```java
@Override
protected void configure(HttpSecurity http) throws Exception {
  http
    .formLogin()
    .and()
    .authorizeRequests()
    .antMatchers("/spitter/me").hasRole("SPITTER")
    .antMatchers(HttpMethod.POST, "/spittles").hasRole("SPITTER")
    .anyRequest().permitAll()
    .and()
    .requeresChannel()
    .antMatchers("/spitter/form").requiresSecure();
}
```

![图 9.2　默认的登录页在审美上过于简陋，但是功能完备](https://3784153818-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LmcjU5gG__lBrRbUxBO%2F-LnHUwZp4DP9X3OniFgV%2F-LnHVcUBdMchOSe3f9Gk%2F9.2%20login%20form.jpg?alt=media\&token=5172f376-3bdf-4d43-93a5-ca7d33ec0344)

我敢打赌，你肯定希望在自己的应用程序中能有一个比默认登录页更漂亮的登录页面。如果这个普通的登录页面破坏了我们原本精心设计的漂亮站点，那真的是件很令人遗憾的事情。没问题！接下来，我们就看一下如何为应用添加自定义的登录页面。
