SpringSecurity学习笔记4:首页定制及记住我功能实现

SpringSecurity学习笔记4:首页定制及记住我功能实现

我们希望只要登录之后,关闭浏览器,再登录,就会让我们重新登录,但是很多网站的情况,就是有一个记住密码的功能,无需重新登陆,这个该如何实现呢?

记住我功能实现

  1. 开启记住我功能
//定制请求的授权规则
@Override
protected void configure(HttpSecurity http) throws Exception {
    //。。。。。。。。。。。
    //记住我
    http.rememberMe();
}
  1. 我们再次启动项目测试一下,发现登录页多了一个记住我功能,我们登录之后关闭浏览器,然后重新打开浏览器访问,发现用户依旧存在!

这是如何实现的呢?其实非常简单

我们可以查看浏览器的cookie

在这里插入图片描述

  1. 我们点击注销的时候,可以发现,spring security 帮我们自动删除了这个 cookie

  2. 结论:登录成功后,将cookie发送给浏览器保存,以后登录带上这个cookie,只要通过检查就可以免登录了。如果点击注销,则会删除这个cookie

定制登录页

现在这个登录页面都是spring security 默认的,怎么样可以使用我们自己写的Login界面呢?

  1. 在刚才的登录页配置后面指定 loginpage
//定制登录页
http.formLogin().loginPage("/toLogin");
  1. 然后前端也需要指向我们自己定义的 login请求
<a class="item" th:href="@{/toLogin}">
    <i class="address card icon"></i> 登录
</a>
  1. 我们登录,需要将这些信息发送到哪里,我们也需要配置,login.html 配置提交请求及方式,方式必须为post:
<form th:action="@{/toLogin}" method="post">
    <div class="field">
        <label>Username</label>
        <div class="ui left icon input">
            <input type="text" placeholder="Username" name="username">
            <i class="user icon"></i>
        </div>
    </div>
    <div class="field">
        <label>Password</label>
        <div class="ui left icon input">
            <input type="password" name="password">
            <i class="lock icon"></i>
        </div>
    </div>
    <div class="field">
        <input type="checkbox" name="remember">记住我
    </div>
    <input type="submit" class="ui blue submit button"/>
</form>

4.在登录页增加记住我的多选框

<div class="field">
    <input type="checkbox" name="remember">记住我
</div>

5.后端验证

//开启记住我功能   cookie/默认保存两周的时间
http.rememberMe().rememberMeParameter("remember");
完整的安全配置代码
package com.hsy.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity// 开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页所有人可以访问,但功能页只有有权限的人才能访问
        //请求授权的规则
        http.authorizeRequests()
                .antMatchers("/").permitAll()
                .antMatchers("/level1/**").hasRole("vip1")
                .antMatchers("/level2/**").hasRole("vip2")
                .antMatchers("/level3/**").hasRole("vip3");

        //没有权限要默认跳转到登录页
        // /login 请求来到登录页
        // /login?error 重定向到这里表示登录失败
        //定制登录页
        http.formLogin().loginPage("/toLogin");

        //注销
        //关闭csrf功能:注销失败可能存在的原因
        http.csrf().disable();
        http.logout().logoutSuccessUrl("/");

        //开启记住我功能   cookie/默认保存两周的时间
        http.rememberMe().rememberMeParameter("remember");
    }

    //认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //这些数据正常情况下应该从数据库里读取
        //我们这里是从内存中读取数据
        //密码需要加密才能被SpringSecurity读取
        auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
                .withUser("hsy").password(new BCryptPasswordEncoder().encode("123456")).roles("vip2", "vip3")
                .and()
                .withUser("root").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1", "vip2", "vip3")
                .and()
                .withUser("guest").password(new BCryptPasswordEncoder().encode("123456")).roles("vip1");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

好汤圆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值