首页 文章

spring security 4自定义登录页面

提问于
浏览
2

我想在Spring Security中创建自定义的纯html / js登录页面 . 我使用Spring Boot 1.2.5.RELEASE我定义了一个应用程序和配置:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@Configuration
@EnableWebSecurity
@EnableWebMvcSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("a").password("a").roles("USER");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
       http
            .csrf().disable() // DISABLED CSRF protection to make it easier !
            .authorizeRequests()
            .antMatchers("/", "/login.html").permit
            .anyRequest().authenticated()
            .and()
            .formLogin()
            .loginPage("/login.html")
            .permitAll()
            .and()
            .logout()
            .permitAll()
            .logoutUrl("/logout")
            .logoutSuccessUrl("/");
    }

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

我的登录页面看起来像那样(从默认页面复制!)

<html><head><title>Login Page</title></head><body onload='document.f.username.focus();'>
<h3>Login with Username and Password</h3><form name='f' action='/login' method='POST'>
<table>
<tr><td>User:</td><td><input type='text' name='username' value=''></td></tr>
<tr><td>Password:</td><td><input type='password' name='password'/></td></tr>
<tr><td colspan='2'><input name="submit" type="submit" value="Login"/></td>    </tr>
</table>
</form></body></html>

但我仍然有:AUTHORIZATION_FAILURE

是否有可能创建pute html登录页面(没有jsp,thymeleaf等)?我的代码怎么办?

1 回答

  • 1

    您将登录页面配置为 /login.html (使用 loginPage("/login.html") ) . 这还将更改您需要将凭据发布到登录位置 . 文件说明:

    如果将“/ authenticate”传递给此方法[loginPage(String)],则更新默认值,如下所示:/ authenticate GET - 登录表单/ authenticate POST - 处理凭据,如果有效验证用户/身份验证?错误GET - 重定向此处是否验证失败/验证?注销GET - 成功注销后重定向到此处

    为了使登录工作,您需要将 login.html 的凭据发布到 /login.html 而不是 /login .

相关问题