如何使用Spring Security自定义登录页面



我的应用程序使用的是Spring Security,这里是对用户进行身份验证的安全部分,但登录页面由Spring Security提供:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.antMatchers("/home*").hasRole("USER")
.and()
.formLogin();
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user")
.password("password")
.roles("USER")
}
}

我想使用下面的登录页面,而不是Spring的登录页面:

login.html:

<html lang='en'>
<head>
<title>WebApp</title>
<meta charset='utf-8' />
<script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="css/login.css" />  
</head>
<body>
<div class="login-page">
<img src='img/taxi_.jpg' style='width: 180px; margin-left: auto; margin-right: auto; display: block; padding-top: 20px; padding-bottom:20px;' />
<div class="heading">
<center>Dashboard</center>
</div>
<div class="form">
<form action ="home.html" class="register-form">
<input type="text" placeholder="name"/>
<input type="password" placeholder="password"/>
<input type="text" placeholder="email address"/>
<button>create</button>
<p class="message">Already registered? <a href="#">Sign In</a></p>
</form>
<form action="home.html" class="login-form">
<input type="text" placeholder="username"/>
<input type="password" placeholder="password"/>
<button id="button_login">login</button>
<p class="message">Not registered? <a href="#">Create an account</a></p>
</form>
</div>
</div>
</body>
</html>

如何使用我的自定义登录页面来显示,而不是显示Spring Security的登录页面?

我只想指出这里没有澄清的一些时刻。

  1. 首先,.loginPage("/login.html")不会起作用(无论如何,在我的情况下都不会)。此处引号中的内容是将在控制器中搜索的URL路径。以下是我的安全配置文件中的片段:

    .formLogin().loginPage("/login")
    .loginProcessingUrl("/authentication").permitAll();
    

在这里我写了"/login"。因此,现在在控制器中定义/login路径应该指向login.html页面。

@GetMapping("/login")
public String login() {
return "login";
}

只有这样,它才应该重定向到所需的视图页面。我通常将视图页面放在/webapp/WEB-INF/view文件夹下
P.S.我希望您正确配置了ViewResolverbean,否则它将不起作用:)

  1. 还有一件事。我看到你想在这个页面上使用一些CSS。要为您的自定义登录页面启用CSS和其他静态资源,您应该添加以下行

.antMatchers("/resources/**").permitAll()

所以,最终会是这样的(非常简短和令人毛骨悚然的版本,但你的自定义登录应该可以工作):

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/resources/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login")
.loginProcessingUrl("/authentication").permitAll();
}

仅供参考,请将您的资源放在/webapp/resources/下。此外,您应该在spring配置文件中配置资源处理程序。

这里还有一个很好的链接可以让你的头脑清醒:创建自定义登录表单:授予对剩余资源的访问权限

请参阅Spring Security Reference:

虽然自动生成的登录页面便于快速启动和运行,但大多数应用程序都希望提供自己的登录页面。为此,我们可以更新我们的配置,如下所示:

protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login") 1
.permitAll();        2
}

1更新后的配置指定登录页面的位置
2我们必须授予所有用户(即未经身份验证的用户)访问我们的登录页面的权限。formLogin().permitAll()方法允许所有用户访问与基于表单的登录相关联的所有URL。

您修改的代码:

public void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity
.authorizeRequests()
.antMatchers("/home*").hasRole("USER")
.and()
.formLogin()
.loginPage("/login.html")
.permitAll();
}
httpSecurity.authorizeRequests()
.antMatchers("/home*").hasRo‌​le("USER").and()                                          
.formLogin()
.loginPage("/login.html")
.permitAll();

loginFormUrl必须以"/"或绝对url开头。并且确保能够正确地处理请求CCD_ 11。

BTW:如果您没有配置处理url,则处理url将与post方法的login page(/login.html)url相同,但在您的登录页面中,操作url为/home.html,请将处理url配置为"/home.html">

.formLogin()
.loginProcessingUrl("/home.html")
.loginPage("/login.html")
.permitAll();

默认情况下CSRF已启用,我在您的登录表单中找不到任何CSRF令牌。您可以禁用csrf或使用令牌请求。

http.csrf().disable();

最新更新