Spring Security 自定义表单登录页

1、服务端定义

完善前面的 SecurityConfig 类,继续重写它的 configure(WebSecurity web) 和 configure(HttpSecurity http) 方法,如下:

/**
 * 如果我们使用 XML 来配置 Spring Security ,里边会有一个重要的标签 ,
 * HttpSecurity 提供的配置方法 都对应了该标签。<http>
 * authorizeRequests 对应了 <intercept-url>。
 * formLogin 对应了 <formlogin>。
 * and 方法表示结束当前标签,上下文回到HttpSecurity,开启新一轮的配置。
 * permitAll 表示登录相关的页面/接口不要被拦截。
 * 最后记得关闭 csrf ,关于 csrf 问题我到后面专门和大家说。
 * @param http
 * @throws Exception
 */
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()//开启登录验证
        .anyRequest().authenticated()
        .and()
        .formLogin()
        .loginPage("/login.html") //默认情况下 登录的页面和登录接口是一样的 login.html中的表单提交的action也是login.html
        .loginProcessingUrl("/doLogin")//这个是配置登录post请求验证
        .passwordParameter("password")
        .usernameParameter("name")
        .defaultSuccessUrl("/s") //记录重定向的地址
        //.successForwardUrl("/hello") //记录
        .permitAll()
        .and().csrf().disable();
}

【登录成功】在 Spring Security 中,和登录成功重定向 URL 相关的方法有两个:

  • defaultSuccessUrl
  • successForwardUrl

这两个咋看没什么区别,实际上内藏乾坤。

首先我们在配置的时候,defaultSuccessUrl 和 successForwardUrl 只需要配置一个即可,具体配置哪个,则要看你的需求,两个的区别如下:

defaultSuccessUrl 有一个重载的方法,我们先说一个参数的 defaultSuccessUrl 方法。
如果我们在 defaultSuccessUrl 中指定登录成功的跳转页面为 /index,此时分两种情况,
如果你是直接在浏览器中输入的登录地址,登录成功后,就直接跳转到 /index,
如果你是在浏览器中输入了其他地址,例如 http://localhost:8080/hello,
结果因为没有登录,又重定向到登录页面,此时登录成功后,就不会来到 /index ,而是来到 /hello 页面。
defaultSuccessUrl 还有一个重载的方法,第二个参数如果不设置默认为 false,也就是我们上面的的情况,
如果手动设置第二个参数为 true,则 defaultSuccessUrl 的效果和 successForwardUrl 一致。 successForwardUrl 表示不管你是从哪里来的,登录后一律跳转到 successForwardUrl 指定的地址。
例如 successForwardUrl 指定的地址为
/index ,你在浏览器地址栏输入 http://localhost:8080/hello,
结果因为没有登录,重定向到登录页面,当你登录成功之后,就会服务端跳转到 /index 页面;或者你直接就在浏览器输入了登录页面地址,登录成功后也是来到 /index。

【登录失败】登录失败也是有两个方法:

  • failureForwardUrl
  • failureUrl

 failureForwardUrl 是登录失败之后会发生服务端跳转,failureUrl 则在登录失败之后,会发生重定向。

【注销登录】注销登录的默认接口是 /logout,我们也可以配置。

.and()
.logout()
.logoutUrl("/logout")
.logoutRequestMatcher(new AntPathRequestMatcher("/logout","POST"))
.logoutSuccessUrl("/index")
.deleteCookies()
.clearAuthentication(true)
.invalidateHttpSession(true)
.permitAll()
.and()
  • 默认注销的 URL 是 /logout,是一个 GET 请求,我们可以通过 logoutUrl 方法来修改默认的注销 URL。
  • logoutRequestMatcher 方法不仅可以修改注销 URL,还可以修改请求方式,实际项目中,这个方法和 logoutUrl 任意设置一个即可。
  • logoutSuccessUrl 表示注销成功后要跳转的页面。
  • deleteCookies 用来清除 cookie。
  • clearAuthentication 和 invalidateHttpSession 分别表示清除认证信息和使 HttpSession 失效,默认可以不用配置,默认就会清除。

 

   /**
     * 默认是加密的
     * @return
     */
    @Bean
    PasswordEncoder passwordEncoder(){
        //PasswordEncoder中的功能是 加密和比较的功能
        return NoOpPasswordEncoder.getInstance();//意思是不加密
    }
   //账号密码设置
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("gjq")
                .password("123")
                .roles("admin");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        //web.ignoring() 用来配置忽略掉的 URL 地址,一般对于静态文件,我们可以采用此操作。
        web.ignoring().antMatchers("/js/**","/css/**","/images/**");
    }

前端代码:

<form action="/doLogin" method="post">
    <div class="input">
        <label for="name">用户名</label>
        <input type="text" name="username" id="name">
        <span class="spin"></span>
    </div>
    <div class="input">
        <label for="pass">密码</label>
        <input type="password" name="password" id="pass">
        <span class="spin"></span>
    </div>
    <div class="button login">
        <button type="submit">
            <span>登录</span>
            <i class="fa fa-check"></i>
        </button>
    </div>
</form>

form 表单中,注意 action 为 /login.html ,其他的都是常规操作,我就不重复介绍了。

好了,配置完成后,再去重启项目,此时访问任意页面,就会自动重定向到我们定义的这个页面上来,输入用户名密码就可以重新登录了。

GitHub地址:https://github.com/lenve/spring-security-samples

 

posted @ 2020-05-29 15:32  An-Optimistic-Person  阅读(613)  评论(0编辑  收藏  举报