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

标签 java jquery html spring-boot 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") 无法解决问题(无论如何在我的情况下都没有)。这里引号中的是将在您的 Controller 中搜索的 URL 路径。这是我的安全配置文件中的片段:

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

这里我写了"/login"。所以现在在你的 Controller 中定义 /login 路径应该指向你的 login.html 页面。

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

只有这样它才能重定向到所需的 View 页面。我通常将 View 页面放在 /webapp/WEB-INF/view 文件夹下。
附言我希望你正确配置了你的 ViewResolver bean,否则它不会工作:)

  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 配置文件中配置资源处理程序。

这里还有一个很好的链接,可以让您全神贯注:Creating a Custom Login Form: Grant access to remaining resources

关于java - 如何使用Spring Security自定义登录页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41787219/

相关文章:

php - 使用复选框过滤 MySQLi

JQuery 转义 HTML 以供预览

java - 如何将 .jar 或 .class 转换为 .dex 文件?

java - 为什么 JTable 总是触发 ListSelectionListener 两次?

jQuery 菜单样式

javascript - 设置并获取按钮 id

java - 重新部署带有计时器任务的单例

java - 降低代码的复杂性

javascript - 我如何关闭这部分 CK 编辑器?

Javascript - 将函数分配给变量无法按预期工作