Java SpringBoot学习笔记 37 记住我及首页定制

来自B站【狂神说Java】SpringBoot最新教程IDEA版通俗易懂

1. 代码

1.1 SecurityConfig.java

1.1.1 关闭防止网站攻击,否则注销时报错 http.csrf().disable();
1.1.2 定制首页 loginPage("/toLogin")
1.1.3 自定义登录url loginProcessingUrl("/login")
1.1.4 开启记住我功能 http.rememberMe()
package com.example.springboot06security.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
public class SecurityConfig extends WebSecurityConfigurerAdapter {

	/**
	 * 授权
	 */
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		// 首页所有人可以访问,功能页只有对应有权限的人才能访问
		// 链式编程
		http.authorizeHttpRequests()
			.antMatchers("/").permitAll()
			.antMatchers("/level1/**").hasRole("vip1")
			.antMatchers("/level2/**").hasRole("vip2")
			.antMatchers("/level3/**").hasRole("vip3");
		// 没有权限默认会到登录页面
		// 定制登录页
		http.formLogin().loginPage("/toLogin").usernameParameter("user").passwordParameter("pwd").loginProcessingUrl("/login");
		
		// 关闭防止网站攻击,否则注销时报错
		http.csrf().disable();
		// 开启了注销功能,跳到首页
		http.logout().logoutSuccessUrl("/");
		// 开启记住我功能:勾选记住我,登录成功后,关掉浏览器再打开,还是登录状态,打开浏览器的控制台,在应用程序中可以看到 cookie,默认保存两周
		// 自定义接收前端参数
		http.rememberMe().rememberMeParameter("remember");
	}

	/**
	 * 认证
	 */
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		// 这些数据正常应该从数据库中读
		auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
			.withUser("moqingchi").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");
	}

}

1.2 login.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>登录</title>
    <!--semantic-ui-->
    <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
</head>
<body>

<!--主容器-->
<div class="ui container">

    <div class="ui segment">

        <div style="text-align: center">
            <h1 class="header">登录</h1>
        </div>

        <div class="ui placeholder segment">
            <div class="ui column very relaxed stackable grid">
                <div class="column">
                    <div class="ui form">
                        <form th:action="@{/login}" method="post">
                            <div class="field">
                                <label>Username</label>
                                <div class="ui left icon input">
                                    <input type="text" placeholder="Username" name="user">
                                    <i class="user icon"></i>
                                </div>
                            </div>
                            <div class="field">
                                <label>Password</label>
                                <div class="ui left icon input">
                                    <input type="password" name="pwd">
                                    <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>
                    </div>
                </div>
            </div>
        </div>

        <div style="text-align: center">
            <div class="ui label">
                </i>注册
            </div>
            <br><br>
            <small>blog.kuangstudy.com</small>
        </div>
        <div class="ui segment" style="text-align: center">
            <h3>Spring Security Study by 秦疆</h3>
        </div>
    </div>


</div>

<script th:src="@{/qinjiang/js/jquery-3.1.1.min.js}"></script>
<script th:src="@{/qinjiang/js/semantic.min.js}"></script>

</body>
</html>

2. 其他

浏览器中打开控制台的应用程序,可以查看cookie

posted @ 2022-08-07 15:19  君子键  阅读(37)  评论(0)    收藏  举报